MySQL插入查询返回false

时间:2016-10-21 10:48:07

标签: php html mysql sql

这可能是这里提出的最常见问题。我做了一个简单的用户注册表。这些值未插入数据库。我使用echo,查询返回false。该表格之前运作良好但后来我将名称分为名字和姓氏,并删除了上一个表格并制作了新表格。这是注册页面代码:

    <!DOCTYPE html>
    <?php 
        session_start();
        include('includes/connect.php');
    ?>

<html>
<head>
    <?php
        if(isset($_POST['register_ok'])){
                global $con;

                $user_first_name = $_POST['user_first_name'];
                $user_last_name = $_POST['user_last_name'];
                $email = $_POST['email'];
                $password = $_POST['password'];
                echo "<script type='text/javascript'>alert(\"$user_first_name\");</script>"; //This echo is returning username successfully!

                $query = "insert into user(user_first_name, user_last_name, user_email, password) values('$user_first_name', '$user_last_name', '$email', '$password')";

                if(mysqli_query($con, $query)){
                        $_SESSION['user'] = 'user';
                        $_SESSION['user_first_name'] = $user_first_name;
                        echo "header('Location:index.php')";

                } else {
                    echo "<script type='text/javascript'>alert(\"Values not inserted\");</script>"; //This is running which means query is not successfull.
                }
        } else {
            echo "<script type='text/javascript'>alert(\"Page didn't receive post values\");</script>";
        }
    ?>

    <link rel="stylesheet" type="text/css" href="styles/register_style.css">

    <title>New User Registration</title>
</head>
<body>
    <div class="wrapper">
        <header></header>
        <div class="form_div">
            <div class="form">
                <form id="register_form" method="post" action="" autocomplete="autocomplete">
                    <table>
                        <tr>
                            <td id="label">First Name: </td>
                            <td id="input"><input type="text" name="user_first_name" required="required" id="input_box"></td>
                        </tr>
                        <tr>
                            <td id="label">Last Name: </td>
                            <td id="input"><input type="text" name="user_last_name" required="required" id="input_box"></td>
                        </tr>
                        <tr>
                            <td id="label">Email: </td>
                            <td id="input"><input type="text" name="email" required="required" id="input_box"></td>
                        </tr>
                        <tr>
                            <td id="label">Password: </td>
                            <td id="input"><input type="password" name="password" id="input_box"></td>
                        </tr>
                        <tr>
                            <td id="label">Confirm Password: </td>
                            <td id="input"><input type="password" name="confirm_password" id="input_box"></td>
                        </tr>
                        <tr id="button_row">
                            <td colspan="2"><input type="reset" value="Reset" id="button">
                            <input type="submit" value="Register" id="button" name="register_ok"></td>
                        </tr>
                    </table>
                </form>
            </div>
        </div>
    </div>
</body>
</html>

这是表:Table Structure 该表为空,第一个警报返回用户名,第二个警报在查询返回false时运行。它返回假。 我认为这可能是一个错字但不能缩小范围。欢迎任何帮助。

4 个答案:

答案 0 :(得分:3)

将列类型从int更改为varchar。我正在谈论字符串列(名称和电子邮件)。 Mysql 可以选择检查您要插入的数据类型,如果它们不匹配则会失败。

答案 1 :(得分:1)

首先,

使用此查询将列名从INT更改为VARCHAR

"ALTER TABLE `user`
      MODIFY COLUMN `user_first_name` VARCHAR(225),
      MODIFY COLUMN `user_last_name` VARCHAR(225),
      MODIFY COLUMN `user_email` VARCHAR(225),
      MODIFY COLUMN `password` VARCHAR(225);";

其次,

id&amp; input在每一行中都相同。 <td>不能相同。

将其更改为。

ID

答案 2 :(得分:0)

您需要了解sql注入以及保护密码,使用mysqli预处理语句/ PDO您会发现哪些内容易于学习

 <!DOCTYPE html>
    <?php 
        session_start();
        include('includes/connect.php');
    ?>

<html>
<head>
    <?php
        if(isset($_POST['register_ok'])){

                $user_first_name = $_POST['user_first_name'];
                $user_last_name = $_POST['user_last_name'];
                $email = $_POST['email'];
                $password = $_POST['password'];

                // Lets encrypt the password;

                $hash = pasword_hash($password,PASSWORD_DEFAULT);


                // lets insert then

                $query = $con->prepare("INSERT INTO user (user_id,user_first_name, user_last_name, user_email, password) VALUES(?,?,?,?)");
                $query->bind_param("ssss", $user_first_name, $user_last_name, $email,$hash);
                $query->execute();

               if ($query->execute()) { 

                    echo "<script type='text/javascript'>alert(\"$user_first_name\");</script>"; //This echo is returning username successfully!
                } else {
                    echo "<script type='text/javascript'>alert(\"Values not inserted\");</script>"; //This is running which means query is not successfull.
                } 




        }

    ?>

    <link rel="stylesheet" type="text/css" href="styles/register_style.css">

    <title>New User Registration</title>
</head>
<body>
    <div class="wrapper">
        <header></header>
        <div class="form_div">
            <div class="form">
                <form id="register_form" method="post" action="" autocomplete="autocomplete">
                    <table>
                        <tr>
                            <td id="label">First Name: </td>
                            <td id="input"><input type="text" name="user_first_name" required="required" id="input_box"></td>
                        </tr>
                        <tr>
                            <td id="label">Last Name: </td>
                            <td id="input"><input type="text" name="user_last_name" required="required" id="input_box"></td>
                        </tr>
                        <tr>
                            <td id="label">Email: </td>
                            <td id="input"><input type="text" name="email" required="required" id="input_box"></td>
                        </tr>
                        <tr>
                            <td id="label">Password: </td>
                            <td id="input"><input type="password" name="password" id="input_box"></td>
                        </tr>
                        <tr>
                            <td id="label">Confirm Password: </td>
                            <td id="input"><input type="password" name="confirm_password" id="input_box"></td>
                        </tr>
                        <tr id="button_row">
                            <td colspan="2"><input type="reset" value="Reset" id="button">
                            <input type="submit" value="Register" id="button" name="register_ok"></td>
                        </tr>
                    </table>
                </form>
            </div>
        </div>
    </div>
</body>
</html>

注意:在插入之前,您需要检查用户名是否不存在。

答案 3 :(得分:0)

就我而言,严格模式设置导致了问题。它要求在 INSERT 命令中指定所有列的值。禁用严格模式设置后一切正常。

检查严格模式(这应该是空白):

SHOW VARIABLES LIKE 'sql_mode';

关闭严格模式:

SET GLOBAL sql_mode = '';

开启严格模式:

SET GLOBAL sql_mode = 'STRICT_TRANS_TABLES';

信用:this post