I am trying to insert a row into database and im getting the following 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 ' '', '', now())' at line 1.
And if I use single quote outside $gradeid
it will get converted to string and since my database column is integer and it is also a foreign key, even this gets failed with the following error
Cannot add or update a child row: a foreign key constraint fails (`mydb`.`mytable1`, CONSTRAINT `activitygoalmaster_ibfk_1` FOREIGN KEY (`intGradeID`) REFERENCES `mytable2` (`intGradeID`))
Im not getting whats the error? May be some simple mistake but Im not getting it. Please help. My code in php is
$title = $_POST['Title'];
$gradeid = $_POST['GradeID'];
$masterimgpath = $_POST['MasterImgPath'];
$description = $_POST['Description'];
$sql = "INSERT INTO activitygoalmaster(vchTitle, intGradeID, vchMasterImgPath, vchDescription, dtCreatedAt)VALUES('$title', $gradeid, '$masterimgpath', '$description', now())";
$result = mysql_query($sql);
Looks like gradeid
is empty. Try to verify it.
Do not use mysql-*
functions, because this is deprecated. Use mysqli_*
or pdo with prepared statement.
Please check table structure and his value.
print your $sql before execute it. so you can find error easily.
I have a question: intGradeID
column is int()
right? Is it a varchar()
? If it is a varchar()
then it really needs single quotes in the insert
query.
To solve the error with a foreign key constraint fails
, please insert to mytable2
the grade id
first before inserting it to mytable1
. It shows this error because it is required by a foreign key
on mytable1
.
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.