简体   繁体   English

"将删除按钮添加到 PHP 结果表"

[英]Add Delete Button to PHP results table

I have outputted the results of a MySQL table to an HTML table.我已将 MySQL 表的结果输出到 HTML 表。 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.我似乎无法让它工作。

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>

You have to pass variable in delete link.您必须在删除链接中传递变量。 You must have to pass <?php echo $contact['name']; ?><\/code>您必须通过<?php echo $contact['name']; ?><\/code> <?php echo $contact['name']; ?><\/code> name value in hidden field or pass this value in URL<\/code> <?php echo $contact['name']; ?><\/code>隐藏字段中的名称值或在URL<\/code>中传递此值

Replace<\/strong>代替<\/strong>

<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>

USe javascript使用 javascript

<input name="Submit2" type="button" class="button" onclick="javascript:location.href='delete.php?id=<?php echo $your_id;?>';" value="&laquo; Back" />

and in delet.php并在 delete.php

$id=$_GET['id'];

and put $id in your sql statement.并将 $id 放入您的 sql 语句中。

You are missing to pass name in this line:您缺少在此行中传递名称:

<input type="hidden" name="name" value="">
<input type="hidden" name="name" value="">

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");

?>

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");
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM