lovephp Posted August 9, 2017 Share Posted August 9, 2017 hey yall, i got a table as blog_posts and there are rows like postID, postTitle, postTag etc, well i store multiple tags in the postTag field e.g. name1, name2, name3 now while adding them i have a separate table as tags so i fetch it from there and add to the blog_posts table in postTag but now im unable to get tthem to stay selected after pulling them out from db my codes look like this <?php if(isset($error)){ foreach($error as $error){ echo $error.'<br />'; } } try { $stmt = $db->prepare('SELECT * FROM blog_posts WHERE postID = :postID') ; $stmt->execute(array(':postID' => $_GET['id'])); $row = $stmt->fetch(); $tg = $row['postTag']; $tg1 = explode(',', $tg); } catch(PDOException $e) { echo $e->getMessage(); } ?> <form action='' method='post' enctype="multipart/form-data"> <input type='hidden' name='postID' value='<?php echo $row['postID'];?>'> <p><label>Title</label><br /> <input type='text' name='postTitle' value='<?php echo $row['postTitle'];?>'></p> <br/> <p><label>Tags <small></small></label><br /><br /> <?php $stmt = $db->prepare('select * from tags'); $stmt->execute(); $rt = $stmt->fetchAll(); foreach($rt as $rtg){ ?> (<input type="checkbox" name="postTag[]" value="<?php echo $rtg['tagUrl']; ?>" <?php if (in_array($rtg['tagUrl'],$tg1)){ echo "checked"; }?>> <?php echo $rtg['tagName']; ?>) , <?php } ?> <br/> <p><input type='submit' name='submit' value='Update'></p> </form> hope someone can help me out here Link to comment Share on other sites More sharing options...
benanamen Posted August 9, 2017 Share Posted August 9, 2017 Your real problem is your database design. You do not store multiple values in the same field. You need one more table that stores the post_id's and the related tag id's. Link to comment Share on other sites More sharing options...
lovephp Posted August 9, 2017 Author Share Posted August 9, 2017 Your real problem is your database design. You do not store multiple values in the same field. You need one more table that stores the post_id's and the related tag id's. but then in edit form i will again have to fetch records of tags table and postTags table matching the blog post how can i do that to loop inside one another? By no means can the way i did is achievable? Link to comment Share on other sites More sharing options...
Sepodati Posted August 9, 2017 Share Posted August 9, 2017 By no means can the way i did is achievable?Sure, but why solve a flawed problem? Anyway, do a var_dump() on $tg1 and $rtg['tagUrl'] in your foreach and look at the output to see if you can find a reason they are not matching. Link to comment Share on other sites More sharing options...
lovephp Posted August 10, 2017 Author Share Posted August 10, 2017 Sure, but why solve a flawed problem? Anyway, do a var_dump() on $tg1 and $rtg['tagUrl'] in your foreach and look at the output to see if you can find a reason they are not matching. problem solved mate used another means to achieve it Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.