看看你VALUES
线路查询:
VALUES ('$firstname','$lastname','$email','$adress',''$mobilephone','$postalcode',city,'$homephone','$deliveryinfo','$totalprice','$status')
不宜,city,
是,'$city',
?为什么会有两个单引号'
在$mobilephone
前尝试改变的是这样的:
VALUES ('$firstname','$lastname','$email','$adress','$mobilephone','$postalcode','$city','$homephone','$deliveryinfo','$totalprice','$status')
这就是说,你也没有做任何错误捕获的查询。这里有很多问题。
这就是说,这里是你的代码的重构,使这一切都更清洁&更稳定。请注意,使用mysqli_error
报告查询错误以及连接的mysqli_connect_error
。还使用$_POST
值的数组来允许更轻松地处理$_POST
值。这也允许对isset()
和!empty()
的输入进行一些基本验证。还使用mysqli_stmt_bind_param
在查询中设置变量。最后使用mysqli_free_result
& mysqli_close
干净地获得空闲内存&关闭连接后,一切都说&完成。
// Sundry items set by the original poster in the original code.
error_reporting(E_ALL);
ini_set('display_errors', '1');
header('Content-type: text/plain; charset=utf-8');
session_start();
// Credentials.
$host = "127.0.0.1";
$user = "root";
$password = "123456";
$database = "bikeshop";
// Connecting, selecting database
$con = mysqli_connect($host, $user, $password, $database) or die("Failed to connect to MySQL: " . mysqli_connect_error());
// Set a '$_POST' array and roll through each value.
$post_array = array('totalprice2', 'firstname', 'lastname', 'email', 'adress', 'mobilephone', 'postalcode', 'city', 'homephone', 'deliveryinfo');
foreach ($post_array as $post_key => $post_value) {
$$post_value = isset($_POST[$post_value]) && !empty($_POST[$post_value]) ? $_POST[$post_value] : null;
}
// Set the other variables.
$name = $_SESSION['username'];
$status = 'pending';
// Set the query.
$query = "INSERT INTO `orderbank` (`firstname`, `lastname`, `email`, `adress`, `mobilephone`, `postalcode`, `city`, `homephone`, `deliveryinfo`, `cost`, `status`)"
. " VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
;
// Bind the params.
mysqli_stmt_bind_param($insertsql, 'sssssssssss', $firstname, $lastname, $email, $adress, $mobilephone, $postalcode, $city, $homephone, $deliveryinfo, $totalprice2, $status);
// Run the query.
$result = mysqli_query($con, $insertsql) or die(mysqli_error());
// Free the result set.
mysqli_free_result($result);
// Close the connection.
mysqli_close($con);
你是不是检查错误... – siride
你有',城市,'不带引号或印记。这肯定会给你一个错误。另外,请不要使用插入的字符串,也不要使用无用户输入。这太可怕了。 – siride
@ user0000000:如果我可能会问,如何提供帮助? –