2013-04-22 66 views
-2

我正在調整我在http://www.phpeasystep.com/mysql/10.html找到的一個示例,以更好地滿足我的需求。這是一個結果大表,可以一次編輯而不是編輯單個記錄。我修改了原文以允許傳遞搜索字詞以將表格內容限制爲特定條件。循環更新語法

我將不勝感激一些幫助找到我的錯字。在最初的查詢中,$ sql,我不得不改變引號一點,所以它可以與我的變量$位置。更新一些行後,我點擊提交,然後只看到表頭和提交按鈕。沒有內容,也沒有更新數據庫。最終更新$ sql1與第一個更新非常相似,所以我不確定它爲什麼不起作用。我試圖弄清楚這個例子中的循環結構是否有問題。

<?php 

$host="localhost"; // Host name 
$username="*****"; // Mysql username 
$password="*****"; // Mysql password 
$db_name="Inventory"; // Database name 

// Connect to server and select databse. 
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 

$location=$_POST['search']; 

echo $location; 
$sql = 'SELECT * FROM `Items` WHERE `Location` = "'.$location.'"'; 

$result=mysql_query($sql); 

// Count table rows 
$count=mysql_num_rows($result); 
?> 

<table width="500" border="0" cellspacing="1" cellpadding="0"> 
<form name="form1" method="post" action=""> 
<tr> 
<td> 
<table width="500" border="0" cellspacing="1" cellpadding="0"> 

<tr> 
<td align="center"><strong>Id</strong></td> 
<td align="center"><strong>Name</strong></td> 
<td align="center"><strong>Present Condition</strong></td> 
<td align="center"><strong>Color</strong></td> 
</tr> 

<?php 
while($rows=mysql_fetch_array($result)){ 
?> 

<tr> 
<td align="center"> 
<?php $id[]=$rows['ItemNumber']; ?> <?php echo $rows['ItemNumber']; ?> 
</td> 
<td align="center"> 
<input name="ItemName[]" type="varchar" id="ItemName" value="<?php echo $rows['ItemName']; ?>"> 
</td> 
<td align="center"> 
<select name="ItemCondition[]" id="ItemCondition"> 
    <option value="">Select...</option> 
    <option value="Excellent">Excellent !</option> 
    <option value="Good">Good</option> 
    <option value="OK">OK</option> 
    <option value="Poor">Below Average</option> 
    <option value="Change">Replace</option> 
    </select> 

</td> 
<td align="center"> 
<input name="ItemColor[]" type="varchar" id="ItemColor" value="<?php echo $rows['ItemColor']; ?>"> 

</td> 
</tr> 

<?php 
} 
?> 

<tr> 
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td> 
</tr> 
</table> 
</td> 
</tr> 
</form> 
</table> 

<?php 

// Check if button name "Submit" is active, do this 
if($Submit){ 
    for($i=0;$i<$count;$i++){ 
    $sql1="UPDATE `Items` SET ItemName='$ItemName[$i]', ItemCondition='$ItemCondition[$i]', ItemColor='$ItemColor[$i]' WHERE ItemNumber='$id[$i]'"; 

    $result1=mysql_query($sql1); 
    echo $i; 
    } 
} 

if($result1){ 
header("location:inventory.php"); 
} 
mysql_close(); 
?> 

表格很簡單。 ItemNumber(鍵),ItemName,ItemCondition,ItemColor。目前它查詢項目列表,正確顯示每個項目的值,但無法更新。我很難過。

百萬分先謝謝。

+5

