存在的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
$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方法与OO MySQLi界面一起使用应该会有所帮助。还将die();
语句添加到各行中,以查看代码到达的位置以进行调试。
我认为添加语句是否成功执行MySQL中的检查是明智的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句