Adding auto increment field breaks my form

Hi, all

I’m running PHP 5.3.1 on a relatively new xampp server.

I’ve build a crude html form that accepts demographic information and calls a .php. The form works when the id (I named it pat_id) field is deleted, but not when I create it. I can insert values just fine from the prompt of course.

Neither the html page nor the php refer to the id field.

Here’s the php…

 $pat_lastn=$_POST['pat_lastn'];
 $pat_firstn=$_POST['pat_firstn'];
 $pat_midn=$_POST['pat_midn'];
 $pat_dob=$_POST['pat_dob'];
 $pat_sex=$_POST['pat_sex'];
 $pat_ssn=$_POST['pat_ssn'];
 $pat_pcphys=$_POST['pat_pcphys'];
 $pat_addr=$_POST['pat_addr'];
 $pat_cor=$_POST['pat_cor'];
 $pat_sor=$_POST['pat_sor'];
 $pat_zip=$_POST['pat_zip'];
 mysql_connect("198.61.179.119", "jweeks", "panther1") or die(mysql_error());
 mysql_select_db("test") or die(mysql_error());
 mysql_query("INSERT INTO `data` VALUES ('$pat_lastn', '$pat_firstn', '$pat_midn', '$pat_dob', '$pat_
sex', '$pat_ssn', '$pat_pcphys', '$pat_addr', '$pat_cor', '$pat_sor', '$pat_zip')");
 Print "Your information has been successfully added to the database.";
?>

Thanks ahead of time for constructive criticism and help.

It would be better to specify the fields in your query. Something like


$query = "INSERT INTO `data` ( last_name, first_name ) VALUES ( '{$last}', '{$first}' )";

Of course, you should also look into sanitizing your input (you shoud never place user input directly into a query) but that’s unrelated to your issue at hand.

If your auto increment field is created as the first field of your table, specifying the fields in the query like that should solve it. If this doesn’t solve it, please let us know what error you’re getting or what happens that you aren’t expecting.

The issue went a when when I ended up doing…

if (mysqli_connect_errno()) {
printf("Connect failed: %s
", mysqli_connect_error());
exit();
} else {

mysqli_query($mysqli,"INSERT INTO data (pat_lastn, pat_firstn, pat_midn, pat_dob, pat_sex, pat_ssn, pat_pcphys, pat_addr, pat_cor, pat_sor, pat_zip)

VALUES (‘$_POST[pat_lastn]’, ‘$_POST[pat_firstn]’, ‘$_POST[pat_midn]’, ‘$_POST[pat_dob]’, ‘$_POST[pat_sex]’, ‘$_POST[pat_ssn]’, ‘$_POST[pat_pcphys]’, ‘$_POST[pat_addr]’, ‘$_POST[pat_cor]’, ‘$_POST[pat_sor]’,
‘$_POST[pat_zip]’)");

    mysqli_close($mysqli);