2016-08-19 64 views
0

我完全不熟悉PHP,並且正在爲包含公司和聯繫人的地址簿工作。當點擊提交更新它剛剛與舊的數據最初在那裏這裏替換它的數據是企業的HTML表單:使用php問題的mysql更新表單不斷更新舊信息不是新更新

<html> 
<head><title>Update Records in MYSQL Database</title> 
    <style> 
     body {background-color: powderblue;} 
     form {border: 5px solid midnightblue; 
      padding: 40px;} 
     label {color: darkblue;} 
     form {border: 5px solid midnightblue; 
      padding: 40px; 
    </style> 
</head> 
<body> 
<?php 
    //Connect to the Database 
    $link = mysqli_connect('localhost', '', ''); 
    //Select the Database 
    mysqli_select_db($link, 'ADDRESS_BOOK_DB'); 
    //Select Query 
    $sql = "SELECT * FROM Companies"; 
    //Execute the Query 
    $records = mysqli_query($link, $sql); 
?> 
<form> 
    <table> 
     <tr> 
      <th>CompanyName</th> 
      <th>Address</th> 
      <th>City</th> 
      <th>State</th> 
      <th>ZipCode</th> 
      <th>PhoneNumber</th> 
     </tr> 
     <?php 
     while($row = mysqli_fetch_array($records)) { 
      echo "<tr><form action=update.php method='post'></form>"; 
      echo "<td><input type='text' name='CompanyName' value='" . $row['companyname1'] . "'></td>"; 
      echo "<td><input type='text' name='address' value='" . $row['address'] . "'></td>"; 
      echo "<td><input type='text' name='city' value='" . $row['city'] . "'></td>"; 
      echo "<td><input type='text' name='state' value='" . $row['state'] . "'></td>"; 
      echo "<td><input type='text' name='zipcode' value='" . $row['zipcode'] . "'></td>"; 
      echo "<td><input type='text' name='phone' value='" . $row['phone'] . "'></td>"; 
      echo "<td><input type=hidden name=companies_id value='" . $row['companies_id'] . "'>"; 
      echo "<td><input type='submit'>"; 
      echo "</form></tr>"; 
     } 
    ?> 
    </table> 
</form> 
</body> 
</html> 

,這裏是實際的更新PHP sql查詢不工作或東西的任何投入將有助於擺脫我的頭痛。

<?php 
//Connect to the Database 
$link = mysqli_connect('localhost', '', ''); 
//Select the Database 
mysqli_select_db($link, 'ADDRESS_BOOK_DB'); 

//Update Query 
$sql = "UPDATE Companies SET CompanyName= $_POST[companyname1], Address= $_POST[address], City= $_POST[city], State= $_POST[state], ZipCode= $_POST[zipcode], PhoneNumber= $_POST[phone] 
WHERE companies_id= $_POST[companies_id]"; 

//Execute the Query 
if(mysqli_query($link, $sql)) 
    header("refresh:1; url=edit.php"); 
    enter code here 

else 
    echo "Not Updated"; 
?> 
+1

減少大聲笑,不大膽,更多的描述什麼是實際問題,請。 – tadman

+0

**警告**:使用'mysqli'時,您應該使用[參數化查詢](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)和['bind_param']( http://php.net/manual/en/mysqli-stmt.bind-param.php)將用戶數據添加到您的查詢中。 **不要**使用字符串插值或連接來完成此操作,因爲您創建了嚴重的[SQL注入漏洞](http://bobby-tables.com/)。 **絕不**將'$ _POST'或'$ _GET'數據直接放入查詢中,如果有人試圖利用您的錯誤,這會非常有害。 – tadman

+0

開始和結束時的3個刻度只在我提交給主要開發人員時才放在那裏。所以在錯誤發生之後,3個滴答聲在那裏。我忘了擺脫他們。 – BTDennis

回答

1

首先把這裏的條件與WHERE條款$sql = "SELECT * FROM Companies";這裏面記錄從數據庫中你要更新。好像你是取裏面form

其次所有從數據庫中的記錄,你已經在while循環的begening關閉了form標籤,並保持輸入字段的其餘部分沒有form

更改此

while($row = mysqli_fetch_array($records)) { 
     echo "<tr><form action=update.php method='post'></form>"; 
     echo "<td><input type='text' name='CompanyName' value='" . $row['companyname1'] . "'></td>"; 
     echo "<td><input type='text' name='address' value='" . $row['address'] . "'></td>"; 
     echo "<td><input type='text' name='city' value='" . $row['city'] . "'></td>"; 
     echo "<td><input type='text' name='state' value='" . $row['state'] . "'></td>"; 
     echo "<td><input type='text' name='zipcode' value='" . $row['zipcode'] . "'></td>"; 
     echo "<td><input type='text' name='phone' value='" . $row['phone'] . "'></td>"; 
     echo "<td><input type=hidden name=companies_id value='" . $row['companies_id'] . "'>"; 
     echo "<td><input type='submit'>"; 
     echo "</form></tr>"; 
    } 

?> 
<form action=update.php method='post'> 
<?php 
while($row = mysqli_fetch_array($records)) { 

     echo "<td><input type='text' name='CompanyName' value='" . $row['companyname1'] . "'></td>"; 
     echo "<td><input type='text' name='address' value='" . $row['address'] . "'></td>"; 
     echo "<td><input type='text' name='city' value='" . $row['city'] . "'></td>"; 
     echo "<td><input type='text' name='state' value='" . $row['state'] . "'></td>"; 
     echo "<td><input type='text' name='zipcode' value='" . $row['zipcode'] . "'></td>"; 
     echo "<td><input type='text' name='phone' value='" . $row['phone'] . "'></td>"; 
     echo "<td><input type=hidden name=companies_id value='" . $row['companies_id'] . "'>"; 
     echo "<td><input type='submit'>"; 

    } 

?> 
</form>