Canit插入我的MYSQL表

时间:2016-07-22 22:26:59

标签: php mysql

我正在尝试创建一个已连接到数据库的注册表单,它还可以检查用户名是否唯一,但遗憾的是,我无法在表格中插入新数据。 如果有人能帮助我,我真的很感激。

<?php
error_reporting(E_ALL ^ E_DEPRECATED);
include 'connect.inc.php'; 
if(isset($_POST['submit'])) {
    $username = mysql_real_escape_string($_POST['username']);
    $password = mysql_real_escape_string($_POST['password']); 
    $password2 = mysql_real_escape_string($_POST['password2']);       
    $firstname = mysql_real_escape_string($_POST['firstname']);
    $lastname = mysql_real_escape_string($_POST['lastname']);

    //md5 password
    $password_hash = md5($password);

    //check to see if the fields are empty
    if(empty($username) || empty($password)|| empty($firstname)|| empty($lastname)) {
        echo "Not all fields filled!<br /><br />";
        exit();
    }

    //check if password is equal

    if($password != $password2) {
        echo "Your Passwords Do Not Match.<br />";
        exit();
    } else { 
        $query = "SELECT `username` From `users` WHERE username='$username'"; 
        $result = mysql_query($query);

        if(mysql_num_rows($result) ==1) { 
            echo "Sorry, that user has already exists.";
            exit();
        } else {
            $query1= mysql_query("INSERT INTO `users` ('',username,password,firstname,lastname) VALUES ('','$username',     '$password_hash', '$firstname', '$lastname'");
            if($result1 = mysql_query($query1)) {
                echo "Registered Successfully";
            } else {
                echo "Sorry, You could not Register";           
            }
        }
    } 
} 

?>
<form action="" method="POST">
    Username:<br />
    <input type="text" name="username" /><br /><br />        

    Password:<br />
   <input type="password" name="password" /><br /><br />

    Confirm Password:<br />
    <input type="password" name="password2" /><br /><br />

    First Name:<br />
    <input type="text" name="firstname" /><br /><br />

    Last Name:<br />
    <input type="text" name="lastname" /><br /><br />

    <input type="submit" value="Register" name="submit" />
</form>

4 个答案:

答案 0 :(得分:2)

您的INSERT语句缺少右括号。

$query1= mysql_query("INSERT INTO ... '$lastname'");

$query1= mysql_query("INSERT INTO ... '$lastname')");
                                                 ^

顺便说一句,我发现在单行INSERT中使用替代语法时更容易,因此匹配列名和值:

$query1= mysql_query("INSERT INTO `users` SET
    username='$username',
    password='$password',
    firstname='$firstname',
    lastname='$lastname'");

更容易确保列与正确的变量匹配。此外,还没有令人担心的右括号。

有关此语法的详细信息,请参阅http://dev.mysql.com/doc/refman/5.7/en/insert.html

您还应该放弃已弃用的mysql扩展,而是使用PDO。阅读这个很好的教程:https://phpdelusions.net/pdo

Jay Blanchard说你的代码不安全是正确的。 安全性,如正确性,不是附加功能。你提到你是初学者,但你不应该开始养成坏习惯。阅读https://blog.codinghorror.com/youre-probably-storing-passwords-incorrectly/

答案 1 :(得分:0)

您可能希望在数据库中插入用户ID。将其定义为Autoincrement e从以下查询中删除空白数据:

<强>之前:

$query1= mysql_query("INSERT INTO `users` ('',username,password,firstname,lastname) VALUES ('','$username',     '$password_hash', '$firstname', '$lastname'");

<强>后:

$query1= mysql_query("INSERT INTO `users` (username,password,firstname,lastname) VALUES ('$username',     '$password_hash', '$firstname', '$lastname')") or die(mysql_error());

您还需要使用if($result1 = mysql_query($query1)) {

替换代码if($result1) {

答案 2 :(得分:0)

尝试使用

$query1= mysql_query("INSERT INTO users (username,password,firstname,lastname) VALUES ('$username', '$password_hash', '$firstname', '$lastname'");

答案 3 :(得分:0)

替换你的else块
else {
    die('Error: ' . mysql_error());
    //echo "Sorry, You could not Register";
}

根据您的评论,您的INSERT QUERY错误。要找出SQL查询的错误,请使用die添加var_export($query1, true)。即。

die('Error: ' . mysql_error().'<br>Info: '.var_export($query1, true));

我的猜测是,您仍在使用旧的查询,其中包含&#39;&#39;作为列名之一。