Results 1 to 4 of 4
  1. #1
    Roll Tide mobilebadboy's Avatar
    Join Date
    January 18th, 2005
    Location
    Mobile, Alabama
    Posts
    1,220
    Update mysql rows with checkboxes
    I've been racking my brain and I just can't anything to work, maybe I've been looking at it too long. Anyway, I need help.

    Basically I've got a review/comments system setup on one of my sites. When a review is submitted it's flagged as not approved (approved=no). I'm trying to setup an admin script where I can grab all of the unapproved comments, each one being displayed with 2 checkboxes (one for approve, one for delete) and then hit submit and all reviews get updated in my mysql DB.

    Grabbing the info from the DB is no problem, it's trying to get a process to loop through all of them and update accordingly that's giving me headaches. Each review is parsed out into a table (inside of a form, of course):

    Code:
    <table width="500" cellspacing="1" cellpadding="3"><tr>
    <td><input type="hidden" name="reviewer" value="<?=$r['reviewer'];?>"><?=$r['reviewer'];?></td>
    <td><input type="hidden" name="date" value="<?=$r['date'];?>"><?=$r['date'];?> CST</td>
    <td><input name="approve" type="checkbox"></td>
    <td><input name="deny" type="checkbox"></td>
    </tr><tr>
    <td colspan="4"><?=$r['review'];?></td>
    </tr></table>
    Since there are no IDs per comment (they're all ID'ed per casino), then I want to update where the reviewer and date match. If checked for approval then:

    PHP Code:
    update casino_reviews set approved='yes' where reviewer='$reviewer' and date='$date' 
    and if checked for deletion then:

    PHP Code:
    delete from casino_reviews where reviewer='$reviewer' and date='$date' 
    I'd really appreciate some help on this.

    Shawn Kerr (.com) | Disney World | SEC Football

  2. #2
    ABW Ambassador FFoc's Avatar
    Join Date
    January 18th, 2005
    Posts
    1,015
    If you have multiple reviews on one submit, you need to increment all the input field names so your processor script knows to which the various checkboxes refer, or you're going to have a mess.

    Easiest way to do it is to tell your php script that the fields you are passing are arrays (note the square brackets '[]'):
    Code:
    <table width="500" cellspacing="1" cellpadding="3"><tr>
    <td><input type="hidden" name="reviewer[]" value="<?=$r['reviewer'];?>"><?=$r['reviewer'];?></td>
    <td><input type="hidden" name="date[]" value="<?=$r['date'];?>"><?=$r['date'];?> CST</td>
    <td><input name="approve[]" type="checkbox"></td>
    <td><input name="deny[]" type="checkbox"></td>
    </tr><tr>
    <td colspan="4"><?=$r['review'];?></td>
    </tr></table>
    Then in your php, simply loop over the array and insert your data accordingly:
    PHP Code:
    $reviewer $_POST['reviewer'];
    $date $_POST['date'];
    // etc..
    $i=0;
    while (
    $i sizeof($reviewer)) {
      
    // test and insert..
      // $reviewer[i], etc..
      
    $i++;


  3. #3
    Roll Tide mobilebadboy's Avatar
    Join Date
    January 18th, 2005
    Location
    Mobile, Alabama
    Posts
    1,220
    Thanks, you got me pointed in the right direction, although it still took me half an hour to get to this point, lol. I ended up with this (for testing, will swap in my DB calls). It works, sort of. I'm still getting a slight problem.

    This is what I have:

    PHP Code:
    <?
    //connect

    $reviewer $_POST['reviewer']; 
    $date $_POST['date']; 
    $i=0

    if(isset(
    $_POST['approve'])){
        foreach(
    $_POST['approve'] as $key=>$value) {
            echo 
    "$reviewer[$i] - $date[$i]<br />";
        
    $i++; 
        } 
    }

    echo 
    "<br /><br />";

    if(isset(
    $_POST['deny'])){
        foreach(
    $_POST['deny'] as $key=>$value) {
            echo 
    "$reviewer[$i] - $date[$i]<br />";
        
    $i++; 
        } 
    }
    ?>
    I'm testing with 3 reviews. If I check approve for the first 2, and deny for the 3rd, it echos out correctly:

    Dave - Jan 26, 2005 - 7:15:00 PM //first review
    Anonymous - Jan 26, 2005 - 7:20:00 PM //second review


    Bob Smith - Jan 26, 2005 - 7:25:00 PM //third review

    However, if I approve the 1st, deny the 2nd, and approve the 3rd, I get this:

    Dave - Jan 26, 2005 - 7:15:00 PM //first review
    Anonymous - Jan 26, 2005 - 7:20:00 PM //second review, should be denied


    Bob Smith - Jan 26, 2005 - 7:25:00 PM //third review, should be approved

    It's got some other quirks too. If I deny the first 2, and deny the third, it displays in an incorrect order. I believe it's something simple, but again I'm totally lost at this point. It works, just not correctly.

    Shawn Kerr (.com) | Disney World | SEC Football

  4. #4
    ABW Ambassador FFoc's Avatar
    Join Date
    January 18th, 2005
    Posts
    1,015
    Your radio boxes need to be 'name'd the same, with different values. That's the quirk.

  5. Newsletter Signup

+ Reply to Thread

Similar Threads

  1. Update Mysql Table Using Inner Join Problem
    By Uncle Rico in forum Programming / Datafeeds / Tools
    Replies: 8
    Last Post: March 26th, 2009, 08:42 PM
  2. Using xml to UPDATE my mySQL database. tricky!
    By halifaxer in forum Programming / Datafeeds / Tools
    Replies: 4
    Last Post: February 20th, 2008, 10:20 PM
  3. Database of datafedds how to update - mysql
    By dflsports in forum Programming / Datafeeds / Tools
    Replies: 7
    Last Post: December 21st, 2005, 01:14 PM
  4. Update Command question in MySQL
    By hotspice in forum Programming / Datafeeds / Tools
    Replies: 5
    Last Post: July 11th, 2005, 06:08 PM
  5. rows
    By cowox in forum Programming / Datafeeds / Tools
    Replies: 0
    Last Post: January 2nd, 2003, 01:12 AM

Posting Permissions

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