普通网友 2019-02-21 10:06
浏览 61

请帮助,初学者在php努力将数据库连接到我的项目[重复]

This question already has an answer here:

I'm working on a login system and I keep getting this error

"The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error. If you think this is a server error, please contact the webmaster."

when I execute the code below:

<?php

if(isset($POST['submit'])){

    include_once 'dbh.php';

    $first = mysqli_real_escape_string($conn, $POST)['first'];
    $last = mysqli_real_escape_string($conn, $POST)['last'];
    $email = mysqli_real_escape_string($conn, $POST)['email'];
    $uid = mysqli_real_escape_string($conn, $POST)['uid'];
    $pwd = mysqli_real_escape_string($conn, $POST)['pwd'];

    //Error handlers
    //Check for empty fields

    if(empty($first)||(empty($last)||(empty($email)||(empty($uid)||(empty($pwd)){
        header("signup.php?signup=empty");
        exit();     
    }else {
        //Check if input characters are valid
        if(!preg_match("/^[a-zA-Z]*$/", $first)||(!preg_match("/^[a-zA-Z]*$/", $last)){
            header("Location: ../signup.php?signup=invalid");
        }exit();    
        }else{
            //Check if email is valid
            if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
                header("Location: ../signup.php?signup=email");
                exit();
            else{
                $sql = "SELECT * FROM users where user_uid='$uid'";
                $result = mysqli_query($conn, $sql);
                $resultCheck = mysqli_num_rows($result);

                if ($resultCheck > 0) {
                    header("Location: ../signup.php?signup=usertaken");
                    exit();
                else {
                    //hashing the password
                    $hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);

                    //Insert the user into the database
                    $sql = "INSERT INTO users (user_first, user_last, user_email, user_uid, user_pwd) 
                                VALUES('$first', '$last','$email','$uid','$hashedPwd');";
                    mysqli_query($conn, $sql);
                    header("Location: ../signup.php?signup=success");
                    exit();
                }
            }       
        }
    }
} else {
    header("Location: ../signup.php");
    exit();
}
}   

the below error message comes up also when I execute just the php script

Parse error: syntax error, unexpected ';' in C:\xampp\htdocs\signup.php on line 17

</div>
  • 写回答

3条回答 默认 最新

  • doumowu7371 2019-02-21 10:24
    关注

    You have several errors here:

    • Your if statement when you check for empty variables have extra ( at the begining
    • Your if else have some missing brackets so you need to be careful when you create them. I would suggest to use an IDE like PhpStorm or text editor like Sublime Text with PHPSyntaxChecker extention

    I have made some fixes to your code so it doesn't have syntax problems but you must check the logic and I would suggest to start using Prepared Statements for mysql queries to add more security to you application.

        if(isset($POST['submit'])){
    
            include_once 'dbh.php';
    
            $first = mysqli_real_escape_string($conn, $POST)['first'];
            $last = mysqli_real_escape_string($conn, $POST)['last'];
            $email = mysqli_real_escape_string($conn, $POST)['email'];
            $uid = mysqli_real_escape_string($conn, $POST)['uid'];
            $pwd = mysqli_real_escape_string($conn, $POST)['pwd'];
    
            //Error handlers
            //Check for empty fields
    
            if(empty($first) || empty($last) || empty($email) || empty($uid) || empty($pwd)){
                header("signup.php?signup=empty");
                exit();
            }else {
                //Check if input characters are valid
                if(!preg_match("/^[a-zA-Z]*$/", $first)|| !preg_match("/^[a-zA-Z]*$/", $last)){
                    header("Location: ../signup.php?signup=invalid");
                    exit();
                }else{
                    //Check if email is valid
                    if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
                        header("Location: ../signup.php?signup=email");
                        exit();
                    }else{
                        $sql = "SELECT * FROM users where user_uid='$uid'";
                        $result = mysqli_query($conn, $sql);
                        $resultCheck = mysqli_num_rows($result);
    
                        if ($resultCheck > 0) {
                            header("Location: ../signup.php?signup=usertaken");
                            exit();
                        }else {
                            //hashing the password
                            $hashedPwd = password_hash($pwd, PASSWORD_DEFAULT);
    
                        //Insert the user into the database
                            $sql = "INSERT INTO users (user_first, user_last, user_email, user_uid, user_pwd) VALUES(
                            '$first', '$last','$email','$uid','$hashedPwd');";
                            mysqli_query($conn, $sql);
                            header("Location: ../signup.php?signup=success");
                            exit();
                        }
                    }    
                }
            }
        } else {
            header("Location: ../signup.php");
            exit();
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度