1

根据一些较早的教程,我创建了一个公式,它使用 mysql_query() 将一些信息存储到 mysql DB 中。但现在我读了一些帖子,说不应该再使用 mysql_query() 所以我试图“翻译”我的旧代码。但之后它不再起作用,并且没有新条目存储在我的数据库中。你能帮忙吗?

<?php
    require '../db/connect.php';

    if ($mysqli->connect_error) {
      echo "Fehler bei der Verbindung: " . mysqli_connect_error();
      exit();
    }

    $flightDate = $_POST['flightDate'];
    $planeID = $_POST['planeID'];
    $planeType = $_POST['planeType'];
    $pilot = $_POST['pilot'];
    $passengers = $_POST['passengers'];


/*  
    $sql = "INSERT INTO `flights`
                (`flightDate`, `planeID`,`planeType`, `pilot`, `passengers`)
            VALUES(
                '" .$flightDate. "',
                '" .$planeID. "',
                '" .$planeType. "',
                '" .$pilot. "',
                '" .$passengers. "'
            )";
    mysql_query( $sql ) or die(mysql_error());
*/



    // prepared statement   
    if($stmt = $mysqli->prepare("INSERT INTO flights
                                        (flightDate, planeID,planeType, pilot, passengers)
                                VALUES (?, ?, ?, ?, ?)")) {
      $stmt->bind_param("sssss", $flightDate, $planeID, $planeType, $pilot, $passengers);
      $stmt->execute();
      echo "Anzahl der veränderten Datensätze : " . $stmt->affected_rows;
      $stmt->close();
    }

    $mysqli->close();

似乎我的变量有问题。如果我直接在浏览器中调用 php 文件,我会收到很多错误消息:

Notice: Undefined index: flightDate in C:\xampp\htdocs\phptomysql\try_2\ajax\addFlight.php on line 9

Notice: Undefined index: planeID in C:\xampp\htdocs\phptomysql\try_2\ajax\addFlight.php on line 10

Notice: Undefined index: planeType in C:\xampp\htdocs\phptomysql\try_2\ajax\addFlight.php on line 11

Notice: Undefined index: pilot in C:\xampp\htdocs\phptomysql\try_2\ajax\addFlight.php on line 12

Notice: Undefined index: passengers in C:\xampp\htdocs\phptomysql\try_2\ajax\addFlight.php on line 13
Anzahl der veränderten Datensätze : -1

我正在使用以下 jquery 函数将值从我的表单传递到 php:

//add flight to db
$('#flightSubmit').on('click', function(){
    var flightDate = ($.datepicker.formatDate("yy-mm-dd", $('#flightDateInput').datepicker("getDate")));
    var planeID = $('input#planeIDInput').val();
    var planeType = $('input#planeTypeInput').val();
    var pilot = $('input#pilotInput').val();
    var passengers = $('input#passengersInput').val();
    $.post('ajax/addFlight.php', {flightDate: flightDate, planeID: planeID, planeType: planeType, pilot: pilot, passengers: passengers}, function(data){
        });
});
4

3 回答 3

0

这与mysqli无关,如果没有发布数据(空),您将收到此错误。

你只需要包装你的代码

if (!empty($_POST) { }

于 2014-01-30T09:14:37.980 回答
0

你有错误吗?或者它只是不插入到数据库中?你能像这样 var_dump 你的 $_POST 和 $stmt

<?php
    require '../db/connect.php';

    if ($mysqli->connect_error) {
      echo "Fehler bei der Verbindung: " . mysqli_connect_error();
      exit();
    }

    $flightDate = $_POST['flightDate'];
    $planeID = $_POST['planeID'];
    $planeType = $_POST['planeType'];
    $pilot = $_POST['pilot'];
    $passengers = $_POST['passengers'];

var_dump($_POST);

    // prepared statement   
$stmt = $mysqli->prepare("INSERT INTO flights
                                        (flightDate, planeID,planeType, pilot, passengers)
                                VALUES (?, ?, ?, ?, ?)");
var_dump($stmt);
    if($stmt) {
      $stmt->bind_param("sssss", $flightDate, $planeID, $planeType, $pilot, $passengers);
      $stmt->execute();
      echo "Anzahl der veränderten Datensätze : " . $stmt->affected_rows;
      $stmt->close();
    }

    $mysqli->close();
于 2013-10-22T15:41:34.720 回答
0

你有mysql_*电话和mysqli_*电话的混合。这行不通解决这个问题。

于 2017-06-05T01:45:13.077 回答