将php mysql_query()更新为准备好的语句

时间:2013-06-29 07:37:13

标签: php mysqli

根据一些较旧的教程,我创建了一个使用mysql_query()将一些信息存储到mysql数据库中的公式。 但现在我已经阅读了一些帖子,说不再使用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){
        });
});

3 个答案:

答案 0 :(得分:0)

你收到错误吗? 或者它只是不插入数据库? 你能不能把你的$ _POST和$ stmt var_dump这个

<?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();

答案 1 :(得分:0)

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

你只需要用

包装你的代码

if (!empty($_POST) { }

答案 2 :(得分:0)

您有mysql_*次来电和mysqli_*来电。这将工作。修复首先