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' ");
?>
$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
<?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";
}
?>
echo $ user_id +'已删除或未找到';
只需将其添加到最后:
return "user id " . ( $deleted ? "deleted" : "not found");
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.";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.