0

How would I add a message that says "$user_id Deleted" or "$user_id not found?"

  <?php

  $con=mysql_connect("localhost","root","");

  if(!$con) {
      die('could not connect:'.mysql_error());
  }

  mysql_select_db("final?orgdocs", $con);

  $user_id = $_POST["user_id"];

  $result = mysql_query("delete from account where user_id='$user_id' ");


  ?>
PleaseStand
  • 31,641
  • 6
  • 68
  • 95
ranlo
  • 19
  • 4

5 Answers5

1
$user_id = $_POST["user_id"];

if(isset($user_id)) {
    $result = mysql_query("delete from account where user_id='$user_id' ");

    $affected_rows = mysql_affected_rows();    // how many rows deleted?

} else {
    $user_id = "";
    $result = false;
    $affected_rows = 0;
}

if($result == true && $affected_rows > 0) {
    echo "User " . $user_id . " deleted."; 
} else {
    echo "User " . $user_id . " not found.";
}

This should help get you started. You can return the response to your calling page and then use a JavaScript library like JQuery to display it in your HTML.

EDIT: I edited the code to get the affected rows as it's possible for a delete query to return true but delete 0 records.

http://www.php.net/manual/en/function.mysql-affected-rows.php

jamesmortensen
  • 33,636
  • 11
  • 99
  • 120
  • This is not true...As if user has submitted wrong user id then also this will not work...It will not show that user id not found... you have to count affected rows and by that result only u can set message. – Kishan Gajjar Jan 02 '11 at 05:26
  • I made the changes above to count the affected rows. Thanks for pointing that out. – jamesmortensen Jan 02 '11 at 06:49
1
<?php

$con = mysql_connect("localhost", "root", "");

if (!$con) {
    die('could not connect:'  .mysql_error());
}

mysql_select_db("final?orgdocs", $con);

$user_id = (int)$_POST["user_id"];

$result = mysql_query("delete from account where user_id=$user_id");

$affected_rows = mysql_affected_rows();

if ($affected_rows) {
    echo "User ID '$user_id' deleted";
} else {
    echo "User ID '$user_id' not found";
}

?>
thirtydot
  • 224,678
  • 48
  • 389
  • 349
0

echo $user_id+' deleted or not found';

Kishan Gajjar
  • 1,120
  • 3
  • 22
  • 43
0

just add this to the end:

return "user id " . ( $deleted ? "deleted" : "not found");
  • I don't see a "$deleted" variable in his code example. Does $deleted do something related to MySQL that I might not be aware of? – jamesmortensen Jan 02 '11 at 05:08
  • yup i don't have $deleted variable in my code does it do anything? – ranlo Jan 02 '11 at 05:18
  • I think $deleted should be $result. `echo ($result ? "deleted":"not found")` would echo "deleted" if $result is true, and "not found" if false. A short version of what jmort did above. – Phoenix Jan 02 '11 at 05:18
0

From the mysql_query man page:

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

So, it should continue to something like this:

if ($result === TRUE) {
    echo "Account deleted.";
} else if ($result === FALSE) {
    echo "Account not found.";
}
esqew
  • 42,425
  • 27
  • 92
  • 132
  • it just shows account deleted automatically, it doesn't change even when the user id is not found – ranlo Jan 02 '11 at 05:17
  • Try === instead of == to test for literal boolean true or false, rather than just whether or not $result exists; Also, since it is literally a boolean choice, it either succeeded or failed, the else if can just be an else, as if $result !== true, then the only other option is that $result === false. – Phoenix Jan 02 '11 at 05:36
  • Updated because of my typo with only two "="'s. – esqew Jan 02 '11 at 13:55