插入表不工作但没有错误

时间:2014-04-29 14:03:36

标签: php mysql sql syntax-error

请帮忙,我是php的新手并尝试编写注册页面。没有出现错误,但数据也没有出现在数据库中。所有的验证工作正常,所以我认为插入表时有问题。有人可以帮忙吗?

<?php 

if($_SERVER['REQUEST_METHOD']=='POST')
{
require('connect_db.php');
$errors = array();


if(empty($_POST['firstname']))
    {
    $errors[]='Enter your Firstname';
    }
else 
    {
    $fn = mysqli_real_escape_string($dbc, trim($_POST['firstname']));
    }


if(empty($_POST['surname']))
    {
    $errors[]='Enter your Surname';
    }
else 
    {
    $sn = mysqli_real_escape_string($dbc, trim($_POST['surname']));
    }


if(empty($_POST['email']))
    {
    $errors[]='Enter your email address';
    }
else 
    {
    $email = mysqli_real_escape_string($dbc, trim($_POST['email']));
    }

if(!empty($_POST['pass1']))
    {
        if($_POST['pass1']!= $_POST['pass2'])
            {
                $errors[] = 'Passwords do not match';   
            }
        else
            {
                $p = mysqli_real_escape_string($dbc, trim($_POST['pass1']));
            }
    }
else
    {
        $errors[]="Enter your password";
    }

if (empty($errors))
    {
        $q = "INSERT INTO Users(Username, Firstname, Surname, Password)
            VALUES($email, $fn, $sn,  User, SHA1($p))";
        $r = mysqli_query ($dbc, $q);
        if ($r)
        {
            echo "success";
        }
    }
else 
    {
        echo '<h1>Error</h1><p id="errors">The following error/s occurred:<br/>';
        foreach ($errors as $error)
            {
                echo"$error <br/>";

            }
        echo"Please try again</p>";
        mysqli_close($dbc);
    }

}

1 个答案:

答案 0 :(得分:0)

您的字符串值缺少引号:

INSERT INTO Users(Username, Firstname, Surname, Password)
        VALUES($email, $fn, $sn,  User, SHA1($p))

应该是:

INSERT INTO Users(Username, Firstname, Surname, Password)
        VALUES('$email', '$fn', '$sn',  'User', 'SHA1($p)')

您收到错误的原因是您实际上没有检查任何错误。当您检查$r的值并在其中调用mysqli_error()以查看错误消息时,您需要添加else语句。

if ($r) {
    echo "success";
}
else {
    echo mysqli_error($dbc); exit// this really should be better then this.
}