Reputation: 55
I'm attempting at exploding an input that I've named $tags into an array, and then inserting that array into the table tags
.
I have a class Global_
with two functions: post_main()
& post_tags()
.
In the upload.php file I have them one after the other, such as:
$Global_->post_main($_POST['title'], $_POST['subtitle'], $_POST['content'], time(), $_FILES["image-file"]["tmp_name"], $_POST['image-cred'],
'0', '0', '0', $uid);
$Global_->post_tags($_POST['tags'], $_POST['title']);
In the class the post_tags()
function is as follows:
function post_tags($tags, $title) {
require "database.php";
$tag_array = explode(", ", $tags);
$tag_count = count($tag_array);
$pid_q = $db->prepare("SELECT * FROM `articles` WHERE `title` = '$title'");
$pid_q->execute();
$pid_i = $pid_q->fetch();
$pid = $pid_i['id'];
for($i=0;$i < $tag_count;$i++) {
$t_stmt = $db->prepare("INSERT INTO `tags` (tag, pid) VALUES (:tag, :pid)");
$t_stmt->bindParam(':tag', $tag_array[$i]);
$t_stmt->bindParam(':pid', $pid);
$t_stmt->execute();
}
}
The layout for the tags
table is: id
, tag
, and pid
- which is the ID of the article that it's attached to.
For some reason this data isn't being inserted into the database, although the article's information is. Any idea why?
Upvotes: 0
Views: 586
Reputation: 11859
your condition for insertion will not be executed because of this(since $tag_count > 0
as per your question):
for($i=0;$i >= $tag_count;$i++) {
should it be
for($i=0;$i < $tag_count;$i++) {
Upvotes: 1