我正在尝试创建一个PHP脚本来执行以下操作:
我有以下代码:
<?php
/* Attempt MySQL server connection. Assuming you are running MySQL
server with default setting (user 'root' with no password) */
$link = mysqli_connect("localhost", "root", "Godzilla7", "ucsd");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
// Escape user inputs for security
$first_name = mysqli_real_escape_string($link, $_POST['firstname']);
$last_name = mysqli_real_escape_string($link, $_POST['lastname']);
$mail_address = mysqli_real_escape_string($link, $_POST['address']);
$city_address = mysqli_real_escape_string($link, $_POST['city']);
$zip_address = mysqli_real_escape_string($link, $_POST['zip']);
$email_address = mysqli_real_escape_string($link, $_POST['email']);
$type = mysqli_real_escape_string($link, $_POST['type']);
$r_date = mysqli_real_escape_string($link, $_POST['r_date']);
$c_date = mysqli_real_escape_string($link, $_POST['c_date']);
$note = mysqli_real_escape_string($link, $_POST['note']);
$indexNum = mysqli_real_escape_string($link, $_POST['indexNum']);
$bill = mysqli_real_escape_string($link, $_POST['billing']);
$billing_a = mysqli_real_escape_string($link, $_POST['billing_a']);
$billing_e = mysqli_real_escape_string($link, $_POST['billing_e']);
// check to see that the data is filled out
if(!$first_name || !$last_name || !$email_address || !$indexNum){
echo " You have not entered all the required details.<br/>
Please try again.";
exit;
}
// attempt insert query execution
$sql = "INSERT INTO btc (first_name, last_name, mail_address, city_address, zip_address, email_address, type, r_date, c_date, note, indexNumber, bill, billing_a, billing_e)
VALUES ('$first_name', '$last_name', '$mail_address', '$city_address', '$zip_address', '$email_address', '$type', '$r_date', '$c_date', '$note', '$indexNum', '$bill', '$billing_a', '$billing_e')";
/* if the insert is successful, the following three things will happen
* 1. the index number for the insert will be pulled from the database
* 2. the user email will be pulled based on the index number
* 3. the user will be emailed the index number
*/
if(mysqli_query($link, $sql)){
echo "Records added successfully. <br>";
}else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
$sql = "SELECT index FROM btc ORDER BY index DESC LIMIT 1";
if($result_i = mysqli_query($link, $sql)){
// debugging statement, currently not showing up
echo "index is $result_i";
}else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
$sql = "SELECT email_address FROM btc ORDER BY index DESC LIMIT 1";
if($result_e = mysqli_query($link, $sql)){
echo "Email is $result_e";
}else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
//pass index and email to mail msg and send to user
$msg = "Thank you very much for your order, your ID is $result_i.";
$msg = wordwrap($msg, 70);
mail($result_e, "Your Order", $msg);
// close connection
mysqli_close($link);
现在索引编号没有被拉出,我收到以下错误:
错误:无法执行SELECT索引FROM btc ORDER BY索引DESC LIMIT 1.您的SQL语法中有错误;检查与您的MySQL服务器版本相对应的手册,以便使用索引附近的正确语法FROM btc ORDER BY index DESC LIMIT 1&#39;在第1行
答案 0 :(得分:0)
index
是MySQL中的保留字
点击此处:http://dev.mysql.com/doc/refman/5.6/en/reserved-words.html
您仍然使用字段名称索引,但它需要以反引号引用:
$sql = "SELECT `index` FROM btc ORDER BY `index` DESC LIMIT 1";