MySQL 数据库未选中。我错过它在哪里?

标签: html5 MySQL PHP
发布时间: 2016/10/13 21:11:00
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

搜索后通过相关的问题,我还是不能解决这个问题。"注册成功"的网页应该到流行起来后提交表单,但是相反,会出现"没有选定的数据库"消息。我是在哪里想念它的。这里是代码。

connect.php

<?php
//connect.php
$server = 'localhost';
$username   = 'root';
$password   = '';
$database   = 'esiro';
$connection = mysqli_connect($server, $username, $password, $database);
    mysqli_set_charset($connection,"utf8");

?>

signup.php

<?php
//signup.php
include 'connect.php';
include 'header.php';

echo '<h3>Sign up</h3>';

if($_SERVER['REQUEST_METHOD'] != 'POST')
{
    /*the form hasn't been posted yet, display it
      note that the action="" will cause the form to post to the same page it is on */
    echo 

    '<form role="form" method="post" action="" class="cover_form">
    <div class="form-group">
    <label class="labelfield" for="username">User Name:</label><br>
    <input class="inputfield" type="text" name="user_name" class="form-control"/><br>

    <label class="labelfield" for="pwd">Password:</label><br>
    <input class="inputfield" type="password" class="form-control" id="pwd" name="user_pass"><br>
    <label class="labelfield" for="pwd"> Confirm Password:</label><br>
    <input class="inputfield" type="password" name="user_pass_check" class="form-control" id="pwd"><br>

    <label class="labelfield" for="email">Email Address:</label><br>
    <input  class="inputfield"type="email" class="form-control" id="email" name="user_email">
  </div><br>

  <input type="submit" class="btn btn-default" value="Complete Registration"/><br>
</form>
';
}
else
{
    /* so, the form has been posted, we'll process the data in three steps:
        1.  Check the data
        2.  Let the user refill the wrong fields (if necessary)
        3.  Save the data 
    */
    $errors = array(); /* declare the array for later use */

    if(isset($_POST['user_name']))
    {
        //the user name exists
        if(!ctype_alnum($_POST['user_name']))
        {
            $errors[] = 'The username can only contain letters and digits.';
        }
        if(strlen($_POST['user_name']) > 30)
        {
            $errors[] = 'The username cannot be longer than 30 characters.';
        }
    }
    else
    {
        $errors[] = 'The username field must not be empty.';
    }


    if(isset($_POST['user_pass']))
    {
        if($_POST['user_pass'] != $_POST['user_pass_check'])
        {
            $errors[] = 'The two passwords did not match.';
        }
    }
    else
    {
        $errors[] = 'The password field cannot be empty.';
    }

    if(!empty($errors)) /*check for an empty array, if there are errors, they're in this array (note the ! operator)*/
    {
        echo 'Uh-oh.. a couple of fields are not filled in correctly...';
        echo '<ul>';
        foreach($errors as $key => $value) /* walk through the array so all the errors get displayed */
        {
            echo '<li>' . $value . '</li>'; /* this generates a nice error list */
        }
        echo '</ul>';
    }

         else
    {


        //the form has been posted without, so save it
        //notice the use of mysql_real_escape_string, keep everything safe!
        //also notice the sha1 function which hashes the password
        $sql = "INSERT INTO
                    users(user_name, user_pass, user_email ,user_date, user_level)
                VALUES('" . mysql_real_escape_string($_POST['user_name']) . "',
                       '" . sha1($_POST['user_pass']) . "',
                       '" . mysql_real_escape_string($_POST['user_email']) . "',
                        NOW(),
                        0)";

        $result = mysql_query($sql);
        if(!$result)
        {
            //something went wrong, display the error
            echo 'Something went wrong while registering. Please try again later.';
            echo mysql_error(); //debugging purposes, uncomment when needed
        }
        else
        {
            echo 'Successfully registered. You can now <a href="signin.php">sign in</a> and start posting! :-)';
        }
    }
}

include 'footer.php';
?>

解决方法 1:

您应该更改脚本

$result = mysql_query($sql);

Instead of use this
---------------------     
$result = mysqli_query($connection,$sql);

同时还能消除 echo mysql_error(); 并使用此echo mysql_error($connection);

这也在添加,而不是 mysql_real_escape_string

$sql = "INSERT INTO
                users(user_name, user_pass, user_email ,user_date, user_level)
            VALUES('" . mysqli_real_escape_string($connection,$_POST['user_name']) . "',
                   '" . sha1($_POST['user_pass']) . "',
                   '" . mysqli_real_escape_string($connection,$_POST['user_email']) . "',
                    NOW(),
                    0)";
官方微信
官方QQ群
31647020