将删除按钮添加到 PHP 结果表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17144846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add Delete Button to PHP results table
提问by Lucero79
I have outputted the results of a MySQL table to an HTML table. In the last column, I want to add a delete option which calls another form and deletes the user. I can't seem to get it to work though.
我已将 MySQL 表的结果输出到 HTML 表。在最后一列中,我想添加一个删除选项,它调用另一个表单并删除用户。我似乎无法让它工作。
This is my code for the results page:
这是我的结果页面代码:
<?php
$contacts = mysql_query("
SELECT * FROM contacts ORDER BY ID ASC") or die( mysql_error() );
// If results
if( mysql_num_rows( $contacts ) > 0 )
?>
<table id="contact-list">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Telephone</th>
<th>Address</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php while( $contact = mysql_fetch_array( $contacts ) ) : ?>
<tr>
<td class="contact-name"><?php echo $contact['name']; ?></td>
<td class="contact-email"><?php echo $contact['email']; ?></td>
<td class="contact-telephone"><?php echo $contact['telephone']; ?></td>
<td class="contact-address"><?php echo $contact['address']; ?></td>
<td class="contact-delete"><form action='delete.php' method="post">
<input type="hidden" name="name" value="">
<input type="submit" name="submit" value="Delete">
</form></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
and, this is my delete.php script
而且,这是我的 delete.php 脚本
<?php
//Define the query
$query = "DELETE FROM contacts WHERE name={$_POST['name']} LIMIT 1";
//sends the query to delete the entry
mysql_query ($query);
if (mysql_affected_rows() == 1) {
//if it updated
?>
<strong>Contact Has Been Deleted</strong><br /><br />
<?php
} else {
//if it failed
?>
<strong>Deletion Failed</strong><br /><br />
<?php
}
?>
Pretty sure I'm just missing something, but I can't figure out what that is :(
很确定我只是错过了一些东西,但我不知道那是什么:(
回答by Devang Rathod
You have to pass variable in delete link. You must have to pass <?php echo $contact['name']; ?>
name value in hidden field or pass this value in URL
您必须在删除链接中传递变量。您必须<?php echo $contact['name']; ?>
在隐藏字段中传递名称值或传递此值URL
Replace
代替
<td class="contact-delete">
<form action='delete.php' method="post">
<input type="hidden" name="name" value="">
<input type="submit" name="submit" value="Delete">
</form>
</td>
With
和
<td class="contact-delete">
<form action='delete.php?name="<?php echo $contact['name']; ?>"' method="post">
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
<input type="submit" name="submit" value="Delete">
</form>
</td>
回答by Ammar Hayder Khan
USe javascript
使用 JavaScript
<input name="Submit2" type="button" class="button" onclick="javascript:location.href='delete.php?id=<?php echo $your_id;?>';" value="« Back" />
and in delet.php
并在delet.php
$id=$_GET['id'];
and put $id in your sql statement.
并将 $id 放入您的 sql 语句中。
回答by Sami Dz Hamida
<input type="hidden" name="name" value="">
You are missing a value which wil be picked up by this line in your delete file.
您缺少一个值,该值将由您的删除文件中的这一行获取。
$query = "DELETE FROM contacts WHERE name={$_POST['name']} LIMIT 1";
Right now it isn't receiving anything, which is why it will not work.
现在它没有收到任何东西,这就是它不起作用的原因。
So add a value to it and it will work. Example:
所以给它添加一个值,它就会起作用。例子:
<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
回答by Voitcus
You are missing to pass name in this line:
您缺少在此行中传递名称:
<input type="hidden" name="name" value="">
You need to have something (<?php echo $contact['name']; ?>
) in the value
attribute.
您需要<?php echo $contact['name']; ?>
在value
属性中包含一些东西 ( ) 。
BTW, do not use deprecated mysql_*
functions, use PDOor mysqli_*
instead.
回答by cardeol
At first, you cannot write the code in that way, the code has no protection against SQL injection.
起初,您不能以这种方式编写代码,代码没有针对SQL 注入的保护。
1)Try to use primary ID's instead of using a name (what happens if 2 people has the same name?).
1)尝试使用主 ID 而不是使用名称(如果 2 个人同名会发生什么?)。
So, you can create a hidden field to know what person you are handling with.
因此,您可以创建一个隐藏字段来了解您正在与什么人打交道。
<input type="hidden" name="contact_id" value="<?php $contact['contact_id']; ?>">
2)Sanitize variables to avoid attacks:
2)清理变量以避免攻击:
<?php $contact_id = isset($_POST['contact_id'])?intval($_POST['contact_id']):0;
// proceed with the query
if($contact_id>0) { $query = "DELETE FROM contacts WHERE contact_id = '$contact_id'";
}
// redirect to the main table with header("location: main.php");
?>
回答by Prakash Bora
This is a php delete process script you link this code in your delete button
这是一个 php 删除过程脚本,您可以在删除按钮中链接此代码
<?php
$link=mysql_connect("localhost","root","");
mysql_select_db("photo",$link);
if(isset($_GET["id"])
$photoid=$_GET['id'];
$sql="delete from photos where photoid=".$_GET['id'];
$result=mysql_query($sql,$link);
header("location:home.php?ok");
?>