【问题标题】:Form handling php script doesn't work properly表单处理 php 脚本无法正常工作
【发布时间】:2020-01-04 05:11:51
【问题描述】:

我创建了一个 html 注册表单(signup.php)。我将dbf/signup.dbf.php 作为操作页面放在dbf 文件夹中。以下代码属于signup.dbf.php。“dbc.dbf.php”是提供数据库连接的脚本。

<?
if(isset($_POST['createbtn'])){
    require 'dbc.dbf.php';

    $uid=$_POST['uid'];
    $mail=$_POST['email'];
    $pwd=$_POST['pw'];
    $repwd=$_POST['re-pw'];
    $add=$_POST['address'];

    if(empty($uid)||empty($mail)||empty($pwd)||empty($add)||empty($repwd))
    {
        header("Location: ../signup.php?error=emptyfields&uid=".$uid."&email=".$mail);
        exit();
    }

        else if(!filter_var($mail,FILTER_VALIDATE_EMAIL)&&!preg_match("/[a-zA-Z0-9]$/",$uid)){
        header("Location:../signup.php?error=invalidemailuid");
        exit();
    }   


    else if(!filter_var($mail,FILTER_VALIDATE_EMAIL)&&!preg_match("/[a-zA-Z0-9]$/",$uid)){
        header("Location:../signup.php?error=invalidemailuid");
        exit();
    }   

    else if(!filter_var($mail,FILTER_VALIDATE_EMAIL)){
        header("Location:../signup.php?error=inavalidemail&uid=".$uid);
        exit();
    }

    else if(!preg_match("/[a-zA-Z0-9]$/",$uid)){
        header("Location:../signup.php?error=inavalideuid&email=".$mail);
        exit();
    }   

    else if($pwd!=$repwd){
        header("Location:../signup.php?error=passwordcheck&uid=".$uid."&email=".$mail);
        exit();
    }

    else{
        $sql="SELECT uid FROM user_details WHERE uid=?";
        $stmnt=mysqli_stmt_init($conn);
        if(!mysqli_stmt_prepare($stmnt,$sql)){
            header("Location:../signup.php?error=sqlerror");
        exit();
        }
        else{

            mysqli_stmt_bind_param($stmnt,"s",$uid);
            mysqli_stmt_execute($stmnt); 
            mysqli_stmt_store_result($stmnt);
            $results=mysqli_stmt_num_rows($stmnt);
            if($results>0){
                header("Location:../signup.php?error=usertaken&email".$mail);
                exit();
            }
             else{
                 $sql="INSERT INTO user_details(uid,email,password,address) VALUES(?,?,?,?)";
                     $stmnt=mysqli_stmt_init($conn);
                 if(!mysqli_stmt_prepare($stmnt,$sql)){
            header("Location:../signup.php?error=sqlerror");
                    exit();
                     }
                 else{
                     $hashpwd=password_hash($pwd,PASSWORD_DEFAULT);
                     mysqli_stmt_bind_param($stmnt,"ssss",$uid,$mail,$hashpwd,$add);
            mysqli_stmt_execute($stmnt);
               header("Location:../index.php");
                    exit();
                 }
             }



        }

    }       

    mysqli_stmt_close($stmnt);
            mysqli_close($conn);


}

            else{
        header("Location:../signup.php");
             }

但是当我单击注册表单的createbtn 时,它既不会将数据保存在数据库中,也不会将页面重定向到signup.php。它总是卡在 signup.dbf.php 显示以下代码。

0){
                header("Location:../signup.php?error=usertaken&email".$mail);
                exit();
            }
             else{
                 $sql="INSERT INTO user_details(uid,email,password,address) VALUES(?,?,?,?)";
                     $stmnt=mysqli_stmt_init($conn);
                 if(!mysqli_stmt_prepare($stmnt,$sql)){
            header("Location:../signup.php?error=sqlerror");
                    exit();
                     }
                 else{
                     $hashpwd=password_hash($pwd,PASSWORD_DEFAULT);
                     mysqli_stmt_bind_param($stmnt,"ssss",$uid,$mail,$hashpwd,$add);
            mysqli_stmt_execute($stmnt);
               header("Location:../index.php");
                    exit();
                 }
             }



        }

    }       

    mysqli_stmt_close($stmnt);
            mysqli_close($conn);


}else{header("Location:../signup.php");
exit();}

即使我评论了这段代码,它仍然会显示。谁能帮我解决这个问题

【问题讨论】:

    标签: php database mysqli


    【解决方案1】:

    您的 PHP 代码是否打印在浏览器输出中? 显然,当您没有将代码正确放入 PHP 标记中时,就会发生这种情况。您在代码上使用了简短的 PHP 标记 (&lt;?),这需要 php.ini 上的 short_open_tag 的许可。确保已设置此设置或使用默认 PHP 标记。

    【讨论】:

    • 是的,有效!!!实际上我没有激活 php.ini 。看到你的答案后我改变了
    猜你喜欢
    • 2017-05-23
    • 1970-01-01
    • 2015-01-27
    • 2013-03-06
    • 2014-11-05
    • 1970-01-01
    • 1970-01-01
    • 2018-02-04
    • 1970-01-01
    相关资源
    最近更新 更多