无法在mysql数据库中插入数据

时间:2014-12-18 13:54:57

标签: php mysql

我无法完成注册,导致数据未插入数据库。请帮我 任何帮助都会有所帮助。非常感谢你

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


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 '<table><th><center><h2>Sign Up</h2></center></th><tr><td><form method="post" action="">
    <input type="text" name="user_idnum" placeholder = "ID Number"/><br><br>
    <input type="text" name="user_lname" placeholder = "Lastname"/><br><br>
    <input type="text" name="user_fname" placeholder = "Firstname"/><br><br>
    <input type="password" name="user_pass" placeholder = "Password"><br><br>
    <input type="password" name="user_pass_check" placeholder = "Re-enter Password"><br><br>
    <input type="email" name="user_email" placeholder = "Email"><br><br>
    <input type="submit" value="Sign Up" />
 </form></td></tr></table>';

}
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_idnum']))
{
if(strlen($_POST['user_idnum']) > 10)
    {
        $errors[] = 'The ID number cannot be longer than 10 characters.';
    }


}
else
{
    $errors[] = 'The ID number 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 'Please input correct information...';
    header ("refresh:3;url=signup.php" );
}
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_pass, user_email ,user_date, user_level, user_lname, user_fname,   user_idnum)
            VALUES('" . mysql_real_escape_string($_POST['user_idnum']) . "',
                   '" . sha1($_POST['user_pass']) . "',
                   '" . mysql_real_escape_string($_POST['user_email']) . "',
                    NOW(),
                    0)";

    $result = mysqli_query($con, $sql);
    if(!$result)
    {
        //something went wrong, display the error
        echo 'Something went wrong while registering. Please try again later.';
        header ("refresh:3;url=signup.php" );

    }
    else
    {
        echo 'Successfully registered. You can now <a href="signin.php">Sign in</a>';
    }
}
}


?>

我不知道出了什么问题,在点击“注册”按钮后,我所能看到的总是“#34;注册时出错了。请稍后再试。&#34; 请帮助我,我需要这个来完成我的项目。三江源

3 个答案:

答案 0 :(得分:2)

更改

$result = mysqli_query($con, $sql);

$result = mysql_query($sql, $con);

或将mysql更改为mysqli,然后检查您是否已安装或启用了mysqli扩展程序。

检查http://php.net/manual/en/mysqli.installation.php

答案 1 :(得分:1)

我已经修复它并且能够在数据库中插入,一个问题是,即使我在其中输入了一个值,lname和fname在数据库中也是完全空白的。

"INSERT INTO users(user_pass, user_email ,user_date, user_level, user_lname, user_fname, user_idnum)
            VALUES('" . sha1($_POST['user_pass']) . "',
                   '" . mysql_real_escape_string($_POST['user_email']) . "',
                    NOW(),0,'','','" . mysql_real_escape_string($_POST['user_idnum']) . "')";

答案 2 :(得分:0)

所以这是你的查询

$sql = "INSERT INTO
    users(user_pass, user_email ,user_date, user_level, user_lname, user_fname,   user_idnum)
            VALUES('" . mysql_real_escape_string($_POST['user_idnum']) . "',
                   '" . sha1($_POST['user_pass']) . "',
                   '" . mysql_real_escape_string($_POST['user_email']) . "',
                    NOW(),
                    0)";

所以你要告诉你想要将数据放入

的mysql列
user_pass, 
user_email ,
user_date, 
user_level, 
user_lname, 
user_fname,   
user_idnum

您要插入的数据

 VALUES(
'" . mysql_real_escape_string($_POST['user_idnum']) . "',
'" . sha1($_POST['user_pass']) . "',
'" . mysql_real_escape_string($_POST['user_email']) . "',
NOW(),
0)

所以我的SQL会像

一样使用
user_pass = '" . mysql_real_escape_string($_POST['user_idnum']) . "', 
user_email = '" . sha1($_POST['user_pass']) . "', ,
user_date = '" . mysql_real_escape_string($_POST['user_email']) . "', 
user_level = NOW(), 
user_lname = 0, 
user_fname,   // ERROR NO VALUE DEFINED
user_idnum    // ERROR NO VALUE DEFINED

所以你告诉我的sql你要将数据插入2而没有他们的数据,这样你就错了

您的列和插入值的顺序错误。

您的查询应该是

$sql = "INSERT INTO users (user_idnum, user_pass, user_email, user_date, user_level) 
VALUES(
'" . mysql_real_escape_string($_POST['user_idnum']) . "', 
'" . sha1($_POST['user_pass']) . "', 
'" . mysql_real_escape_string($_POST['user_email']) . "',
NOW(),
0
)";