看看你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:如果我可能會問,如何提供幫助? –