Y2J
Y2J

Reputation: 41

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

I want to delete my Users from mst_user table. but delete query isn't working it show the message that user deleted but not deleted from database.
This is 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

              $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";
              }
              ?>

Upvotes: 0

Views: 5655

Answers (2)

Mouser
Mouser

Reputation: 13304

A PDO example and solution to your problem

PDO

To update your code even further: PDO is a safer way to do queries. mysql queries are deprecated. 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. 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 variables can be declared by name or by index:

  • By name: use : to declare a named variable. 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.

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. fetch returns a row. While fetchAll returns an array containing all rows.

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

Upvotes: 0

David Xu
David Xu

Reputation: 5607

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.

Upvotes: 1

Related Questions