1

如果数据库的查询或更新成功与否,我想提醒用户..

添加用户.php

<?php
include('sqlconnection.php');

$firstname = $_POST['fname'];
$lastname = $_POST['lname'];
$middlename = $_POST['mname'];
$password = $_POST['pword'];
$username = $_POST['uname'];
$gender = $_POST['gender'];
$utype = $_POST['utype'];

$query = "INSERT INTO user (firstname,lastname,middlename,gender) VALUES ('$firstname','$lastname','$middlename','$gender')";   

mysql_query($query);

$result = mysql_query("SELECT id FROM user WHERE firstname = '$firstname'");

$row = mysql_fetch_assoc($result);

mysql_query("INSERT INTO accounts (u_id,username,password,utype) VALUES ('$row["id"]','$username',md5('$password'),$utype);")
?>

我将如何提醒用户?或者我应该用什么来给用户一个弹出消息,说它成功了?

4

3 回答 3

1

检查受影响的行数,参考http://php.net/manual/en/function.mysql-affected-rows.php

功能也mysql_*被贬低,尝试使用mysqli_*or PDO

于 2013-08-22T02:23:46.250 回答
0

mysql_query()对无效查询返回 false。mysql_num_rows()返回为查询(如选择)返回的记录数。mysql_affected_rows()检查受影响的行。

例如:

$result = mysql_query("your query");

if($result){ 
  //query successful but this doesn't mean it returned anything
  //you can now use mysql_num_rows (for select query) to check if something is returned
  if( mysql_num_rows($result) > 0 ){
   //some results were returned
  }

  //for insert, update or delete query you need to check affected rows like this
  if( mysql_affected_rows() > 0 ){
   //some records were updated or deleted
  }
}else {
  echo "There was a problem";
}

旁注:MySQL_*扩展自 PHP 5.5.0 起已弃用,将来将被删除。使用MySQLiPDO代替。

于 2013-08-22T03:53:53.620 回答
0

为 mysql_query 分配一个变量,例如:

 $result = mysql_query("your query");

 if($result){
 $xxx = "query successful";
 }else{
 $xxx = "query failed";
 }

在 hmtl 中:

 <script>
 alert(<?php echo $xxx;?>);
 </script>

这是一种方式,从这里您可以随心所欲地玩


作为旁注:Mysql_* 扩展自 PHP 5.5.0 起已弃用,并将在未来删除。相反,应该使用 MySQLi 或 PDO_MySQL 扩展。

一个有用的链接为什么我不应该在 PHP 中使用 mysql_* 函数

于 2013-08-22T02:23:58.387 回答