How do I get My SELECT query to select the information for the 3 ticked Items I chose

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jason1975
    New Member
    • Jul 2010
    • 3

    How do I get My SELECT query to select the information for the 3 ticked Items I chose

    I have a list of tickboxes named "model" with different values genreated from mysql DB. When I tick 3 of them the script below below only bring the information of the last model ticked. how do I get the 3 ticked items to display at once. You urgent help is much appreciated.

    Code:
    <?php
    $self = $_SERVER['PHP_SELF'];
    $Model = $_POST['Model'];
    if ( $Model );
    {
    $conn= mysql_connect('localhost,'user','cpass') or die( "could not CONNECT scotty" );
    $rs = mysql_select_db( "db name", $conn ) or die ( "no database" ); 
    $sql= "SELECT * FROM handsets WHERE Model = '$Model'";
    $rs= mysql_query( $sql, $conn ) or die( "could not do SELECT" );
    
    while ( $row = mysql_fetch_assoc( $rs ))
    {  
    ?>
    Last edited by Atli; Jul 4 '10, 05:14 PM. Reason: Added [code] tags.
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    I think there is some HTML misunderstandin g as well.
    Have you made the name of your checkboxes an array?
    Code:
    <INPUT type="checkbox" name="test[]" value="Option1">Option 1<br /> 
    <INPUT type="checkbox" name="test[]" value="Option2">Option 2<br /> 
    <INPUT type="checkbox" name="test[]" value="Option3">Option 3<br />
    And then you can look through that array to find what is checked. Not sure if this is your problem, but that will only give you the last value checked if you simply give the checkboxes the same name.

    Comment

    • jason1975
      New Member
      • Jul 2010
      • 3

      #3
      Originally posted by TheServant
      I think there is some HTML misunderstandin g as well.
      Have you made the name of your checkboxes an array?
      Code:
      <INPUT type="checkbox" name="test[]" value="Option1">Option 1<br /> 
      <INPUT type="checkbox" name="test[]" value="Option2">Option 2<br /> 
      <INPUT type="checkbox" name="test[]" value="Option3">Option 3<br />
      And then you can look through that array to find what is checked. Not sure if this is your problem, but that will only give you the last value checked if you simply give the checkboxes the same name.
      Hi Servant
      Thanks for getting back to me.
      I am new at PHP, so i think that my query that all the required data from my SQL database to form a series of tick boxes is the "array" see code below (that form the series of checkboxes based on how many models I have in the database). Thanks again for all the input you help is much appreiated.

      Code:
      <?php
      $conn = mysql_connect('localhost,'root','password') or die("Could not do it");
      $rs = mysql_select_db("cosmonet") or die("wheres the DB");
      $sql ="SELECT Model, Image FROM handsets WHERE Brand = 'Brand_name'";
      $result = mysql_query($sql);
      
      while( $row=mysql_fetch_array($result))
      { 
      $Model = $row['Model'];
      $image = $row['Image'];
      echo "<img src=\"http://bytes.com/images/$image\" width='50px' height='100%' align='absmiddle'><input name='$Model' type='checkbox' value='$Model' />$Model<br>";
      }
      ?>
      Last edited by Dormilich; Jul 8 '10, 10:33 PM. Reason: Please use [code] tags when posting code

      Comment

      • TheServant
        Recognized Expert Top Contributor
        • Feb 2008
        • 1168

        #4
        If you make the name a variable like $Model, then you will need to process each $Model checkbox separately. If you want it to be similar to your original post, then change the name to an array like:
        Code:
        while( $row=mysql_fetch_array($result))
        { 
        $Model = $row['Model'];
        $image = $row['Image'];
        echo "<img src=\"images/$image\" width='50px' height='100%' align='absmiddle'><input name='model[]' type='checkbox' value='$Model' />$Model<br>";
        }
        The array $_POST['model'] will have all the models that were checked in it. Then you can process each with a foreach loop.

        Comment

        Working...