[英]How to delete a specific rows in mySQL in a page, within a php function
I have a headache on how to delete a specific rows in my database from a function in a page. 我对如何从页面中的函数删除数据库中的特定行感到头疼。 This is where I am stuck : I know I must use the specific ID from a row, so I put my $id in a while loop, and here I am stuck.
这就是我被困住的地方:我知道我必须使用一行中的特定ID,所以我把我的$ id放在一个while循环中,在这里我被卡住了。 Because I don't know how to retrieve that ID from a specific row.
因为我不知道如何从特定行检索该ID。 IN each row though, there's a "submit" button to delete it.
但是在每行中都有一个“提交”按钮来删除它。 Someone can help me out??
有人可以帮帮我吗?
In my other page when I call these 2 functions : 在我的另一页中,当我调用这两个函数时:
<?php
session_start();
include_once('connect.php');
$object = new User;
if(isset($_POST['supprimer'])) {
$object->supprimer($id);
}
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="public/css/layout.css">
</head>
<body>
<?php $object->showConcessionnaire(); ?>
</body>
</html>
PHP PHP
public function showConcessionnaire(){
$st = $this->db->prepare("SELECT * FROM `phplogin`.`users` WHERE type='Concessionnaire'");
$st->execute();
echo '<form action="supprConc.php" method="post">
<table class="tableCredit">
<tr class="">
<th>Date</th>
<th>Nom d\'utilisateur</th>
<th>Mot de pass</th>
<th>E-mail</th>
<th>Créé par</th>
<th></th>
<th></th>
</tr>
';
while($r = $st->fetch(PDO::FETCH_ASSOC)){
$id = $r['ID'];
echo '<tr class="lightGray">
<td>'.$r['Date'].'</td>
<td>'.$r['username'].'</td>
<td>'.$r['password'].'</td>
<td>'.$r['E-mail'].'</td>
<td>'.$r['Creer par'].'</td>
<td></td>
<td><input type="submit" name="supprimer" value="X" class="logout"></td>
</tr>';
}
echo '</table></form>';
}
public function supprimer($id){
$st = $this->db->prepare("DELETE FROM `phplogin`.`users` WHERE `users`.`id` = '$id'");
$st->execute();
}
One solution would be to insert a hidden input containing the ID of the row. 一种解决方案是插入包含行ID的隐藏输入。
<input type="hidden" name="rowID" value="$id" />
Then when calling your function... 然后在调用你的函数时......
$object->supprimer($_POST['rowID']);
!! ! Make sure you escape your string to avoid SQL Injection!
确保转义字符串以避免SQL注入!
Change this: 改变这个:
<td><input type="submit" name="supprimer" value="X" class="logout"></td>
to this: 对此:
<td><input type="submit" name="supprimer" value=".$id." class="logout"></td>
and then change your function call to: 然后将您的函数调用更改为:
$object = new User;
if(isset($_POST['supprimer'])) {
$id = $_POST['supprimer'];
$object->supprimer($id);
}
This is not entirely secure solution but more like a proof of concept to demonstrate the approach. 这不是完全安全的解决方案,而更像是证明该方法的概念证明。
You can add hidden input field with your id: 您可以使用您的ID添加隐藏的输入字段:
<input type="hidden" name="id" value="'.$id.'">
of course this is on your echo
and you should move form to your <td>
当然这是你的
echo
,你应该将表格移动到你的<td>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.