简体   繁体   English

PHP删除查询无法正常工作,甚至没有收到任何错误消息

[英]PHP Delete Query is Not working and even not getting any error message

I want to delete my Users from mst_user table. 我想从mst_user表中删除我的用户。 but delete query isn't working it show the message that user deleted but not deleted from database. 但是删除查询不起作用,它会显示用户已删除但未从数据库中删除的消息。
This is Users.php 这是Users.php

       include("database.php");
       $rs = mysql_query("select * from  mst_user ORDER BY user_id ASC")
       or die(mysql_error());

       echo "<h1 align=center>Users Detail</h1>";

       <table border="1" align="center" class='table'>
        <tr>
        <th align="center"> ID Number </th>
        <th align="center"> Email </th>
        <th align="center"> Username </th>
        <th align="center"> Delete User</th>         
        </tr>

        <?php
        while ($row = mysql_fetch_array($rs)) {
        ?>
        <tr>

            <td align=center> <?php echo $row['user_id']?> </td>
            <td align=center> <?php echo $row['email']?> </td>
            <td align=center> <?php echo $row['username']?> </td>                
            <td aling=center"><a href="delete.php?user_id=<? echo 
            $row['user_id'];?>"><img src="image1/delete.png"></a> </td></tr>
            <?php
              }
           echo "</table>";
           }
          ?>

And this is Delete.php 这是Delete.php

              $user_id=$_GET['user_id'];
              include "database.php";

              $sql="delete from mst_user where user_id='$user_id'";
              $result="mysql_query($sql)" or die("error");

              if($result){
              echo "<h3>User has been Deleted</h3>";
               }
              else{ 
              echo "not delete";
              }
              ?>

This line is the problem: 这行是问题所在:

$result="mysql_query($sql)" or die("error");

should be 应该

$result=mysql_query($sql) or die("error");

You have quotes around it making it a string. 您在它周围有引号使它成为字符串。

Also like other people have said, mysql_* is deprecated, use PDO or MySQLi instead. 还像其他人所说的那样,不建议使用mysql_ *,而是改用PDO或MySQLi。

A PDO example and solution to your problem 一个PDO示例和您的问题的解决方案

PDO PDO

To update your code even further: PDO is a safer way to do queries. 要进一步更新代码,请执行以下操作:PDO是一种更安全的查询方式。 mysql queries are deprecated. 不建议使用mysql查询。 They shouldn't be used anymore. 他们不应该再使用了。

Replace your database calls with the following code: 用以下代码替换数据库调用:

function openDBConnection()
{
    $name = "xxxxxx";
    $pw = "xxxxxx";
    $server = "xxxxxxx";

        $dbConn = new PDO("mysql:host=$server;dbname=xxx", $name, $pw, , array( PDO::ATTR_PERSISTENT => false));
    }
    catch( PDOException $Exception ) 
    {   
         echo "Unable to connect to database. ";
    }       
    return $dbConn;
}
function doPDOQuery($sql, $type, $var = array())
{
    $db = openDBConnection();
    $db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    if ($type == "prepare")
    {
        $queryArray = $var;
        $sth = $db->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
        $sth->execute($queryArray);
    }
    else if ($type == "query")
    {
        $sth = $db->query($sql);
    }
    else
    {
        echo "Supplied type is not valid.";
        exit;
    }

    if (!$sth)
    {
        $error = $db->errorInfo();
        echo $error;
        exit;
    }

    return $sth;
}

These functions you can use to make PDO queries to the database. 您可以使用这些功能对数据库进行PDO查询。 The first function opens a database connection, while the second functions actually performs the query. 第一个功能打开数据库连接,而第二个功能实际执行查询。 You do not need to call the first function. 您不需要调用第一个函数。 It's called in the second one. 在第二个中称为。

Example based upon your code: 根据您的代码的示例:

$sql = "delete from mst_user where user_id= :id"
$sql_result = doPDOQuery($sql, 'prepare', array(":id" => $user_id));

if ($row = $sql_result->rowCount() > 0 )
{
    echo "<h3>user was deleted.</h3>";
}

PDO works as follows: instead of passing php variables into the SQL string (and risking SQL-injection), PDO passes the SQL string and variables to the database and let's the database's driver build the query string. PDO的工作方式如下:PDO不会将php变量传递到SQL字符串中(并冒着SQL注入的风险),而是将SQL字符串和变量传递给数据库,让我们由数据库的驱动程序构建查询字符串。

PDO variables can be declared by name or by index: PDO变量可以通过名称或索引来声明:

  • By name: use : to declare a named variable. 按名称:使用:声明一个命名变量。 SELECT * FROM TABLE WHERE id = :id . SELECT * FROM TABLE WHERE id = :id Each key must be unique. 每个密钥必须唯一。
  • By index: use ? 按索引:使用? to declare an indexed variable. 声明一个索引变量。 SELECT * FROM TABLE WHERE id = ?

An array containing the variables needs to be passed to PDO. 包含变量的数组需要传递给PDO。

named array: 命名数组:

 array(":id" => 1);

indexed array: 索引数组:

 array(1);

With named arrays you don't have to worry about the order of the variables. 使用命名数组,您不必担心变量的顺序。

Results are retrieved with the fetch or fetchAll functions. 使用fetchfetchAll函数检索结果。 fetch returns a row. fetch返回一行。 While fetchAll returns an array containing all rows. fetchAll返回包含所有行的数组。

http://php.net/manual/en/book.pdo.php http://php.net/manual/en/book.pdo.php

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

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