Thursday, 20 July 2017

php - error in your SQL syntax; MariaDB server version for the right syntax to use near ''studentid') VALUES ('52')' at line 1





$studentCheckQ = "select * from students order by studentid desc";
$studentCheckQR = mysqli_query($con,$studentCheckQ);
$studentCheckRow = mysqli_fetch_assoc($studentCheckQR);
$latestStudentID = $studentCheckRow['studentid'];
$newAdmissionQ = "INSERT INTO `admission` ('studentid') VALUES ('$latestStudentID');";
$newAdmissionQR = mysqli_query($con,$newAdmissionQ);

if($newAdmissionQR){
echo "successfully added";
}
else
{
echo mysqli_error($con);
}


I'm facing this error when I run my code





You have an error in your SQL syntax; check the manual that
corresponds to your MariaDB server version for the right syntax to use
near ''studentid') VALUES ('52')' at line 1



Answer



You should use variables inside brackets and don't quote columns... And maybe, you should avoid quotation of table names, only values need to be quoted:



$newAdmissionQ = "INSERT INTO admission (studentid) VALUES ('{$latestStudentID}')";



Also, the final ; is not needed if you will only make a request.



Instead, I suggest you to use PDO:



http://thisinterestsme.com/pdo-insert-example/



Is more practical, because you can change its system anytime (Oracle, MySQL, etc...) and also, avoid SQL injections.


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...