当尝试将数据插入数据库表时,我收到以下错误:
Notice: Undefined index: follower_user_id in C:\xampp\htdocs\blog\newentry.php on line 29
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' 'post', 12, NOW(), NOW())' at line 1
相关代码如下:
$node_sql = "SELECT * FROM nodes WHERE followed_blog_id=".$row['id']." order by id DESC;";
$node_result = mysql_query($sql);
while ($node_row = mysql_fetch_assoc($node_result)){
$event_sql = "INSERT INTO events (followed_id, follower_id, type, item_id, last_active, date) VALUES (".$row['id'].", ".$node_row['follower_user_id'].", 'post', ".$item_id.", NOW(), NOW());";
mysql_query($event_sql) or die(mysql_error());
}
虽然通知说第 29 行索引“follower_user_id”未定义,但我检查了数据库表,它是拼写正确的索引,所以我不知道是什么导致了这个问题。 我还检查了 SQL 语句的语法,没有发现任何问题。我在这里忽略了什么?
如有任何帮助,我们将不胜感激!
最佳答案
第 2 行 $node_result = mysql_query($sql); 可能应该使用 $node_sql 作为查询调用中的变量。 我认为生成的 INSERT 语句有一个空白 $node_row['follower_user_id'] 所以有两个逗号,中间没有任何内容,导致 sql 解析器哭了:)
关于php - 关联数组中 undefined index 和 MySQL 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11813624/