数据未插入数据库

时间:2015-01-11 22:52:32

标签: php mysql

我的代码似乎运行正常(我没有得到任何错误)但INSERT INTO查询似乎没有工作,因为数据永远不会被放入数据库。

以下是代码:

编辑:我稍微编辑了代码,因此它具有逻辑意义,但它仍然没有将数据添加到表中。 (我甚至完全删除了if语句,只是将查询留在了,并没有添加它。)

<?php

    //connect to user database
    include("db_connect.php");

    //set variables
    $firstName = $_POST['firstName'];
    $lastName = $_POST['lastName'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $gender = $_POST['gender'];
    $date = date('Y/m/d H:i:s a');

    //check if email exists
    $db_query = "SELECT * FROM users WHERE email LIKE '$email'";
    $db_result = mysql_query($db_query);
    if(!$db_result)
    {
        $query = "INSERT INTO users (lastName, firstName, email, password, gender, signup) VALUES ('$lastName', '$firstName', '$email', '$password', '$gender', '$date')";
        mysql_query($query);
        echo 'You have been successfully registered. Please <a href="login.php">Click Here</a> to log in.';
    }
    else {
        echo 'That email is already in use. <a href="login.php">Click Here</a> to return to the sign up page.';
    }

?>

2 个答案:

答案 0 :(得分:1)

您需要替换

if($email_taken)

if(mysql_num_rows($email_taken))

答案 1 :(得分:1)

我会说它更像是:

   //check if email exists
$db_query = "SELECT * FROM users WHERE email='{$email}'";
$res = mysql_query($db_query);
$email_taken = mysql_num_rows($res);

if($email_taken == 1)
{
    echo 'That email is already in use. <a href="login.php">Click Here</a> to return to the sign up page.';
}
else {
    $query = "INSERT INTO users (lastName, firstName, email, password, gender, signup) VALUES ('$lastName', '$firstName', '$email', '$password', '$gender', '$date')";
    mysql_query($query);
    echo 'You have been successfully registered. Please <a href="login.php">Click Here</a> to log in.';
}