I have been building a forum or blog if you will for my web portfolio with PHP and MySQL. I'm pretty decent with PHP but MySQL has always been a problem for me because I have never had a reason to use MySQL or any database before this, not MySQL error messages are the worst I have error seen. This is the query I am trying to use for add new post to the forum/blog thing.
START TRANSACTION;
DECLARE postKey int;
INSERT INTO posts(post_subject, post_content, post_date, post_by)
SELECT ?, ?, NOW(), user_id FROM users WHERE user_name = ? LIMIT 1;
SET postKey = LAST_INSERT_ID();
INSERT INTO juct_tags_posts(post_key, tag_key)
SELECT postKey, tag_id FROM tags WHERE tag_name = ? LIMIT 1;
INSERT INTO juct_tags_posts(post_key, tag_key)
SELECT postKey, tag_id FROM tags WHERE tag_name = ? LIMIT 1;
INSERT INTO juct_tags_posts(post_key, tag_key)
SELECT postKey, tag_id FROM tags WHERE tag_name = ? LIMIT 1;
COMMIT;
The first insert is the post_data. The next three inserts are to a junction table of tag_id and post_id. The problem is this. Prepare failed: (1064) 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 'DECLARE postKey int;INSERT INTO posts(post_subject, post_content, post_date, pos' at line 1
I have no idea what's causing the problem. Also, if this help when the query is prepare its set into the string like this.
START TRANSACTION;DECLARE postKey int;INSERT INTO posts(post_subject, post_content, post_date, post_by) SELECT ?, ?, NOW(), user_id FROM users WHERE user_name = ? LIMIT 1;SET postKey = LAST_INSERT_ID();INSERT INTO juct_tags_posts(post_key, tag_key) SELECT postKey, tag_id FROM tags WHERE tag_name = ? LIMIT 1;INSERT INTO juct_tags_posts(post_key, tag_key) SELECT postKey, tag_id FROM tags WHERE tag_name = ? LIMIT 1;INSERT INTO juct_tags_posts(post_key, tag_key) SELECT postKey, tag_id FROM tags WHERE tag_name = ? LIMIT 1;COMMIT;