php变量不更新

时间:2014-12-24 23:14:51

标签: php html mysqli

var存在并不会更新它只是保持初始值。求助。

    $exists = 0;
    $result = $mysqli->query("SELECT username from tblUsers WHERE username = '{$username}'");
    if ($result-> num_rows == 1) {
      $exists = 1;
      $result = $mysqli-> query("SELECT email from tblUsers WHERE email = '{$email}'");
      if ($result-> num_rows == 1) $exists = 2;
    } else {
      $result = $mysqli-> query("SELECT email from tblUsers WHERE email = '{$email}'");
      if ($result-> num_rows == 1) $exists = 3;
    }

     if ($exists == 1) echo "<p>Username already exists!</p>";
    else if ($exists == 2) echo "<p>Username and Email already exists!</p>";
    else if ($exists == 3) echo "<p>Email already exists!</p>";
    else {
     // insert data into mysql database

1 个答案:

答案 0 :(得分:0)

 $exists = 0;
    $result = $mysqli->query("SELECT username from tblUsers WHERE username = '$username'");
    if ($result->num_rows == 1) {
      $exists = 1;
      $result = $mysqli->query("SELECT email from tblUsers WHERE email = '$email'");
      if ($result->num_rows == 1)  {
$exists = 2;
}
    } 
else {
      $result = $mysqli->query("SELECT email from tblUsers WHERE email = '{$email}'");
      if ($result->num_rows == 1) {
$exists = 3;
}
    }

     if ($exists == 1) echo "<p>Username already exists!</p>";
    else if ($exists == 2) echo "<p>Username and Email already exists!</p>";
    else if ($exists == 3) echo "<p>Email already exists!</p>";
    else {
     // insert data into mysql database
}

我为了自己的想法稍微重新编写了这个,但我最初的想法是,对于OO编程,你使用了一种将数据输入数据库的程序方式,这是怎么回事:

   $exists = 0;
        if($object_Var = $mysqli->prepare("SELECT username from tblUsers WHERE username = ?")) {
        $object_Var->bind_param("s",$username);
        $object_Var->execute();
     }
        else {
        ///this will run if the script execution failes for whatever reason
        print "error: ".$mysqli->error;
        } 
  if ($object_Var->num_rows == 1) {
           $exists = 1;
           $object_Var_two = $mysqli->prepare("SELECT email from tblUsers WHERE email = ?");
           $object_Var_two->bind_param("s",$email);
           $object_Var_two->execute();
          if ($object_Var_two->num_rows == 1)  {
                $exists = 2;
            }
        } 
    else {
          $object_Var_three = $mysqli->prepare("SELECT email from tblUsers WHERE email = ?");
          $object_Var_three->bind_param("s",$email);
          $object_Var_three->execute();
          if ($object_Var_three->num_rows == 1) {
               $exists = 3;
            }
        }

         if ($exists == 1) {
            echo "<p>Username already exists!</p>";
         }
        elseif ($exists == 2) {
            echo "<p>Username and Email already exists!</p>";
            }
        elseif ($exists == 3) {
            echo "<p>Email already exists!</p>";
            }
        else {
         // insert data into mysql database
        }

等等。

道歉我没有完成完整的脚本,但我认为使用OO MySQLi接口的OO方法应该会有所帮助。还要在行中添加die();语句,以查看代码到达的位置以进行调试。

我认为如果在MySQL成功执行的状态检查中添加是明智的。