php 显示和删除数据库中的数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19348841/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 19:20:44  来源:igfitidea点击:

Display and Delete data from database

phpmysql

提问by henk.io

I am new to PHP and just wanting to make a basic page where i can see all the users in the database and delete them. I have come this far but it keeps on telling me that I have an i have and Undefined index: user_id and although it tells me that it has deleted the fields it has not deleted anything. Here is my code:

我是 PHP 的新手,只想制作一个基本页面,我可以在其中查看数据库中的所有用户并删除它们。我已经走了这么远,但它一直告诉我我有一个 i have 和 Undefined index: user_id ,虽然它告诉我它已经删除了字段,但它没有删除任何内容。这是我的代码:

<?php include_once "includes/scripts.php"; ?>
<?php include_once "includes/connect.php";?>
<?php include_once "includes/cms_page_security.php";?>
<div id="cms_container"><br>
    <br>
    <h1>MANAGE USERS<img src="images/three_column_grid_line.png" alt="line"></h1>
    <p class="logout_btn"><a href="admin_cms.php">Back</a></p>
<?php
$tbl="users"; // Table name 
$sql = "SELECT * FROM $tbl";
$result = mysql_query($sql, $connect);
while($rows = mysql_fetch_array($result)){
?>
<?php
echo $rows['user_id'];
echo $rows['user_name'];
echo $rows['user_password'];  
?> 
<a href="delete_user.php?id= <? echo $rows['user_id']; ?>">delete</a>
<?php
}
?>
<?php
mysql_close();
?>
</div><!--cms_container-->
</body>
</html>

The page that it should link to that deletes the query:

它应该链接到删除查询的页面:

<?php include_once "includes/connect.php";?>
<?php
    $tbl="users"; 
    $user_id= $_GET ['user_id'];
    $sql="DELETE FROM $tbl WHERE user_id = '$user_id'";
    $result = mysql_query($sql, $connect);
    if($result){
        echo "Deleted Successfully";
        echo "<BR>";
        echo "<a href='delete.php'>Back to main page</a>";
    }else {
        echo "ERROR";
    }
    ?> 
<?php
mysql_close();
?>

采纳答案by T.Coutlakis

In addition to the other answers:

除了其他答案:

It looks like this line could be a fatal error, if php short tags aren't enabled:

如果未启用 php 短标签,这行看起来可能是一个致命错误:

<a href="delete_user.php?id= <? echo $rows['user_id']; ?>">delete</a>

The php manual says:

php手册说:

*PHP also allows for short tags <?and ?>(which are discouraged because they are only available if enabled with short_open_tag php.ini configuration file directive, or if PHP was configured with the--enable-short-tags option.* http://php.net/manual/en/language.basic-syntax.phptags.php

*PHP 还允许使用短标签<??>(不鼓励使用短标签,因为它们仅在使用 short_open_tag php.ini 配置文件指令启用,或者使用 --enable-short-tags 选项配置 PHP 时才可用。* http://php .net/manual/en/language.basic-syntax.phptags.php

回答by Felix

In delete_user.php you must get user_id

在 delete_user.php 你必须得到 user_id

$user_id= $_GET ['id'];

because in your <a href="delete_user.php?id= <? echo $rows['user_id']; ?>">delete</a>link GET variable is "id", not "user_id"

因为在您的<a href="delete_user.php?id= <? echo $rows['user_id']; ?>">delete</a>链接中 GET 变量是“id”,而不是“user_id”

回答by null

You really should be using PDO instead. The issue is in the information that you are passing.

您确实应该改用 PDO。问题在于您传递的信息。

The link : <a href="delete_user.php?id= <? echo $rows['user_id']; ?>">delete</a>is looking for an 'id' but you're later looking for 'user_id'

链接:<a href="delete_user.php?id= <? echo $rows['user_id']; ?>">delete</a>正在寻找“id”,但您稍后会寻找“user_id”

If you change it to <a href="delete_user.php?user_id=<? echo $rows['user_id']; ?>">delete</a>, it should work.

如果您将其更改为<a href="delete_user.php?user_id=<? echo $rows['user_id']; ?>">delete</a>,它应该可以工作。

I still strongly suggest you look into PDO instead though, it's much more secure and easier to work with.

我仍然强烈建议您改用 PDO,它更安全且更易于使用。

Example of PDO Delete

PDO 删除示例

 public function deleteUser($username, $user_id){

    if($this->isAdmin($username) == true){

        $query = $this->db->prepare('DELETE FROM users WHERE user_id = ?');
        $query->bindValue(1, $user_id);

        try{
            $query->execute();
        }catch(PDOException $e){
            die($e->getMessage());
        }
    }else{
        return false;
    }
}

I'm running an extra check to make sure the person who is requesting the deletion is an admin member but you should be able to see the structure

我正在运行额外的检查以确保请求删除的人是管理员成员,但您应该能够看到结构

回答by Quentin

The SQL query will be successful even if it alters zero rows. You are prefixing your user ids with a space when you are generating your HTML (id= <?), so you aren't matching any rows (since "1"won't be matched by " 1").

即使 SQL 查询更改了零行,它也会成功。当您生成 HTML ( id= <?)时,您在用户 ID 前添加了一个空格,因此您不会匹配任何行(因为"1"不会被 匹配" 1")。

回答by Quentin

Where you are creating your 'Delete' link

您在哪里创建“删除”链接

<a href="delete_user.php?id= <? echo $rows['user_id']; ?>">delete</a>

You're creating a variable of 'id', but later you look for 'user_id.

您正在创建一个“id”变量,但稍后您会查找“user_id”。

Change your link to

将您的链接更改为

<a href="delete_user.php?user_id=<? echo $rows['user_id']; ?>">delete</a>