php - 关联数组中 undefined index 和 MySQL 语法错误

标签 php mysql

当尝试将数据插入数据库表时,我收到以下错误:

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/

相关文章:

php - 使用 Codeigniter 迁移和 DBFORGE 在 POSTGRESQL 中创建 ENUM

php - Symfony2 - 有没有办法 Hook 事件以供用户确认?

php - MySQL 更新语法错误

MySQL 与 MongoDB 聚合性能对比

mysql - 三张表之间的SQL查询

mysql - 访问虚拟主机时 SQL 访问被拒绝

mysql - php mysql 选择数据所在列数组

php - 如何更改 SQL 表的日期(使用 phpmyadmin)?

如果不在当前表中,mysql 从存档表中选择数据

php - 使用 PHPWord 下载 DOCX 文件时出现损坏的文件