how can we show data from database which is checked by checkbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • namita
    New Member
    • Apr 2013
    • 9

    #1

    how can we show data from database which is checked by checkbox

    i have problem with php code, i dont know how to show data which is checked using checkedbox
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    That's a pretty vague question. What data is being checked in your checkboxes? What data is supposed to be showing?

    Checkbox values are usually sent to PHP as POST arrays. That is to say, if, for example, your checkboxes use the IDs of rows in your database as their values, they may come into PHP as an array if IDs in the $_POST["ids"] element. So if you did:
    Code:
    foreach ($_POST["ids"] as $id) {
        echo $id;
    }
    This would print all the IDs that were checked.

    So, using that, you could easily query all the rows from the database where those IDs match.
    Code:
    $idList = array();
    foreach ($_POST["ids"] as $id) {
        if ((int)$id > 0) {
            $idList[] = (int)$id;
        }
    }
    
    $sql = "SELECT stuff FROM thetable
            WHERE id IN(" . implode(",", $idList) . ")";
    Of course, this is very generic and probably won't fit your exact situation. If you want more targeted advice, we need more details.

    Comment

    • namita
      New Member
      • Apr 2013
      • 9

      #3
      i got warning
      Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs \prac\test.php on line 4

      Comment

      • namita
        New Member
        • Apr 2013
        • 9

        #4
        my stuff is




        Code:
        <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
        <html xmlns="http://www.w3.org/1999/xhtml"> 
        <head> 
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> 
        <title>View records in MySQL</title> 
        
        </head> 
        
        <body> 
        
        <?php 
        //connect to the database 
        $connect = mysql_connect("localhost","root",""); 
        mysql_select_db("login",$connect); //select the table
        $query =mysql_query( "SELECT * FROM employee")or die(mysql_error());
        $id = $_POST['id'];
        
        if(isset($_POST['createcovid']))
        {
        	
        	
        	if(empty($id) || $id == 0)
        	{
        		echo "<h2><font color='#FF0000'>Please Mark id</font></h2>";	
        	}
        	else
        	{ // display date and time   
        	$timezone = "Asia/Calcutta";
        if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);
        
        	
        	echo"<center><h2>";
                  echo "Cover id generate at :".date('d-m-Y g:i a');
        		  
        		  echo "</h2></center>";
        		  // generate cover id using random number
        	$rand= rand();
        	echo"<center><h2>";
                  echo "Cover Id :".$rand;
        		  echo "</h2></center>";
        		$impid=implode(", ",$id);
        		$Qselect = mysql_query("SELECT * FROM employee WHERE id IN('.$impid.')") or die( mysql_error());
        	}
        }
        echo'
        <form action="'.$_SERVER['PHP_SELF'].'" method="post">
        <table width="100%" cellpadding="4" border="1">
        <tr>
        <td> id  </td>
        <td> reg_no  </td>
        <td> reg_time  </td>
        <td>name   </td>
        <td> email_id  </td>
        <td> valid_from  </td>
        <td> valid_to  </td>
        <td> pan_no  </td>
        <td>  policy </td>
        <td> org_name  </td>
        <td> reseller_id  </td>
        <td> reseller_name </td>
        <td>mark</td>
        </tr>';
        
        while($row = mysql_fetch_assoc($query))
        {
        	echo'
        <tr>
        <td>'.$row['id'].'</td>
        <td>'.$row['reg_no'].'</td>
        <td>' .$row['reg_time'].'</td>
        <td>'.$row['name'].'</td>
        <td>'.$row['email_id'].'</td>
        <td>'.$row['valid_from'].'</td>
        <td>' .$row['valid_to'] .'</td>
        <td>'.$row['pan_no'].'</td>
        <td>' .$row['policy'].'</td>
        <td> '.$row['org_name'].'</td>
        <td>'.$row['reseller_id'].'</td>
        <td>' .$row['reseller_name'].'</td>
        <td><input type="checkbox" name="id[]" value="'.$row['id'].'"</td>
        
        </tr>';	
        }
        mysql_free_result($query);
        echo '
        </table>
        <br>
        <div align="center">
        
        <input type="submit" name="createcovid"  value="Create cover_id"  >
        <input type="reset" name="cancel"  value="Cancel mark">
        </div>
        </form>';
        
        ?> 
        
        
        </body> 
        </html>
        Last edited by Rabbit; Apr 30 '13, 03:16 PM. Reason: Please use code tags when posting code.

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          i got warning
          Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs \prac\test.php on line 4
          The code I posted was an example; a demonstration of how you can write code that reads an array. It's not going to work if you just execute it by itself.


          my stuff is
          Remember the [code] tags, please!

          In that code, you have an <input> box for each row with the name "id[]". That means that, once submitted, all the checked IDs will be received by PHP as: $_POST["id"]. You can use that like I demonstrated in my last post to build the SQL query to fetch your data.

          Remember, the code I posted earlier is an example. It won't work unaltered for you; you have to adapt it to fit your database and your code.

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            By the way, the checkbox <input> in that code is missing the closing > char. That could well be causing problems in the browsers.

            Comment

            • namita
              New Member
              • Apr 2013
              • 9

              #7
              plz tell me how can i modify my code?

              Comment

              • namita
                New Member
                • Apr 2013
                • 9

                #8
                i insert your code in my code then i got only id of checked box but i want to display all information which is checked

                Comment

                • Atli
                  Recognized Expert Expert
                  • Nov 2006
                  • 5062

                  #9
                  I'm not going to write the code for you. The examples I provided in my earlier post demonstrate how you can construct a SQL query to fetch data based on checked IDs. It's not difficult to use that knowledge to achieve what you are asking for, as long as you actually understand the concepts I'm trying to explain. - Copying and pasting my example code into your code won't do you any good. (Again, they are EXAMPLES, not working snippets meant to be used like that.)

                  I don't mean for this to sound condescending or discouraging, but if you are having trouble applying this to your code, you need to back up a step and go over the basic concepts of PHP and HTML development. Specifically how to handle form data. You can't get very far in the PHP world without a very good understanding of how that all works.

                  Comment

                  Working...