简体   繁体   English

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

[英]Updating php mysql_query() to prepared statement

according to some older tutorials i created a formular that stores some information into a mysql DB with mysql_query(). 根据一些较旧的教程,我创建了一个公式器,该公式器使用mysql_query()将一些信息存储到mysql数据库中。 But now i've read some posts, that say one shouldn't use mysql_query() any longer so i've tried to "translate" my old code. 但是现在我读了一些帖子,说一个人不应该再使用mysql_query()了,所以我试图“翻译”我的旧代码。 But afterwards it doesn't work anylonger and no new entries are stored in my DB. 但是此后它不再起作用,并且没有新条目存储在我的数据库中。 Can you help please? 你能帮忙吗?

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

Seems like there is something wrong with my variables. 好像我的变量有问题。 If i call the php file directly in the browser i get a lot of error messages: 如果我直接在浏览器中调用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

I'm using the following jquery function to pass the values from my form to the php: 我正在使用以下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){
        });
});

Do you get an error? 收到错误消息吗? Or it just dont insert int o the db? 还是只是不向数据库插入int? Could you please var_dump your $_POST and $stmt like this 您能像这样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();

this is not related to mysqli, you will get this error if there is no posted data (empty). 这与mysqli无关,如果没有发布的数据(空),则会出现此错误。

you just have to wrap your code with 您只需要用

if (!empty($_POST) { }

You have a mixture of mysql_* calls and mysqli_* calls. 您混合了mysql_*mysqli_*调用。 This will not work. 这将无法正常工作。 Fix that first . 首先解决该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM