我想創建這個表單&讓它通過PHP將信息傳遞給mySQLi服務器。沒有任何字段傳遞輸入的信息,但是當我提交表單時,表中會創建一個新行。此外,如果我嘗試再次提交表格,但仍然在表格中的最前面的行,我得到錯誤表單輸入不通過PHP到mySQL數據庫
Error: INSERT INTO DOC_Tracking (Num_Start, Num_End, Sch_Org_Name, Date_Entered, Date_Distributed) VALUES ('', '', '', '', '') Duplicate entry '' for key '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">
to
<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>
<?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();
?>
你永遠定義變量,但您使用它們。當然他們是空的。請閱讀關於PHP的基本教程。還要學習如何立即在SQL查詢中使用參數。 –
@SamiKuhmonen你願意修改代碼中的變量,以便我可以看到你在說什麼嗎? – user2275209