简体   繁体   English

用php删除mysql表中的行

[英]deleting rows in mysql table with php

new to PHP here I have a page that loops through a DB and displays the contents of the table. PHP 新手在这里我有一个页面循环遍历数据库并显示表的内容。 When the delete button is pressed I would like to delete that entry in the DB.当按下删除按钮时,我想删除数据库中的该条目。 Right now when the button is pressed and the page goes to delete.php I keep getting Undefined index: userid现在,当按下按钮并且页面转到 delete.php 时,我不断收到 Undefined index: userid

Here is the first page:这是第一页:

<?php
foreach($rows as $row){
$userid = $row['userid'];
$pName = $row['pName'];
$pDesc = $row['pDesc'];
$dDate = $row['dDate'];
?>

<div class="project-container">

    <label>Project ID:</label>
    <span><?php echo $userid; ?></span><br>
    <label>Project Owner:</label>
    <span><?php echo $pName; ?></span><br>
    <label>Project Description:</label>
    <span><?php echo $pDesc; ?> </span><br>
    <label>Project Due Date:</label>
    <span><?php echo $dDate; ?> </span><br>
    <br>

    <form action="#" method="GET">
        <input type="submit" name="delete" value="Delete Project">
    </form>
    <form action="index.php">
        <input type="submit" name="update" value="Update Project">
    </form>
    </div>
    <br>
    <br>

</div><br><br><?php } ?>

and this is delete.php:这是delete.php:

include('connect.php');
$userid = $_GET['userid'];
echo $userid;
$sql = "DELETE FROM projecttable WHERE userid  = '$userid'";
$conn->exec($sql);

Any help is appreciated, thank you任何帮助表示赞赏,谢谢

Try this.尝试这个。 It will send a variable in the GET array named userid with the userid value to delete.php:它会将名为 userid 的 GET 数组中的变量与 userid 值发送到 delete.php:

<form action="delete.php" method="GET">
    <input type="hidden" name="userid" value="<?php echo $userid; ?>">
    <input type="submit" name="delete" value="Delete Project">
</form>

The action attribute in the form tag will direct where to sent the request which is the delete.php script and the method will tell it to use the GET array which basically puts the keys and values in the URL itself.表单标签中的 action 属性将指示发送请求的位置,即 delete.php 脚本,该方法将告诉它使用 GET 数组,该数组基本上将键和值放在 URL 本身中。 You could alternately do this by modifying the action field instead of using an input field.您也可以通过修改操作字段而不是使用输入字段来执行此操作。 I just like this way for readability.我只是喜欢这种方式以提高可读性。

Because input fields are usually visible, using a hidden field prevents it from being displayed on the web page, but it's easy to manage in the code.因为输入字段通常是可见的,使用隐藏字段可以防止它在网页上显示,但在代码中很容易管理。 The name attribute of the input field determines the name of the key in the GET array and the value attribute determined the value of that element in the GET array.输入字段的 name 属性决定了 GET 数组中键的名称,而 value 属性决定了 GET 数组中该元素的值。 So it ends up being $_GET['name_attribtue'] = value_attribute in your PHP script.所以它最终是 $_GET['name_attributue'] = value_attribute 在你的 PHP 脚本中。

Just for example.举个例子。 If you changed the form method attribute to POST, your PHP script would need to use the $_POST array instead of the $_GET array.如果您将表单方法属性更改为 POST,您的 PHP 脚本将需要使用 $_POST 数组而不是 $_GET 数组。

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

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