根据一些较早的教程,我创建了一个公式,它使用 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){
});
});