I've been recently working on my course work and stuck with the problem.
The case is this: User makes a request of an art work from the website HTML, input data such as UserID for who the art must be made is needed to include into SQL table, but it gives an error that I can't modify a foreign key that it takes from session data
This is the error it displays and the code itself
errorCannot add or update a child row: a foreign key constraint fails (
project work
.arts
, CONSTRAINT arts_ibfk_1FOREIGN KEY (
For_UserID) REFERENCES
users(
UserID`))
<?php
session_start();
$genre=$_POST['genre'];
$ext=$_POST['ext'];
$desc=$_POST['desc'];
$conn= new mysqli("127.0.0.1", "root", "","project work") or die ("Can't connect to db");
if (($genre!="") and ($ext!="") and ($desc!="")) {
$query= "INSERT INTO `Arts` (`Genre`,`Extension`,`Description`,`For_UserID`,`Is_Done`)
VALUES ('$genre','$ext','$desc','{$_SESSION['UserID']}','0')";
if ($conn->query($query)== TRUE)
echo "Succesfully!" ;
else die('error' .$conn->error);
}
else echo "Fill all the fields, please!";
?>
Is there a way to avoid this error?