我已经从测试表单将行插入到数据库中,当我引入 uniqid() 时,它不起作用。该表单工作正常并插入数据,但是当我引入唯一的行时,它却没有。
我最初将 MySQL 数据库中的“唯一”行设置为 VARCHAR(500),但不知道这是否是问题所在,因此我将其更改为 TEXT(500)。这不应该是问题,因为我已经回显了 $unique 并且它只是数字和字母,但我不确定服务器端脚本是否对它进行了一定的解释。我也不知道还能做什么来调试!我已经搜索过在将 uniqid() 值插入数据库时是否应该做一些特殊的事情,但它似乎与任何其他值没有什么不同。我错过了什么吗?感谢您的帮助!
$test1 = $mysqli->real_escape_string($_POST['test1']);
$test2 = $mysqli->real_escape_string($_POST['test2']);
$test3 = $mysqli->real_escape_string($_POST['test3']);
$test4 = $mysqli->real_escape_string($_POST['test4']);
$unique = uniqid();
//ECHO OUT UNIQUE ID TO MAKE SURE IT WORKED
echo "<script>alert('".$unique."');</script>";
$sql_insert = "INSERT INTO users (test1, test2, test3, test4, unique) VALUES ('$test1', '$test2', '$test3', '$test4', '$unique')";
$result_insert = $mysqli->query($sql_insert);
//ECHO OUT UNIQUE ID AGAIN TO MAKE SURE IT REMAINS THE SAME (AND IT DOES)
echo "<script>alert('".$unique."');</script>";
//IF EVERYTHING WORKS, FORWARD TO SECOND FORM PAGE
if($result_insert){
header('Location:second_form.php');
}
//IF THERE IS AN ERROR INSERTING THE DATA, GIVE AN ERROR MESSAGE
else{
echo "<script>alert('There was an error submitted your info. Please try again.');</script>";
}
编辑
回显 $mysqli-error 这就是我得到的:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'unique) VALUES ('Test1', 'Test2', 'Test3', 'Test4', '51bd57e7bf4dd')' at line 1
我在代码的该区域没有看到错误?
最佳答案
UNIQUE
是 reserved MySQL word 。您应该使用反引号,这应该可以解决问题。
关于php - 无法将 uniqid() 值插入 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17130641/