简体   繁体   中英

Not Able to Insert uniqid() value into MySQL database

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM