[英]Add 'delete' button to php results table
I outputted the results of a MySQL table to an HTML table, I'm trying to add a Delete button to remove the user but it doesn't work.我将 MySQL 表的结果输出到 HTML 表,我试图添加一个删除按钮来删除用户,但它不起作用。
HTML form code: HTML 表单代码:
<?php
$response = $bdd->query('SELECT * FROM users');
$i = 1;
while ($datas = $response->fetch()) {
?>
<tr>
<td><?php echo $datas['first_name']; ?></td>
<td><?php echo $datas['last_name']; ?></td>
<td>
<form action="_delete.php?id=<?php echo $datas['id']; ?>" method="post">
<input type="hidden" name="name" value="<?php echo $datas['id'];?>">
<input class="btn btn-danger" type="submit" name="submit" value="X">
</form>
</td>
</tr>
And this is my _delete.php :这是我的 _delete.php :
<?php
try
{
$bdd = new PDO('mysql:host=localhost;dbname=dbname;charset=utf8', 'root', 'root');
}
catch (Exception $e)
{
die('Erreur : ' . $e->getMessage());
}
?>
<?php
$id = (int)$_GET['id'];
$query = "DELETE FROM users WHERE id={$id} LIMIT 1";
//sends the query
mysql_query ($query);
if (mysql_affected_rows() == 1) {
?>
<strong>User Has Been Deleted</strong>
<?php
} else {
?>
<strong>Deletion Failed</strong>
<?php
}
?>
My result url is good /_delete.php?id=13
but Delete script isn't.我的结果 url 很好
/_delete.php?id=13
但删除脚本不是。
I have this error: Deprecated: mysql_query(): The mysql extension is deprecated and will be removed in the future
我有这个错误:
Deprecated: mysql_query(): The mysql extension is deprecated and will be removed in the future
Any idea?任何的想法?
Your messing around with GET and POST params.你搞乱了 GET 和 POST 参数。 You defined a get param named
id
containing your id and a post param named name
containing also your id.您定义了一个名为
id
的 get param 包含您的 id 和一个名为name
的 post param 也包含您的 id。
But currently you are trying to access the get param with $_POST
(which contains only post params).但是目前您正在尝试使用
$_POST
(仅包含 post 参数)访问 get 参数。
To solve your problem, you should use $_GET['id']
or $_POST['name']
.要解决您的问题,您应该使用
$_GET['id']
或$_POST['name']
。
In each way, keep in mind to protect you input from sql injections.在每种方式中,请记住保护您的输入免受 sql 注入。 Currently the user could pass anything else as well.
目前,用户也可以通过其他任何东西。 A simple cast to an int, would be enough.
对 int 的简单转换就足够了。
$id = (int)$_GET['id'];
$query = "DELETE FROM users WHERE id={$id} LIMIT 1";
I have incoporated a few suggestions in my answer, try and see if it works.我在我的回答中加入了一些建议,试试看它是否有效。 Create a connection, then get the ID using $_GET instead of $_POST.
创建一个连接,然后使用 $_GET 而不是 $_POST 获取 ID。
<?php
$con=mysqli_connect("localhost","dbuser","dbpassword","dbname");
if($con==false){
die("ERROR:Could not connect.". mysqli_connect_error());
}
else{
$id=$_GET['id']
$query = "DELETE FROM users WHERE id='$id' LIMIT 1";
//sends the query
mysql_query ($con,$query);
if (mysql_affected_rows() == 1) {
?>
<strong>User Has Been Deleted</strong>
<?php
} else {
?>
<strong>Deletion Failed</strong>
<?php
}
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.