Results 1 to 3 of 3

Thread: [RESOLVED] Mysql Help!!!!!!

  1. #1

    Thread Starter
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Resolved [RESOLVED] Mysql Help!!!!!!

    okay in my script i have it list a bunch of checkbox's.
    The code is sumtin like this for listing the checkbox's with each extracted name.

    PHP Code:

     $sql 
    "SELECT * FROM members WHERE trainer = 1";
      
    $result mysql_query($sql) or die(mysql_error());
      while(
    $row mysql_fetch_array($result)) {
      echo
    " <tr>
        <td scope=col><input name=rem_trainer type=checkbox value=
    $row[username]>$row[username]</td>
      </tr>"
    ;
      } 
    now when i check multiple box's and hit remove trainer. I want to remove each of the selected.
    This is how i have it so far... tho it only works for 1 at a time.

    PHP Code:

       
    if(isset($_POST['removed'])){
     echo 
    "Removed trainer " $_POST['rem_trainer'] ." from list.<BR>";
    mysql_query("UPDATE members SET trainer = 0 WHERE username = '$_POST[rem_trainer]'") or die(mysql_error());

    any help would be greatly apperciated.
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

  2. #2
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Mysql Help!!!!!!

    You need to append square brackets to the checkbox names. This will cause PHP to create an array when the data is posted. You can then loop through the array to execute the query for each selected checkbox:
    HTML Code:
    <input name="rem_trainer[]" type="checkbox" /> 
    PHP Code:
    if (is_array($_POST['rem_trainer'])) {
        foreach(
    $_POST['rem_trainer'] as $trainer) {
            
    $trainer addslashes($trainer); // use if magic quotes is disabled

            
    mysql_query("UPDATE members SET trainer = 0 WHERE username = '$trainer'") or die(mysql_error());

            echo 
    "Removed trainer " $_POST['rem_trainer'] ." from list.<BR>";
        }

    P.s: A little coding tip. It improves readability if you embed the PHP inside the HTML. Like this:
    PHP Code:
    <?php
        $sql 
    "SELECT * FROM members WHERE trainer = 1";
        
    $result mysql_query($sql) or die(mysql_error()); 
    ?>

    <?php while($row mysql_fetch_array($result)): ?>
    <tr>
        <td scope=col><input name="rem_trainer[]" type="checkbox" 
            value="<?php echo($row[username]) ?>" /><?php echo($row[username]) ?></td>
    </tr>
    <?php endwhile; ?>
    Check the alternate template like while syntax. PHP also offers the same for if, for, foreach and switch. Have a look here: http://uk.php.net/manual/en/control-...ive-syntax.php
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  3. #3

    Thread Starter
    Hyperactive Member PlaGuE's Avatar
    Join Date
    Jun 2005
    Location
    in ur mind.
    Posts
    445

    Re: Mysql Help!!!!!!

    it worked. thanks.
    Without balance, there could only be chaos.
    Without chaos, there could be no balance.
    I live with karma. Eat with destiny. Dream of life without shackles....
    Yet. If life had no consequences, life could not exist, nor could it flourish.


    If at first you dont succeed.You're screwed.

    C++/Java NOOB.

    I aint a professional at PHP, but if i can help i will.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width