将删除按钮添加到php结果表

0aydgbwb  于 2021-10-10  发布在  Java
关注(0)|答案(5)|浏览(294)

我已经将mysql表的结果输出到html表。在最后一列中,我想添加一个delete选项,它调用另一个表单并删除用户。但我似乎无法让它发挥作用。
这是我的结果页面代码:

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

这是我的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
} 
?>

我很确定我错过了什么,但我不知道那是什么:(

dsf9zpds

dsf9zpds1#

您必须在删除链接中传递变量。你必须通过考试 <?php echo $contact['name']; ?> 在隐藏字段中命名值或传入此值 URL 代替

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

具有

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

vsaztqbk2#

使用javascript

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

在delet.php中

$id=$_GET['id'];

并将$id放入sql语句中。

7xzttuei

7xzttuei3#

您在此行中缺少传递名称:

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

你需要一些东西( <?php echo $contact['name']; ?> )在 value 属性
顺便说一句,不要使用不推荐的 mysql_* 函数,使用pdo或 mysqli_* 相反

uqzxnwby

uqzxnwby4#

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

您缺少一个值,该值将由删除文件中的此行提取。

$query = "DELETE FROM contacts WHERE name={$_POST['name']} LIMIT 1";

现在它没有收到任何东西,这就是它无法工作的原因。
因此,给它添加一个值,它就会起作用。例子:

<input type="hidden" name="name" value="<?php echo $contact['name']; ?>">
efzxgjgh

efzxgjgh5#

首先,您不能以这种方式编写代码,因为代码没有针对sql注入的保护。
1) 尝试使用主id,而不是使用名称(如果两个人同名会发生什么情况?)。
因此,您可以创建一个隐藏字段,以了解您正在与谁打交道。

<input type="hidden" name="contact_id" value="<?php $contact['contact_id']; ?>">

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

?>

相关问题