数据未提交到数据库,并出现消息"ERROR"所示


Data is not submitted to the database and a message "ERROR" shows

有两个php文件,一个包含表单,另一个包含将表单数据插入数据库中的表的代码。

这是第一个文件

中的提交表单代码
<form name="form" action="insert_dataE.php" onSubmit="return  
validation()" method="post" id="formServiceEntry">
  <fieldset id="fieldSetServiceEntry">
    <legend align="center">Please fill the form</legend>
    <p class="FieldHeading"><i>Vehicle No(*): </i></p>
    <input id="VehicleNoFieldArea" type="text" name="VehicleNoField" 
size="6" maxlength="8"/>
    <p class="FieldHeading"><i>Description(*):</i></p>
    <textarea id="descriptionFieldArea" name="descriptionField" 
rows="2" cols="20" size="15" maxlength="18"></textarea>
    <p class="FieldHeading"><i>Total(*):</i></p>
    <input id="totalFieldArea" name="totalField" type="text" 
size="4" maxlength="4"/>
    <p id="amountFieldHeading"><i>Bill(*):</i></p>
    <input id="amountFieldArea" name="amountField" type="text" 
size="3" maxlength="3" onKeyUp="balance();" />
    <br/>
    <div id="divisionRadioButton">
      <h3 id="radioButtonHeading">Service(*):</h3>
      Service &nbsp;
      <input class="textFields" type="radio" 
name="serviceSelection" value="service" checked />
      <br/>
      Wash &nbsp;
      <input class="textFields" type="radio" 
name="serviceSelection" value="wash" />
    </div>
    <p id="balanceFieldHeading"><i>Balance(*):</i></p>
    <input id="balanceFieldArea" name="balanceField" type="text" 
size="4" maxlength="4"/>
  </fieldset>
  <input class="btnsSE" type="submit" name="Button" value="Submit" />
  <input class="btnsSE" type="reset" name="Button" value="Reset Form"/>
  <input type="button"  class="btnsSE" value="Back to the staff 
interface" onClick="window.location='staffE.php';"/>
</form>

这是从第二个文件

插入数据代码
<?php
// Connects to your Database 
$conn=mysql_connect("localhost", "webgeek1_service", "6defyu4642070") or 
die(mysql_error()); 
mysql_select_db("webgeek1_software_order", $conn) or die(mysql_error()); 
$result = mysql_query("SELECT * FROM application", $conn);
$num_rows = mysql_num_rows($result);
$num_rows = $num_rows + 1;
$id= $num_rows;
$dateAndTime = date('y-m-d H:i:s',time());
$vehicleNo=mysql_real_escape_string($_POST['VehicleNoField']);
$description=mysql_real_escape_string($_POST['descriptionField']);
$amount=mysql_real_escape_string($_POST['amountField']);
$service=mysql_real_escape_string($_POST['serviceSelection']);
// Build an sql statment to add the query details
$sql="INSERT INTO  `webgeek1_software_order`.`application`(`serialNo`, 
`dateAndTime` , `vehicleNo` , `description` ,`amount`,`service`) 
VALUES
('$id',      
'$dateAndTime','$vehicleNo','$description','$amount','$service')";
$result = mysql_query($sql, $conn);
if($result)
{
    echo "<p id='headingInsertData'>Service Station Web Application</p>";
    echo "<p id='receiptHeading'>Receipt</p>";
    echo "<div id='mainFieldsInsertData'>";
    echo  "Serial No: " . " " . $id; 
    echo "<br/>";
    echo "Date and Time: " . " " . $dateAndTime;
    echo "<br/>";
    echo "Vehicle No: " . " " . $vehicleNo;
    echo "<br/>";
    echo "Description: " . " " . $description;
    echo "<br/>";
    echo "Amount: " . " " . $amount;
    echo "<br/>";
    echo "Service:" . " " . $service;
    echo "<br/>";
    echo "<br/>";
    echo"Thanks for using our services";
    echo "</div>";
    echo "<div id='footerInsertData'>";
    echo "<i>Developed by: Web Geeks - Information Technology (IT) 
    Company</i>";
    echo "</div>";
    echo "<div align='center'>";
    echo "<input class='btns' type='button' value='Print'  
    onClick='javascript: window.print();'/>";
    echo "<input type='button'  class='btns' value='Back to the 
    Application' onClick='newDoc()'/>";
    echo "</div>";
}
else 
{
    echo "ERROR";
}
// close connection 
mysql_close($conn);
?>

你的错误(Duplicate entry '51' for key 'PRIMARY')似乎很合乎逻辑,因为你给你指定的ID行,而不是自动写入。此外,您使用的ID是基于当前的行数。这将导致MySQL错误与ID重复。

解决方法:

  1. 修改serialNo列,在A_I列(AUTO_INCREMENT)上勾选复选框。这将确保您始终拥有唯一的ID

  2. 完全删除代码中的这一部分:

$result = mysql_query("SELECT * FROM application", $conn); $num_rows = mysql_num_rows($result); $num_rows = $num_rows + 1; $id= $num_rows;

  • 修改查询:
  • 这个已经修改了

    $sql="INSERT INTO `webgeek1_software_order`.`application` (`dateAndTime`, `vehicleNo`, `description`,`amount`, `service`) VALUES('$dateAndTime', '$vehicleNo', '$description', '$amount', '$service')";
    

    我相信数据库应该自动设置插入新数据后的下一个ID。这将防止您获得诸如ID重复之类的错误,因为您不再插入自己的编号。

    一个边注(但很重要):您应该使用mysqliPDO statements,因为mysql扩展已被弃用(甚至在PHP 7.0.0中被删除)。

    在phpmyadmin中的表结构中,我需要将列'serialNo'设置为AI (Autoincrement),并且在插入数据代码中,我需要注释以下行:

    $num_rows = mysql_num_rows($result);
    $num_rows = $num_rows + 1;
    $id= $num_rows;
    

    同样,我需要从同一文件中的插入查询中删除'serialNo'。最后,我需要注释以下行:

    echo  "Serial No: " . " " . $id; 
    echo "<br/>";
    

    插入数据代码