Reputation: 19
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' ");
?>
Upvotes: 0
Views: 115
Reputation: 44711
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.";
}
Upvotes: 0
Reputation: 34068
$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
Upvotes: 1
Reputation: 228282
<?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";
}
?>
Upvotes: 1
Reputation: 1
just add this to the end:
return "user id " . ( $deleted ? "deleted" : "not found");
Upvotes: 0