2016-09-17 85 views
1

有兩個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); 
?> 
+0

請提供您的問題和錯誤代碼全部細節。也使用mysqli作爲mysql貶損 – rak007

+0

我試過mysqli以及問題依然存在。問題很簡單。我的數據未提交 –

+0

是整個PHP代碼?或只是其中的一部分?如果($ result)條件結果爲假,則爲 –

回答

1

您遇到的錯誤的插入數據的代碼提交表單代碼(Duplicate entry '51' for key 'PRIMARY')似乎非常合乎邏輯因爲你正在給你指定的ID行而不是自動寫入。此外,您正在使用基於當前行數量的ID。這導致了ID重複的MySQL錯誤。

要解決此問題:

  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。這樣可以避免因爲您不再插入自己的號碼而導致身份證複製等錯誤。

    旁註(但很重要):你應該使用mysqliPDO statements因爲mysql擴展已被棄用(和甚至在PHP 7.0.0刪除)。

    +0

    非常感謝Edvin,我也在想,因此,我實現了這些目標,並且工作。特別是對於命令:「mysql_error($ conn)」非常感謝。這個命令解決了這個問題,因爲它給了我們什麼錯誤的想法 –

    +0

    @ user3119917我很高興幫助!如果可以,請考慮upvoting /接受我的答案,因爲它確實幫助我很多(接受答案也會給你+2聲望)。祝你好運! –

    0

    在phpmyadmin的結構爲我的表,我需要設置列「的SerialNo」到AI(自動增量),並在該插入數據代碼,我需要評論的行:

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

    類似地,我需要從插入查詢中刪除同一個文件中的'serialNo'。最後,我需要評論的線路:

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

    在插入數據代碼

    相關問題