Updating MySQL with PHP using mysqli

Posted in Tutorials

Tweet This Share on Facebook Bookmark on Delicious Digg this Submit to Reddit

This tutorial continues directly from the last tutorial where we inserted an record.  Now we will update the record that was just inserted.  Adding to the previous code, we construct an MySQL query UPDATE statement in the string “$query” to set the username to “Test Three” in the newly inserted record…

update with mysqli

update with mysqli

The UPDATE statement needs an id in its WHERE clause.  To obtain the id of the newly insert records, we call mysqli_insert_id with the database handle.  Note that this only works on a “table with a column having the AUTO_INCREMENT attribute. If the last query wasn’t an INSERT or UPDATE statement or if the modified table does not have a column with the AUTO_INCREMENT attribute, this function will return zero.” (from PHP manual).