[**請不要在新代碼中使用'mysql_ *'函數**](http://bit.ly/phpmsql)。他們不再被維護[並且被正式棄用](http://j.mp/XqV7Lp)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。 – Kermit 2013-04-22 21:49:07

+0

你從哪裏得到'$ ItemName','$ ItemCondition'和'$ ItemColor'數組?除了最終更新查詢之外,我無法在任何地方看到它們? – 2013-04-22 22:05:25

回答

0

我注意到你的代碼有幾個問題,併爲你重寫了一些代碼。希望評論/代碼可以解釋它,但隨時可以提出任何問題!

這沒有經過測試,所以我提前道歉任何錯別字。

<?php 
$host = "127.0.0.1"; 
$username = "*****"; 
$password = "*****"; 
$db_name = "Inventory"; 

// When possible, you should use object-based SQL statements vs procedural 
$sql = new mysqli($host, $username, $password, $db_name); 

if($sql->connect_error) { 
    die($sql->connect_error); 
} 


// This should really be checked with isset() 
// It should also be sanitized to prevent SQL injections 
$location = $_POST['search']; // This field should be sanitized to prevent SQL injections 

$get_items = $sql->query(" 
    SELECT * 
     FROM `Items` 
    WHERE `Location` = '".$location."' 
"); 

// count 
$count = $get_items->num_rows; 

// NOTE: Notice that the Submit logic comes before some of the other logic 
// This is because of the header() statement. You can't change location if you've already outputted code 
// One method around this is to buffer, but in this case, it's easier to just shift the code 
if(isset($_POST['Submit'])) { 
    for($i = 0; $i < $count; $i++) { 
     $sql->query(" 
      UPDATE `Items` 
       SET `ItemName`  = '".$_POST['ItemName'][$i]."', 
        `ItemCondition` = '".$_POST['ItemCondition'][$i]."', 
        `ItemColor`  = '".$_POST['ItemColor'][$i]."' 
      WHERE `ItemNumber` = '".$_POST['id'][$i]."' 
     "); 
    } 

    $sql->close(); // Close the connection on page-leave 
    header("Location: inventory.php"); // Remember: You can't use this method to redirect if you've already displayed any content 
    exit; 
} 
?> 
<form name="form1" method="post"> 
    <table width="500" border="0" cellspacing="1" cellpadding="0"> 
     <thead> 
      <tr> 
       <th>ID</th> 
       <th>Name</th> 
       <th>Present Condition</th> 
       <th>Color</th> 
      </tr> 
     </thead> 
     <tbody> 
     <?php 
     while($row = $get_items->fetch_assoc()) { 
     ?> 
     <tr> 
      <td align="center"> 
       <?php echo $row['ItemNumber']; ?> 
       <input type="hidden" name="id[]" value="<?php echo $row['ItemNumber']; ?>"> 
      </td> 
      <td align="center"> 
       <input name="ItemName[]" type="text" class="ItemName" value="<?php echo $row['ItemName']; ?>"> 
       <!-- This input type was "varchar" which isn't valid HTML --> 
       <!-- Also, keep in mind that an ID should only be used once. Classes can be used multiple times, however. --> 
      </td> 
      <td align="center"> 
       <select name="ItemCondition[]" id="ItemCondition"> 
        <option value="">Select...</option> 
        <option value="Excellent">Excellent !</option> 
        <option value="Good">Good</option> 
        <option value="OK">OK</option> 
        <option value="Poor">Below Average</option> 
        <option value="Change">Replace</option> 
       </select> 
      </td> 
      <td align="center"> 
       <input name="ItemColor[]" type="text" class="ItemColor" value="<?php echo $row['ItemColor']; ?>"> 
      </td> 
     </tr> 
     <?php 
     } 
     ?> 
     <tr> 
      <td colspan="4" align="center"> 
       <input type="submit" name="Submit" value="Submit"> 
      </td> 
     </tr> 
     </tbody> 
    </table> 
</form> 
<?php 
$sql->close(); // Close the connection when done executing queries 
?> 
+0

另請注意,您可以通過編寫像'...'' – Uze 2013-04-22 22:32:45

+0

我想了解更多關於您的評論的信息「如果可能,您應該使用基於對象的SQL語句與過程式」,您能否提供有關任一方法的優點/缺點的參考,或者是否只是個人喜好? – 2013-04-22 22:34:44

+0

謝謝:明天當我在辦公室時,我正在檢查這個。我想我沒有足夠的安全問題經驗,但幸運的是,這是完全內部的,而不是面向互聯網。我找到了一個例子,並盡我所能修改它以供我使用。從我鏈接的例子來看,佈局和邏輯是直接的。我現在看到你的解釋背後的邏輯和原因。謝謝 – kkjensen 2013-04-23 02:29:15