samedi 25 février 2017

Php : How can I make values that have been saved in a table appear on the page as being checked?

I have a table in my app, contacts, where a user (user_id) has a list of contacts :

contact_auto_inc      user_id       contact_id
17                       2             7
18                       2             8
19                       2             9

I show these contacts, their corresponding names, with this code :

<form action="" method="POST">

        <?php

         //this code below will get the username of contacts
         // for $user_id. we get the 'contact_id'
         //values in the contacts table for $user_id, match those contact_ids to    the corresponding 
         //'user_ids' in the user table, and then show the 'usernames' for each of those user_ids
           $select_from_user_table = "SELECT  contacts.contact_id, user.username
         FROM contacts 
         INNER JOIN user
         ON contacts.contact_id=user.user_id WHERE contacts.user_id = '$user_id'";

        //get the result of the above
         $result2=mysqli_query($con,$select_from_user_table);

    //show the usernames, phone numbers
         while($row = mysqli_fetch_assoc($result2)) { ?>
         <input type='checkbox' name='check_contacts[]' value='<?=$row['contact_id']?>'> <?php echo $row['username'] ?> </br>

        <?php   
        //we need the php bracket below to close the while loop

        }

            ?>

    <!--<input type="submit" name = "create" value = "Create new Contact"></p> -->

    <!--</form> -->

<p><input type="submit" name = "Save" value = "Save"></p>
<p><input type="submit" name = "Delete" value = "Delete"></p>
<a href="exit.php">Exit</a>
</form>

</body>
</html>

So it looks like this :

image.

And if one of the boxes is checked and then saved that contact gets saved to a review_shared table like this :

<?php
//here we want to save the checked contacts to the review_shared table ;  that is,
//who the user wants to share reviews with
if(!empty($_POST['check_contacts'])) {
    foreach($_POST['check_contacts'] as $check) {

    $insert_review_shared_command = "INSERT INTO review_shared VALUES(NULL," .$_GET['id']. ", '$user_id','$check')";

        //we want to save the checked contacts into the review_shared table
        $insert_into_review_shared_table = mysqli_query($con,$insert_review_shared_command);

    }

}

    $con->close();


    ?> 

But whenever I go back to the page, I still see :

enter image description here

How would I show contacts from the contacts table that are also in the review_shared table with a check in the corresponding check box ?




Aucun commentaire:

Enregistrer un commentaire