douzhao9608 2017-06-23 07:24
浏览 11

“if”正在运行或“else”正在运行

I want to make a user authentication script. Here if the user wants to sign up, he/she will have to fill the registration form and click "Sign up"

PHP

<?php 
$usrnm=$_POST["userName"];
$email=$_POST["mailID"];
$pwd=$_POST["Password"];
$firstName=$_POST["firstName"];
$lastName=$_POST["lastName"];
$confpwd=$_POST["ConfirmPassword"];
 if ($pwd == $confpwd)
{
if (!$con = @mysql_connect("localhost", "root","","login"))
{
echo "connection unsuccessful
";
}
if (!$selectdb = mysql_select_db("login",$con))
{
echo "database selection unsuccessful
";
}
$sql = "SELECT userName FROM userdetails WHERE userName='$usrnm'";
$sql2 = "INSERT INTO userdetails (userName, Password,mailID, firstName, 
lastName) VALUES ('$usrnm','$pwd','$email','$firstName','$lastName')";
$retval = mysql_query( $sql, $con );
while($row = mysql_fetch_row($retval))
{

If the number of fields is more than 0, this means that the Username is already present in the database and ELSE add the information the database. My problem is, the ELSE condition is not working and IF is working. I even tried using ISSET but still no luck.

$fields=mysql_num_fields($retval);
if ($fields>0)
{echo "Username already exists";}
else
{$retval2 = mysql_query( $sql2, $con );
echo "Information added";
}
}
}
}
else
{
echo "Opps...";
}
mysql_close($con);
?>
  • 写回答

2条回答 默认 最新

  • dongzhi9032 2017-06-23 07:33
    关注

    You had 1 extra curly bracket close to your else statement.

    Here's a cleaned up version of your code:

    if ($pwd == $confpwd){
        if (!$con = @mysql_connect("localhost", "root","","login")){
            echo "connection unsuccessful
    ";
        }
        if (!$selectdb = mysql_select_db("login",$con)){
            echo "database selection unsuccessful
    ";
        }
        $sql = "SELECT userName FROM userdetails WHERE userName='$usrnm'";
        $sql2 = "INSERT INTO userdetails (userName, Password,mailID, firstName, lastName) VALUES ('$usrnm','$pwd','$email','$firstName','$lastName')";
        $retval = mysql_query( $sql, $con );
        while($row = mysql_fetch_row($retval)){
            $fields=mysql_num_fields($retval);
    
            if ($fields>0){
                echo "Username already exists";
            } else {
                $retval2 = mysql_query( $sql2, $con );
                echo "Information added";
            }
        }
    } else {
        echo "Opps...";
    }
    mysql_close($con);
    

    I haven't tested it nor do I want to do that, because you seriously need to use either mysqli() or PDO() and you should definitely validate the user inputs.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c