简体   繁体   English

MySQL数据未插入数据库

[英]MySQL data not inserting into database

I am working on a notification script for my site. 我正在为我的网站制作通知脚本。 I can't get the info to insert into the DB. 我无法获取要插入数据库的信息。 Everything else using these functions worked, except the notification insert. 除了通知插入外,使用这些功能的所有其他操作均有效。 It's sending the notification to the creator of a thread on my forum script, so I put a query where it grabs the data from the forums. 它会将通知发送到论坛脚本上线程的创建者,因此我在其获取来自论坛数据的地方进行了查询。 query() is a predefined function and $id was already set up in the script. query()是预定义的函数,并且$ id已在脚本中设置。

Here's my code: 这是我的代码:

// Notifications
$eee = "SELECT * FROM forums WHERE id = $id";
$eeee = query($eee);
while($notif = mysqli_fetch_assoc($eeee)){
$query3 = "INSERT INTO notifications (username, purpose, url, from) VALUES ('$notif[creator]', 'commented on your thread', '/forums/view_topic.php?id=$id', '$_SESSION[username]')";
$active3 = mysqli_query(db(), $query3);
}

I tried the "or die(mysqli_error(db)), but it just died and returned nothing. I would appreciate any help thanks. 我尝试了“或die(mysqli_error(db)),但它死了,没有返回任何内容。感谢您的帮助,我将不胜感激。

You need to put the backticks around the colum name "from" in the columns list. 您需要将反引号放在列列表中的列名“ from”周围。

Like this 像这样

INSERT INTO notifications (`username`, `purpose`, `url`, `from`)

Without backticks "from" gets interpreted as mysql keyword. 没有反引号,“ from”被解释为mysql关键字。 That would cause an error. 那会导致错误。

You need to use quotes around the string keys when you are accessing them in an array. 在数组中访问字符串键时,需要在字符串键周围使用引号。

Your query3 should be: 您的query3应该是:

$query3 = "INSERT INTO notifications (username, purpose, url, from)
           VALUES ('$notif[\"creator\"]', 'commented on your thread',
                   '/forums/view_topic.php?id=$id', '$_SESSION[\"username\"]')";
$active3 = mysqli_query(db(), $query3);

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

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