简体   繁体   中英

Query won't insert username into database

So I've got this query:

mysql_query(
    "INSERT INTO  wall_post (post,username,userip,date_created)
    VALUES(
        '".checkValues($_REQUEST['value'])."',
        '".$_SESSION['user']."',
        '".$userip."',
        '".strtotime(date("Y-m-d H:i:s"))."'
    )"
);

and I also tried to make the query this way:

mysql_query(
    "INSERT INTO wall_post (post,username,userip,date_created)
    VALUES(
            '".checkValues($_REQUEST['value'])."',
            $_SESSION['user'],
            '".$userip."',
            '".strtotime(date("Y-m-d H:i:s"))."'
           )"
);

I don't see any error message from the database when the insert fails.

It won't insert the username into the database but when I echo $_SESSION['user'] it would still show me its content, please I would appreciate some help.

The table structure is:

CREATE TABLE wall_post (
    p_id int(11) NOT NULL auto_increment,
    username varchar(50) NOT NULL,
    post varchar(255) NOT NULL,
    image varchar(50) NOT NULL,
    date_created int(11) NOT NULL,
    userip varchar(200) NOT NULL, PRIMARY KEY (p_id)
)

The value which contains $_SESSION['user'] is theil, it doesn't have any special character, but if I replace $_SESSION['user'] with a string like $user = "test"; it will insert the value "test" into the database

mysql_query for insert statements either returns True on success or False on error. You have to check the return value if it was successful, and if it wasn't successful get the error via mysql_error :

$result = mysql_query($sql);
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

It should be easy to fix from there.

The image column is set to NOT NULL, but you are not inserting anything into it. I suspect removing the NOT NULL clause, or setting a default value for the column might fix your problem.

Additional tip. use MYSQLS NOW() for the date. Just let the database handle that bit:)

just check what the value is and make sure there are no special characters in there.

You can also try "'.mysql_real_escape_string($_SESSION['user']).'"

the problem might be special characters.

From all the comments try this

$name = isset($_REQUEST['user'])? $_REQUEST['user']: '';

mysql_query('INSERT INTO wall_post (post,username,userip,date_created) VALUES("'..checkValues($_REQUEST['value']).'", "'.$name.'","'$ipAddress'","'.$timestamp.'")');

From one of your comments above, I learnt that if you echo your query, it shows as

INSERT INTO wall_post (post,username,userip,date_created) 
VALUES('','theil','127.0.0.1','1309975742')

Did you do this echo just before the statement where you run the query? If not, I'd request you to please do the echo just before the call, like this:

echo "INSERT INTO  wall_post (post,username,userip,date_created) VALUES(
    '".checkValues($_REQUEST['value'])."',
    '".$_SESSION['user']."',
    '".$userip."',
    '".strtotime(date("Y-m-d H:i:s"))."')"; 
mysql_query("INSERT INTO  wall_post (post,username,userip,date_created) VALUES(
    '".checkValues($_REQUEST['value'])."',
    '".$_SESSION['user']."',
    '".$userip."',
    '".strtotime(date("Y-m-d H:i:s"))."')"
);

Your query seems to be absolute fine and should run fine. The only reason why username might not be saving into the database is that `$_SESSION['user'] is empty or does not exist.

Did you try running this echoed query - INSERT INTO wall_post (post, username, userip, date_created) VALUES('', 'theil', '127.0.0.1', '1309975742') - directly into MySQL, either on the prompt or any other client that you might be using?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM