表单输入未通过PHP传递到mySQL数据库

时间:2016-09-23 03:39:16

标签: javascript php mysql mysqli

我正在尝试创建此表单&让它通过PHP将信息传递给我的mySQLi服务器。没有字段传递输入的信息,但是当我提交表单时,表格中会生成一个新行。此外,如果我尝试再次提交表格,同时仍然在表格中的forst creted行,我收到错误

  

错误:INSERT INTO DOC_Tracking(Num_Start,Num_End,Sch_Org_Name,Date_Entered,Date_Distributed)VALUES('','','',&# 39;','')   重复输入''关键' PRIMARY'

有什么想法吗?

HTML表单:



<!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('Y-m-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>
&#13;
&#13;
&#13;

&#13;
&#13;
<?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();
?>
&#13;
&#13;
&#13;

Screenshot from PHPmyAdmin

2 个答案:

答案 0 :(得分:0)

您的某个表格列可能标记为必须唯一的主键,但是您要为每列提交值 - 与您的第一次总结相同的数据。

如果您需要唯一密钥,可以添加ID列吗?

答案 1 :(得分:0)

在与'id'或'trackId'相同的字段中创建一个单独的字段,并将其设为'主键'并选择自动增量选项,以便每行具有唯一的ID。希望这会有所帮助。如果需要任何细节,请告诉我。