0

ここに画像の説明を入力

データベース エントリを受け取るテーブルを html で作成しました。今、エントリを編集および削除できるようにしたいと考えています。Xボタンをクリックすると、選択した行を削除する必要があります。データベースエントリに応じてテーブルが変化するように、SQLクエリでこれを行う必要があることはわかっています。しかし、特定の削除ボタンに属する行を知る必要があるため、どうすればよいでしょうか?

<div id="customers">
<table id="customerTable">
<tr>
<td>First Name</td>
<td>Last Name</td>
<td>Company</td>
<td>Adress</td>
<td>Wijzig</td>
<td>Verwijder</td>
</tr>
<?php
    //connect to database
    include_once('mysql_connect.php');

    // Select database
    mysql_select_db("etn207") or die(mysql_error());

    // SQL query
    $strSQL = "SELECT * FROM customer";

    // Execute the query (the recordset $rs contains the result)
    $rs = mysql_query($strSQL);
    // Loop the recordset $rs
    // Each row will be made into an array ($row) using mysql_fetch_array

        while($row = mysql_fetch_array($rs)) {

       // Write the value of the column FirstName (which is now in the array $row
       echo '<tr>'; 
       echo '<td>'."<center>".$row['firstname']."<br>"."</center>".'</td>'; 
       echo '<td>'."<center>".$row['lastname']."<br>"."</center>".'</td>'; 
        echo '<td>'."<center>".$row['company']."<br>"."</center>".'</td>';
        echo '<td>'."<center>".$row['adress']."<br>"."</center>".'</td>';  
        echo '<td>'."<center>".'<img src="images/edit.png" width="20px" height="20px" border=0>'."<br>"."</center>".'</td>';   
        echo '<td>'."<center>".'<img src="images/delete.png" onClick="" width="20px" height="20px" border=0>'."<br>"."</center>".'</td>';    
        echo '</tr>'; 
      }



    // Close the database connection
    mysql_close();

?>
</table>
</div>
4

2 に答える 2

2

id を非表示の値として渡し、それを使用してその特定の行を削除します

<input type="hidden" name="id_to_be_deleted" value="<?php echo $id; ?>" />

送信時にこのIDを取得し、データベースから行を削除するだけです

<form method="post">
   <input type="hidden" name="id_to_be_deleted" value="<?php echo $id; ?>" />
   <input type="submit" name="delete_row" />
</form>

<?php
if(isset($_POST['delete_row'])) {
   $id = $_POST['id_to_be_deleted'];
   if(!mysqli_query($connection, "DELETE FROM table_name WHERE id = $id")) {
     echo mysqli_error($connection);
   } else {
      //redirect $_SERVER['REQUEST_URI'];
   }
}
?>
于 2013-03-19T15:37:09.783 に答える
1

データベースレコードには、おそらく一意の識別子(主キーなど)があります。削除ボタンにリンクを作成するときは、この一意の識別子を使用してください。

  echo '<a href="deleterecord.php?id=' . $yourUniqueidentifier . '"><img src="images/delete.png"></a>'

「center」タグではなくCSSを使用する、AJAXを使用して完全なポストバックなしでレコードを削除する、クエリ文字列にハッシュを使用してレコードの不正な削除を回避するなど、コードには他のアドバイスも使用する必要がありますが、それはここでのポイントではありません:-)

于 2013-03-19T15:48:19.590 に答える