Jump to content

multiple checkbox values selected from db


Go to solution Solved by benanamen,

Recommended Posts

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

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 by lovephp

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.

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 :)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.