0

When trying to INSERT data into a database table, I've been getting the following errors:

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

The related code is as follows:

$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());
        }

While the notice says the index "follower_user_id" is undefined on line 29, I checked the database table and it is the correct index spelled correctly, so I have no idea as to what is causing this problem. I've also checked the syntax for the SQL statement and I don't see any problem with it. What have I overlooked here?

Any help would be appreciated!

4
  • What do you see if you echo $event_sql;? Commented Aug 5, 2012 at 3:01
  • You have 2 sql statement which one are you using? $node_sql or $sql Commented Aug 5, 2012 at 3:03
  • I think you are using the wrong query. Commented Aug 5, 2012 at 3:04
  • Yeah, wrong query, can't believe I overlooked that. Commented Aug 5, 2012 at 3:12

2 Answers 2

1

line 2 $node_result = mysql_query($sql); should probably use $node_sql as the variable in the query call. I think the generated INSERT statement has a blank $node_row['follower_user_id'] so there are two commas with nothing in between, causing the sql parser to cry :)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this solved it! I can't believe I overlooked such a silly mistake!
0

I can't find the comment link on the question, so I'll post it as an answer.

You're using a single quote on 'post' whereas every other value is covered in double quotes.

Hint: You can freely use variables alone when within a double-quoted statement. Easy SQL on the eyes.

Here's the updated version of the SQL INSERT line:

$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();";

1 Comment

The reason why I used single quotes is because "post" is not a variable, but rather a value I want to insert into the column manually.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.