2012-11-10 69 views
2

我只是無法理解爲什麼行不會被刪除! 請注意,我正在獲取PHP頁面中更正複選框的登錄值。 從我的角度來看,最有可能的錯誤應該是在我正在使用 'DELETE FROM'查詢的php頁面中。無法通過php刪除行,HTML

<?php 
    session_start(); 
?> 

<html> 
    <head> 
    <form id="delete_customers" action="deletecustomers.php" method="post"> 
    <?php 
     $con = mysql_connect("localhost","root",""); 
     if (!$con) { 
     die('Could not connect: ' . mysql_error()); 
     } 
     mysql_select_db("car_rental_system", $con); 
     $result = mysql_query("SELECT * FROM customer"); 

     echo "<table border='1' align='center'> 
       <tr> 
       <th>first_name</th> 
       <th>Last_name</th> 
       <th>login</th> 
       </tr>"; 

     while($row = mysql_fetch_array($result)) { 
     echo "<tr>"; 
     echo "<td>" . $row['first_name'] . "</td>"; 
     echo "<td>" . $row['login'] . "</td>"; 

     echo "<td>"."<input type='checkbox' name='deletingcustomers[]' 
        value=$row['login']}"."</td>"; 

     echo "</tr>"; 
     } 
     echo "</table>"; 

     mysql_close($con); 
    ?> 
    <p class='submit'> 
     <button type='submit' name='dscustomer'>Delete selected</button> 
    </p> 
    </head> 
</html> 

//現在deletecustomers.php

<?php 
    session_start(); 
    $_SESSION['deletingcustomers'] = $_POST['deletingcustomers']; 
    $N = count($_SESSION['deletingcustomers']); 
    $con = mysql_connect("localhost","root",""); 
    if (!$con) die('Could not connect: ' . mysql_error()); 
    mysql_select_db("car_rental_system", $con); 

    if(empty($_SESSION['deletingcustomers'])) { 
    echo("No customers selected"); 
    } else { 
    for ($i=0; $i<$N; $i++) { 
     $sql1="delete from `customer` 
       where login='{$_SESSION[deletingcustomers][$i]}'"; 
     if(mysql_query($sql1,$con)) 
     echo 'executed'; 
    } 
    } 
?> 
+5

歡迎堆棧溢出!請不要使用'mysql_ *'函數來編寫新的代碼。他們不再被維護,社區已經開始[棄用流程](http://goo.gl/q0gwD)。看到[紅色框](http://goo.gl/OWwr2)?相反,您應該瞭解[準備好的語句](http://goo.gl/orrj0)並使用[PDO](http://goo.gl/TD3xh)或[MySQLi](http://php.net/ mysqli的)。如果你不能決定哪些,[這篇文章](http://goo.gl/YXyWL)會幫助你。如果你選擇PDO,[這裏是很好的教程](http://goo.gl/b2ATO)。另請參閱[爲什麼不應該在PHP中使用mysql函數?](http://goo.gl/J5jAo) – Daedalus

+0

更不用說您的代碼容易受到注入攻擊。 – Daedalus

+0

另外,PDO非常容易。我想我可能會把它扔到那裏。 – Ben

回答

2

更換

echo "<td>"."<input type='checkbox' name='deletingcustomers[]' value=$row['login']}"."</td>"; 

To 

    echo "<td><input type='checkbox' name='deletingcustomers[]' value='".$row['login']."'</td>"; 

並嘗試

+0

真棒yar:D:D 工作喜歡的魅力,我差點把我的頭昨晚做! 德雷克指出了一些非常好的事情,它會很有趣的閱讀,肯定比W3更先進:) –

+0

抱歉不能投票, 非常新的堆棧溢出,所以沒有足夠的聲譽:/ –

+0

如果它應該接受答案你要走的方式......左邊的複選標記。 – codemonkee

3

NO!沒有!爲什麼人們繼續使用mysql_query().....(總檯)

請查閱PDO。 http://php.net/manual/en/book.pdo.php它有助於防止sql注入,並讓您更好地理解如何利用oop的權力。

$_SESSION[deletingcustomers][$i]需要在它的途中$_SESSION['deletingcustomers'][$i]

$tempVar = $_SESSION['deletingcustomers'][$i]; 
$dbConnection = new PDO("mysql:host=".$hostName.";dbname=".$dbName, $username, $password); 
$sql = "delete from `customer` where login='$tempVar'"; 
$stmt = $newObj->prepare($sql); 
$stmt->execute(); 
+0

這應該是一個評論。這不是一個答案。沒有downvote呢:) – codingbiz

+0

這是因爲這種類型的代碼在搜索結果中非常流行,沒有人真正研究過*正確*做事的方式(或者讀取文檔) – sberry

+0

這也是導致**的原因之一所以**許多糟糕的PHP開發人員在外面。 – sberry