简体   繁体   English

如何添加显示“ $ user_id已删除”或“找不到$ user_id”的消息?

[英]How would I add a message that says “$user_id Deleted” or “$user_id not found?”

How would I add a message that says "$user_id Deleted" or "$user_id not found?" 我如何添加一条消息“$ user_id Deleted”或“$ 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. 您可以将响应返回到调用页面,然后使用JavaScript库(例如JQuery)将其显示在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. 编辑:我编辑了代码以获取受影响的行,因为删除查询可能返回true,但删除0条记录。

http://www.php.net/manual/en/function.mysql-affected-rows.php 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: mysql_query手册页:

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error. 对于SELECT,SHOW,DESCRIBE,EXPLAIN和其他返回结果集的语句,mysql_query()在成功时返回资源,如果出错则返回FALSE。

For other type of SQL statements, INSERT, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error. 对于其他类型的SQL语句,INSERT,UPDATE,DELETE,DROP等,mysql_query()在成功时返回TRUE,在出错时返回FALSE。

So, it should continue to something like this: 因此,它应该继续这样:

if ($result === TRUE) {
    echo "Account deleted.";
} else if ($result === FALSE) {
    echo "Account not found.";
}

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

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