2

我只是无法理解为什么行没有被删除!请注意,我在 php 页面中获得了更正复选框的登录值。从我的角度来看,很可能错误应该出现在我使用“DELETE FROM”查询的 php 页面中。

<?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>

//现在删除customers.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';
    }
  }
?>
4

2 回答 2

3

不!不!为什么人们一直使用 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();
于 2012-11-10T06:02:44.677 回答
2

代替

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

To

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

并尝试

于 2012-11-10T06:17:44.347 回答