无法通过php,HTML删除行

时间:2012-11-10 06:00:06

标签: php html

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

<?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';
    }
  }
?>

2 个答案:

答案 0 :(得分:3)

NO!没有!为什么人们继续使用mysql_query().....(head desk)

请查看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();

答案 1 :(得分:2)

替换

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

To

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

并尝试