du532861657 2013-06-16 06:02
浏览 13
已采纳

无法将uniqid()值插入MySQL数据库

I've inserting rows into a database from a test form and when I bring in uniqid(), it doesn't work. The form works fine and inserts the data but when I bring in the unique row it doesn't.

I originally had the 'unique' row in my MySQL database set as VARCHAR(500) and didn't know if that was the problem so I changed it to TEXT(500). It shouldn't be the problem since I've echoed out $unique and it's just numbers and letters but I wasn't sure if the server side script interprets it a certain. I also didn't know what else to do to debug! I've searched around to if there's something special I should do when inserting a uniqid() value into a database but it doesn't seem to be different than any other value. Am I missing something? Thanks for your help!

$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>";
}

EDIT

Echo'ed $mysqli-error and this is what I got:

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

I don't see an error in that area of the code?

  • 写回答

1条回答 默认 最新

  • doutuzhuohao6449 2013-06-16 08:06
    关注

    UNIQUE is a reserved MySQL word. You should use backticks and that should fix the problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么