PHP - MySQL problem with checkboxes in form

Discussion in 'PHP' started by yoyo99, Mar 2, 2009.

  1. #1
    Hey, I will try to explain in the problem and what I want to accomplish the best I can.

    I have a series of records I pull from my database and display on my webpage. That works fine. On the web page, each record has a checkbox at the end of the row. What I want is when the user clicks a checkbox or two, then hits a submit button, that more data stored in the Database from the record will be displayed on the new page.

    Here is a small example:

    On the first page, the record is displayed something like this-

    Name: Joe; Occupation: Roofer; (checkbox)...

    Then once the checkbox is selected and the user hits submit, the following info would be displayed on a separate page:

    Name: Joe; Occupation: Roofer; Phone: 555-1234; Address: 99 Main St.;

    Any ideas on how to go about doing this? I am new to PHP so please try to explain. I would appreciate any and all comments and ideas, Thanks! :)
     
    yoyo99, Mar 2, 2009 IP
  2. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #2
    $sql = "select id, name, occupation from person_table";
    $st = mysql_query($sql) or die(mysql_error());
    while ($row = mysql_fetch_assoc($st))
    {
       echo "<p>Name: {$row['name']}; Occupation: {$row['occupation']}"
          . '<input type="checkbox" name="person[' . $row['id'] . ']" value="1">';
    }
    Code (markup):
    When this form is posted back to you, the array $_REQUEST['person'] will conveniently - nay, miraculously - hold the IDs of each checked item.

    You can then sanitize it and use it for your detailed query:

    $clean_people = array();
    foreach ($_REQUEST['person'] as $raw_person)
       if ($clean_person = intval($raw_person))
          $clean_people[] = $clean_person;
    if (count($clean_people))
    {
       $sql = "select * from person_table where id in ("
          . join(', ', $clean_people) . ")";
    }
    else
       echo "<p>No people selected, doofus.</p>";
    Code (markup):
     
    SmallPotatoes, Mar 2, 2009 IP
  3. yoyo99

    yoyo99 Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Hey, thanks for the quick response! One question though, what code should I use to display all the info from the records that were checked?
     
    yoyo99, Mar 2, 2009 IP
  4. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It's hard to guess without knowing what your data is like. Basically, after the "$sql =" assignment in that second code snippet (inside that block delineated by curly braces), you'd execute the query, loop through your result rows, and output whatever you like.
     
    SmallPotatoes, Mar 2, 2009 IP
  5. yoyo99

    yoyo99 Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    thanks again for replying so fast. I tried to loop the results and only get the first record displaying, no matter which checkbox I click or how many I select. Only shows the first record from my database.

    If you wouldn't mind taking a quick look at my code to see what you think, I will love you forever ... lol

    
    $clean_people = array();
    foreach ($_REQUEST['person'] as $raw_person)
       if ($clean_person = intval($raw_person))
          $clean_people[] = $clean_person;
    if (count($clean_people))
    {
       $sql3 = "SELECT * FROM myDB WHERE propID IN ("
          . join(', ', $clean_people) . ")";
    // Here is my code ...
    	$result3 = mysql_query($sql3);
    	$numberOfRows3 = mysql_num_rows($result3);
    	
    	if ($numberOfRows3 > 0){
    	$k = 0;
    while ($k < $numberOfRows3){
    		$city = mysql_result($result3,$k,"city");
    		$ln = mysql_result($result3,$k,"ownerLname");
    ?>
    		
    <table>
    <tr>
    <td><? echo $city ?></td>
    </tr>
    <tr>
    <td><? echo $ln ?></td>
    </tr>
    
    <?
    $k++;
    } // end while
    } // end numberOfRows	
    } // end if count
    else
       echo "<p>No people selected, doofus.</p>";
    ?>
    </table>
    
    Code (markup):
    Let me know what you think. I greatly appreciate all of your help and input :D
     
    yoyo99, Mar 3, 2009 IP
  6. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #6
    You can squeeze your code down a bit which would make it easier to read and work with; there's no need to monkey around with $k if you're not going to actually use the number for anything (and if you are, use a for loop). Also, you are generating multiple <table> tags when I think you really just want one. Instead of:

    	$result3 = mysql_query($sql3);
    	$numberOfRows3 = mysql_num_rows($result3);
    	
    	if ($numberOfRows3 > 0){
    	$k = 0;
    while ($k < $numberOfRows3){
    		$city = mysql_result($result3,$k,"city");
    		$ln = mysql_result($result3,$k,"ownerLname");
    ?>
    		
    <table>
    <tr>
    <td><? echo $city ?></td>
    </tr>
    <tr>
    <td><? echo $ln ?></td>
    </tr>
    
    <?
    $k++;
    } // end while
    } // end numberOfRows
    Code (markup):
    You could just have:
    
    $result3 = mysql_query($sql3) or die(mysql_error());
    if (mysql_num_rows($result3))
    {
       echo "<table>";
       while ($row = mysql_fetch_row($result3))
       {
          echo "<tr>
             <td>{$row['city']</td>
             </tr>
             <tr>
             <td>{$row['ownerLname']}</td>
             </tr>";
       } // end while loop through result rows
       echo "</table>";
    } // end if
    Code (markup):
    But anyway, this part of the code looks okay at first glance, other than the table tag thing. I wonder if the problem is with the form on the previous page. Can you include that code too?
     
    SmallPotatoes, Mar 3, 2009 IP
  7. yoyo99

    yoyo99 Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Sure, I am just using your code with slight mod to the names to match my DB.
    
    <?php 
    $sql = "SELECT propID, ownerLname, city, price FROM myDB";
    $st = mysql_query($sql) or die(mysql_error());
    while ($row = mysql_fetch_assoc($st))
    {
    <html>
    <body>
    <form method="post" action="t2.php">
    <?
    echo "<p>Name: {$row['ownerLname']}; City: {$row['city']}"
         . '<input type="checkbox" name="person[' . $row['propID'] . ']" value="1">';
    }	
    ?>
    <br />
    <input type="submit" value="Submit" />
    </form>
    </body>
    </html>
    
    Code (markup):
    I am gonna use this code in a different page, once it works properly. Not sure if you wanted to see that code or not. But for now I am using the code you gave me and just pulling the records from my DB.

    Oh yeah, I plugged in your last code and now I am not getting anything on the second page... lol hope I didn't break something...

    Thanks again! You ROCK! :cool:
     
    yoyo99, Mar 3, 2009 IP
  8. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I forgot a closing curly brace after {$row['city']. That's probably why nothing is showing at all.

    As for the rest, I'd recommend doing the following:

    1) View source on the form to make sure the checkboxes look like this:

    <input type="checkbox" name="person[123]" value="1">
    Code (markup):
    (of course the number 123 would be different).

    2) Print out your $sql3 and make sure it's sensible.

    One of us has made a mistake but it's not immediately clear to me what it is.
     
    SmallPotatoes, Mar 3, 2009 IP
  9. yoyo99

    yoyo99 Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    this is on the page when i print $sql3:

    
    SELECT * FROM myDB WHERE propID IN (1, 1, 1)
    
    Code (markup):
    I have 3 checkboxes selected.

    I added the missing curly brace and still no records are displaying.

    Any ideas?
     
    yoyo99, Mar 3, 2009 IP
  10. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Thanks. It was I who made the mistake. Try changing this:

    foreach ($_REQUEST['person'] as $raw_person)
    Code (markup):
    to this:
    foreach ($_REQUEST['person'] as $raw_person => $x)
    Code (markup):
     
    SmallPotatoes, Mar 3, 2009 IP