简体   繁体   English

查询不会将用户名插入数据库

[英]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.它不会将用户名插入数据库,但当我回显$_SESSION['user']时,它仍会向我显示其内容,我将不胜感激。

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";包含 $_SESSION['user'] 的值是 theil,它没有任何特殊字符,但是如果我将 $_SESSION['user'] 替换为类似 $user = "test"; 的字符串; it will insert the value "test" into the database它将值“test”插入数据库

mysql_query for insert statements either returns True on success or False on error.用于插入语句的mysql_query要么在成功时返回 True,要么在错误时返回 False。 You have to check the return value if it was successful, and if it wasn't successful get the error via mysql_error :如果成功,您必须检查返回值,如果不成功,则通过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.图像列设置为 NOT NULL,但您没有在其中插入任何内容。 I suspect removing the NOT NULL clause, or setting a default value for the column might fix your problem.我怀疑删除 NOT NULL 子句,或为列设置默认值可能会解决您的问题。

Additional tip.附加提示。 use MYSQLS NOW() for the date.使用 MYSQLS NOW() 作为日期。 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']).'"你也可以试试“'.mysql_real_escape_string($_SESSION['user']).'”

the problem might be special characters.问题可能是特殊字符。

From all the comments try this从所有评论中尝试这个

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

mysql_query('INSERT INTO wall_post (post,username,userip,date_created) VALUES("'..checkValues($_REQUEST['value']).'", "'.$name.'","'$ipAddress'","'.$timestamp.'")'); 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.用户名可能没有保存到数据库中的唯一原因是 `$_SESSION['user'] 为空或不存在。

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?您是否尝试过运行此回显查询 - INSERT INTO wall_post (post, username, userip, date_created) VALUES('', 'theil', '127.0.0.1', '1309975742') - 直接进入 MySQL,在提示符或任何您可能正在使用的其他客户端?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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