Re: Those checkboxes again

From: Date: Thu, 16 Jun 2005 18:18:43 +0000
Subject: Re: Those checkboxes again
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-217092@lists.php.net to get a copy of this message
Jack, Everything /appears/ to be in order here, but that still leaves the actual content of the database and the actual POST data in question. If this were my script I'd think a simple place to start troubleshooting is your /if/ statement. I'd update it to output a little more detail just until you figure out what's happening there: if ($media_rows['art_id'] === $art_id) {
       $checkbox_media[] .= "checked ";
       } else {
        print "Post ID = " . $art_id . " and DB Id = " . $media_rows['art_id'] . "<br />";
      }
--oh yeah, and watch out for those whitespace in the user data. Maybe use $art_id = (int)$_POST['art_id']; just to make sure you're getting a number passed without whitespace or any other nasties. Hopefully that will output something to help you figure out the issue. The only other thing that I can see that looked odd to me is how your using the $checkbox_media array. At the end your using <?php echo join($checkbox_media); ?> and never taking advantage of having each line in an array. Since I'm a little like Monk I couldn't help but mention that this should probably just be a standard variable containing a single string given how your using it. *wipes hands on pants and blazer* ;-) Good luck! -Joe W. Jack Jackson wrote:
hi, I'm severely frustrated and perhaps you can help with what I have done wrong. The checkboxes I use to populate an intersection table work to add the records, and now I am trying to update them. It's almost there but producing some unexpected results when the form is submitted: the idea is that it displays the checkboxes as pre-selected if they're associated with the art_id in the intersection table. They're now appearing as all unselected. Any help is appreciated. <?php $query = ''; $result = 0; $art_id = $_POST['art_id']; print_r($_POST); if (isset($_POST['editrecordMedia'])){
    if (!empty($_POST['media_types'])) {
        foreach($_POST['media_types'] as $type)
         {
             $query = "UPDATE media_art SET
             media_id='$type',
             art_id='$art_id'";
var_dump($query);
             mysql_query($query) or die(mysql_error());
         }
    }
        else {echo "mediatypes is empty";}
    //Closes if (!empty($media_types))
    }//closes if (isset($_POST['editrecordMedia'])
//If editrecord hasn't been set, but selectcartoon has, get the cartoon's recordset
    //This is just to get the title for display purposes
$query = "SELECT art_title
    FROM art
    WHERE art_id='$art_id'";
//end
    $media_query = "SELECT media.media_id,
        media.media_name,
        media_art.art_id
        FROM media
        LEFT JOIN media_art
        ON media_art.media_id=media.media_id
        AND media_art.art_id='$art_id'";
//These $art results are just to get the title for display purposes
$art_result = mysql_query($query);       $art_rows = mysql_fetch_assoc($art_result);
//end $media_result = mysql_query($media_query); $checkbox_media = array (); while ($media_rows = mysql_fetch_assoc($media_result)){
        $checkbox_media[] = "<input type='checkbox' name='media_types[]' value='{$media_rows['media_id']}' ";
        if ($media_rows['art_id'] === $art_id) {
        $checkbox_media[] .= "checked ";
        }
               $checkbox_media[] .= "/>{$media_rows['media_name']}  ";
       }
?> <table align="center">
        <tr><th colspan="4">Main Menu</th></tr>
    <tr>
        <td><a href="addrecord.php">Add A Record</a></td>
        <td><a href="chooserecord.php">Edit A Record</a></td>
        <td><a href="deleterecord.php">Delete A Record</a></td>
    </tr>
</table> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="editrecordMedia"> <input type="hidden" name="art_id" value="<?php echo $art_id ;?>"> Choose media related to <strong><?php echo $art_rows['art_title'];?></strong><br /><br /> Media: <?php echo join($checkbox_media); ?> <input type="submit" name="editrecordMedia" value="Update">
    </form>


« previous php.general (#217092) next »