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>

//NOW 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';
    }
  }
?>
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 に答える