繁体   English   中英

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

[英]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.";
}

这应该有助于您入门。 您可以将响应返回到调用页面,然后使用JavaScript库(例如JQuery)将其显示在HTML中。

编辑:我编辑了代码以获取受影响的行,因为删除查询可能返回true,但删除0条记录。

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");

mysql_query手册页:

对于SELECT,SHOW,DESCRIBE,EXPLAIN和其他返回结果集的语句,mysql_query()在成功时返回资源,如果出错则返回FALSE。

对于其他类型的SQL语句,INSERT,UPDATE,DELETE,DROP等,mysql_query()在成功时返回TRUE,在出错时返回FALSE。

因此,它应该继续这样:

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