数据未插入XAMPP数据库

时间:2016-04-29 10:03:12

标签: php mysql xampp

注册一个人时遇到问题。我已经尝试登录并且它运行良好。

数据库脚本

CREATE TABLE userinfor (
    userid INT(4) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(25) NOT NULL,
    password VARCHAR(15) NOT NULL,
    email VARCHAR(35) NOT NULL
);

PHP代码

$emailErr = $passMismatch = "";
if( isset($_POST['submit']) ){
    if ( $_SERVER["REQUEST_METHOD"] == "POST" ) {
        $name = $_REQUEST['name'];
        $pwd = $_REQUEST['password'];
        $cpwd = $_REQUEST['cpassword'];
        $email = $_REQUEST['email'];

        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            $emailErr = "ERROR::Invalid E-Mail Format"; 
        } else $emailErr = "";

        if ( $pwd != $cpwd ) {
            $passMismatch = "ERROR::Passwords Don't Match";
        } else $passMismatch = "";

        $host = "localhost";
        $username = "root";
        $password = "";
        $database = "ontheway";

        $conn = mysqli_connect($host, $username, $password, $database);

        if ( empty($emailErr) && empty($passMismatch) ) {
            $sql = "INSERT INTO userinfo (userid, name, password, email)
                    VALUES (NULL, $name', $pwd', '$email')";


            $result = mysqli_query($conn, $sql);
            if ( !$result ) {
                echo "chala nahi query";
            }

            if ( $result ) {
                session_start();
                $_SESSION['name'] = $name;
                $_SESSION['pass'] = $pass;

                header("location: home.php");    
            }
        }
    }
}

我尝试回应问题,但我找不到任何,$ name,$ pwd,$ cpwd,$ email都在回应。甚至错误也完美地显示在表单上。数据库也正在连接。什么似乎是问题?请帮助:)

我总是得到" chala nahi查询"这意味着每次运行时查询sql都不起作用。

表格代码

姓名:

                            <input type="text" name="name" /> <br /> 

                            <p>Password: </p>
                            <input type="password" name="password" /> <br />
                            <?php echo '<h1>'.$passMismatch.'</h1>'; ?>

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

                            <p>Email: </p>
                            <input type="email" name="email" /> <br />
                            <?php echo '<h1>'.$emailErr.'</h1>'; ?>

                            <input type="submit" name="submit" />

2 个答案:

答案 0 :(得分:1)

你在查询中错过了$ name和$ pwrd变量的查询中的单引号 - 它应该是;

$sql = "INSERT INTO userinfo (userid, name, password, email)
                    VALUES (NULL, '$name', '$pwd', '$email')";

您应该调查PDO和绑定参数 - 更安全。另外 - 你应该清理输入,你的页面上应该只有1个H1元素。

答案 1 :(得分:1)

变量周围缺少引号也无需插入userid,因为它已经autoincremented

$sql = "INSERT INTO userinfo (`name`, `password`, `email`)
                    VALUES ('".$name."', '".$pwd."', '".$email."')";