简体   繁体   中英

Form Input Not Passing Through PHP to mySQL Database

I am trying to create this form & have it pass the information through PHP to my mySQLi server. None of the fields are passing the entered info through, but when I submit the form, a new row is made in the table. Also if I try to submit the form again with the forst creted row still in the table, I get the error

Error: INSERT INTO DOC_Tracking (Num_Start, Num_End, Sch_Org_Name, Date_Entered, Date_Distributed) VALUES ('', '', '', '', '') Duplicate entry '' for key 'PRIMARY'

Any ideas?

HTML Form:

 <!doctype html> <html> <head> <meta charset="UTF-8"> <title>DOC Tracker</title> <script type="text/javascript"> var downStrokeField; function autojump(fieldName,nextFieldName,fakeMaxLength) { var myForm=document.forms[document.forms.length - 1]; var myField=myForm.elements[fieldName]; myField.nextField=myForm.elements[nextFieldName]; if (myField.maxLength == null) myField.maxLength=fakeMaxLength; myField.onkeydown=autojump_keyDown; myField.onkeyup=autojump_keyUp; } function autojump_keyDown() { this.beforeLength=this.value.length; downStrokeField=this; } function autojump_keyUp() { if ( (this == downStrokeField) && (this.value.length > this.beforeLength) && (this.value.length >= this.maxLength) ) this.nextField.focus(); downStrokeField=null; } </script> </head> <body> <h1>DOC Tracking System</h1> <br> <form ACTION="insert.php" METHOD="POST"> DOC Number Range: <input type="number" id="Num_Start" NAME="DOC_NUM_START" MAXLENGTH="14" SIZE="14"> &nbsp; to &nbsp; <input type="number" id="Num_End" NAME="DOC_NUM_END" MAXLENGTH="14" SIZE="14"> <br> School/Organization Name: <input type="text" id="Sch_Org_Name" NAME="Organization" SIZE="50"> <br> Today's Date: <input type="date" id="Date_Entered" value="<?php echo date('Ym-d'); ?>" name="Date_Entered" /> <br> Date Coupons To Be Distributed: <input type="date" id="Date_Distributed" name="Date_Distributed" /> <br><br> <input type="submit"> </form> <script TYPE="text/javascript"> autojump('DOC_NUM_START', 'DOC_NUM_END', 14); autojump('DOC_NUM_END', 'Organization', 14); </script> </body> </html> 

 <?php $servername = "localhost"; $username = "wilderx2_chris"; $password = "chrisw"; $dbname = "wilderx2_DOCTRACK"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $sql = "INSERT INTO DOC_Tracking (Num_Start, Num_End, Sch_Org_Name, Date_Entered, Date_Distributed) VALUES ('$Num_Start', '$Num_End', '$Sch_Org_Name', '$Date_Entered', '$Date_Distributed')"; if ($conn->query($sql) === TRUE) { echo "New record created successfully"; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); ?> 

Screenshot from PHPmyAdmin

One of your table columns is likely marked as a primary key that must be unique, however you are submitting values for each column - the same data as your first sumbission.

If you need a unique key, add an ID column perhaps?

Create a separate field in same field as 'id' or 'trackId' and make it as 'primary key' and select auto increment option so that each row having unique id. Hope this will help. let me know if any details required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM