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 Quote Link to comment Share on other sites More sharing options...
Solution benanamen Posted August 9, 2017 Solution 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. 1 Quote Link to comment Share on other sites More sharing options...
lovephp Posted August 9, 2017 Author Share Posted August 9, 2017 (edited) 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? Edited August 9, 2017 by lovephp Quote 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. Quote 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.