PHP - mysqli_select_db无效

时间:2014-12-19 13:13:54

标签: php mysql mysqli

我正在从MySQL切换到MySQLi,我发现了一个错误,我谷歌无法解决。问题在于数据库的选择(mysqli_select_db)在我使用的转换(mysql_select_db)之前没问题。希望你能帮助我解决这个问题。

更新 - 我的当前代码

        <?php
        $submit = $_POST['submit'];
        //form data
        $name = mysqli_real_escape_string($_POST['name']);
        $name2 = mysqli_real_escape_string($_POST['name2']);
        $email = mysqli_real_escape_string($_POST['email']);
        $password = mysqli_real_escape_string($_POST['password']);
        $password2 = mysqli_real_escape_string($_POST['password2']);
        $phone2 = mysqli_real_escape_string($_POST['phone2']);
        $email2 = mysqli_real_escape_string($_POST['email2']);
        $address = mysqli_real_escape_string($_POST['address']);
        $address2 = mysqli_real_escape_string($_POST['address2']);
        $address3 = mysqli_real_escape_string($_POST['address3']);
        $address4 = mysqli_real_escape_string($_POST['address4']);
        if ($submit) {
            //connect database
            $connect = mysqli_connect("localhost", "root", "Passw0rd", "logindb") or die ("Connection Error");
            //namecheck
            $namecheck = mysqli_query($connect, "SELECT * FROM users WHERE email='{$email}'");
            //check for existance
            if($name&&$name2&&$email&&$password&&$password2&&$phone2&&$email2&&$address&&$address2&&$address3&&$address4) {
                if(strlen($password)<8) {
                    echo "Password must be least 8 characters";
                }
                if(!preg_match("#[A-Z]+#",$password)) {
                    echo "Password must have at least 1 upper case characters";
                }
                if(!preg_match("#[0-9]+#",$password)) {
                    echo "Password must have at least 1 number";
                }
                if(!preg_match("#[\W]+#",$password)) {
                    echo "Password must have at least 1 symbol";
                }
                if($_POST['password'] != $_POST['password']) {
                    echo "Password does not match";
                }
                if($_POST['email2'] == $_POST['email']) {
                    echo "Secondary email must not be the same as Email";
                }
                //encrypt password
                $password = sha1($password);
                $password2 = sha1($password2);
                //generate random code
                $random = rand(11111111,99999999);
                if(isset($error)&&!empty($error)) {
                    implode($error);
                }
                else
                {
                    //register the user
                    $register = mysqli_query($connect, "INSERT INTO users VALUES ('','$name','$name2','$email','$password','$password2','$phone2','$email2','$address','$address2','$address3','$address4','$random','0')");
                    $lastid = mysqli_insert_id($connect);
                    echo "<meta http-equiv='refresh' content='0; url=Activate.php?id=$lastid&code=$random'>";
                    die ();
                }
            }
        }
        ?>

2 个答案:

答案 0 :(得分:2)

connection函数的第一个参数中传递mysqli_select_db()参考,试试这个

mysqli_select_db($connect, "logindb") or die("Selection Error");

以及mysqli_query($connect, "query here")

更新所有mysqli_*个函数都需要连接参考,但结果函数除外

答案 1 :(得分:1)

快速完整的解决方案

   <?php
    $submit = $_POST['submit'];

    //connect database
    $connect = mysqli_connect("localhost", "root", "Passw0rd") or die ("Connection Error");
    //select database
    mysqli_select_db($connect, "logindb") or die("Selection Error");

    //form data
    $name = mysqli_real_escape_string($connect, $_POST['name']);
    $name2 = mysqli_real_escape_string($connect, $_POST['name2']);
    $email = mysqli_real_escape_string($connect, $_POST['email']);
    $password = mysqli_real_escape_string($connect, $_POST['password']);
    $password2 = mysqli_real_escape_string($connect, $_POST['password2']);
    $phone2 = mysqli_real_escape_string($connect, $_POST['phone2']);
    $email2 = mysqli_real_escape_string($connect, $_POST['email2']);
    $address = mysqli_real_escape_string($connect, $_POST['address']);
    $address2 = mysqli_real_escape_string($connect, $_POST['address2']);
    $address3 = mysqli_real_escape_string($connect, $_POST['address3']);
    $address4 = mysqli_real_escape_string($connect, $_POST['address4']);
    if ($submit) {
        //namecheck
        $namecheck = mysqli_query($connect, "SELECT * FROM users WHERE email='{$email}'");
        $count = mysqli_num_rows($namecheck);
        if(strlen($email)<5) {
            echo "Email must have at least 5 characters";
        }
        else
        {
            if($count==0) {
            }
            else
            {
                if($count==1) {
                    echo "User already Exists";
                }
            }
        }
        //check for existance
        if($name&&$name2&&$email&&$password&&$password2&&$phone2&&$email2&&$address&&$address2&&$address3&&$address4) {
            if(strlen($password)<8) {
                echo "Password must be least 8 characters";
            }
            if(!preg_match("#[A-Z]+#",$password)) {
                echo "Password must have at least 1 upper case characters";
            }
            if(!preg_match("#[0-9]+#",$password)) {
                echo "Password must have at least 1 number";
            }
            if(!preg_match("#[\W]+#",$password)) {
                echo "Password must have at least 1 symbol";
            }
            if($_POST['password'] != $_POST['password']) {
                echo "Password does not match";
            }
            if($_POST['email2'] == $_POST['email']) {
                echo "Secondary email must not be the same as Email";
            }
            //encrypt password
            $password = sha1($password);
            $password2 = sha1($password2);
            //generate random code
            $random = rand(11111111,99999999);
            if(isset($error)&&!empty($error)) {
                implode($error);
            }
            else
            {
                //register the user
                $register = mysqli_query($connect, "INSERT INTO users VALUES ('','$name','$name2','$email','$password','$password2','$phone2','$email2','$address','$address2','$address3','$address4','$random','0')");
                $lastid = mysqli_insert_id($connect);
                echo "<meta http-equiv='refresh' content='0; url=Activate.php?id=$lastid&code=$random'>";
                die ();
            }
        }
    }
    ?>

您还必须在第70行$register = mysqli_query($connect, "INSERT INTO users上的表名后面和值VALUES (之前写下字段,如下例所示。

INSERT INTO users(username,userpass,name,email,userrole,joined,userstatus) VALUES('$username','$userpass','$name','$email','$userrole','$joined','$userstatus')

使用以下

替换第70行
$register = mysqli_query($connect, "INSERT INTO users(name,name2,email,password,password2,phone2,email2,address,address2,address3,address4,random,active) VALUES ('$name','$name2','$email','$password','$password2','$phone2','$email2','$address','$address2','$address3','$address4','$random','0')");