Editing a field in a record in PHP using a form

I know to edit but I only want to edit one field using a form but when I use the form all the other fields which I don’t want edited are blanked, when I edit just the one or two fields of the table.
$query = “update student set sname=’$sname’,smobile=’$smobile’,sgender=’$sgender’,scity=’$scity’ where sid=”.$_POST[“sid”];

From a range in the form I only want to edit one field only.

Regards.,
John

Dynamically build the set statement for one thing.

First you create and array that matches the $_POST keys to their respective column values. This prevents someone trying to pass in something that could damage the DB.

If the value is not empty add it to the array.

You should also be using prepared statements.

Sponsor our Newsletter | Privacy Policy | Terms of Service