How to solve the Column ID cannot be null?

Why I am getting an error like this if I have my ExamineeID as Auto_Increment?
Uncaught mysqli_sql_exception: Column ‘ExamineeID’ cannot be null

$sqlhonor = "SELECT Honor1, Honor2, Honor3, Honor4 From honor Where Honor1 = ? And Honor2 = ? And Honor3 = ? And Honor4 = ? ";

if ($stmthonor = $conn->prepare($sqlhonor)) {
					$INSERThonor = "INSERT INTO honor (Honor1,Honor2,Honor3,Honor4, ExamineeID) values (?,?,?,?,?)";
					$stmthonor -> bind_param("ssss", $Honor1,$Honor2,$Honor3,$Honor4);
					$stmthonor -> execute();
					$stmthonor -> bind_result($Honor1,$Honor2,$Honor3,$Honor4);
					$stmthonor -> store_result();
					$rnum = $stmthonor -> num_rows;

				if ($rnum == 0 ) {	
				$stmthonor = $conn -> prepare($INSERThonor);
				$stmthonor -> bind_param ("ssssi", $Honor1, $Honor2, $Honor3, $Honor4, $ExamineeID);	
				$stmthonor -> execute();	

If you remove the ExamineeID column, what will happen?

That is a very bad way of using a database. What about database normalization?

Someone said that I need to insert the ExamineeID so the Foreign key will be working. Because, before I have an error of using the Foreign key because the other table cannot read the ExamineeID. I have a problem of calling the $ExamineeID in the bind_param, that’s why I have an error that the Column ExamineeID cannot be null. I do not know how to call the primary key and inserted it to the other table.

And, for the database normalization, I want to do it after this issue, on how to insert the primary key for the other table because of the Foreign key

Ah so you have two tables

  1. Examinee
  2. Honor

The Examinee table should have this columns
ExamineeID: integer, primary key, autoincrement.
And other normal columns

The Honor table should have this colums:
HonorID: integer, primary key, autoincrement.
ExamineeID: integer, foreignkey (and NOT autoincrement)
And other normal columns

Your queries:
INSERT INTO Examinee (Othercolumn1, Othercolumn2) values (?,?)
INSERT INTO honor (Honor1,Honor2,Honor3,Honor4, ExamineeID) values (?,?,?,?,?)

When you need the bright new key from the first query to add to the second query use $conn->insert_id.

Is insert_id a built in from the php though? I am having a problem with the syntax since I just learn php for 2 weeks though


Because the new record get an ID automatically from the MySql server PHP cannot know this ID unless you ask for it.

Okay. So, inserting the ExamineeID in the INSERT is right and for the bind_param of the insert, I will insert that insert_id?

Well it is right for the foreign key column in the Honor table yes. Not for the primary and autoincrement columns. And yes you can use that insert_id .

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service