0

I have a SQL query to get my id and insert a new id into the database. How am I supposed to alert the user if he has not entered in anything in the textbox before submitting? What should I add?

Here is my code:

if ($_GET['username'])
{
    $result = mysql_query("SELECT * FROM users WHERE user_name = '$_GET[username]'");
    if ($user = mysql_fetch_assoc($result))
    {
        $_SESSION['user_id'] = $user['user_id'];
        $_SESSION['user_name'] = $user['user_name'];
    }
    else
    {
        mysql_query("INSERT INTO  `personality_test`.`users` (`user_name`)VALUES ('$_GET[username]');");
        $_SESSION['user_id'] = mysql_insert_id();
        $_SESSION['user_name'] = $_GET['username'];
    }
}

if ($_SESSION['user_id'])
{
    $result = mysql_query("SELECT * FROM users WHERE user_id = '$_SESSION[user_id]'");
    $row = mysql_fetch_assoc($result);
    $user_result = json_decode($row['user_result'], true);

    if (!empty($user_result))
    {
        header('Location:result.php');
    }
        else
        {
            header('Location:index.php');
        }
}
4

1 回答 1

0

首先:

 if ($user = mysql_fetch_assoc($result))

需要是

 if ($user == mysql_fetch_assoc($result))

在让他们提交之前,我会使用 Javascript 确保文本框不为空。

于 2013-08-20T16:21:44.900 回答