Jump to content

How to insert checkbox in my php table data


shwetapandit

Recommended Posts

i want that as my program prints all the records from tthe table in columns i want a checkbox in front of every row so that if a user want to delete that particular row then he can  click on that checkbox. how can i do this.......Here is the snippet.

 

<?php
$conn=mysqli_connect("localhost","","","test");
echo"connected";

$query="SELECT * FROM student ORDER BY roll_no";
$res=mysqli_query($conn,$query);

$row=(mysqli_fetch_array($res,MYSQLI_ASSOC));

echo "roll_no".$row['roll_no'];
echo "subject".$row['subject'];
echo "marks".$row['marks']."<br/>";

echo "<table border='1'>";
echo "<tr>
 <th>ROLL_NO</th>
 <th>SUBJECT</th>
 <th>MARKS</th>
</tr>";
while($row = mysqli_fetch_array($res,MYSQL_ASSOC)){
    echo"<tr><td>";
echo $row['roll_no'];
echo "</td><td>";
echo $row['subject'];
echo "</td><td>";
echo $row['marks'];
echo "</td></td>";
//echo "<td><input type="checkbox"></td>"
//echo "</td></tr>"
}
echo "</table>";
?>
 

Hi,,

 If you have a unique id you can give it in check box value...

 

<?php
$conn=mysqli_connect("localhost","","","test");
echo"connected";

$query="SELECT * FROM student ORDER BY roll_no";
$res=mysqli_query($conn,$query);

$row=(mysqli_fetch_array($res,MYSQLI_ASSOC));

echo "roll_no".$row['roll_no'];
echo "subject".$row['subject'];
echo "marks".$row['marks']."<br/>";

echo "<table border='1'>";
echo "<tr>
 <th>ROLL_NO</th>
 <th>SUBJECT</th>
 <th>MARKS</th>

<th>Select</th>
</tr>";
while($row = mysqli_fetch_array($res,MYSQL_ASSOC))

{
    echo"<tr><td>";
echo $row['roll_no'];
echo "</td><td>";
echo $row['subject'];
echo "</td><td>";
echo $row['marks'];
echo "</td>";
echo "<td><input type=\"checkbox\" name=\"name\" value=\"$row[id]\"></td>";
echo "</tr>";
}
echo "</table>";
?>

Using the concept above, likely you would use an array.  Also, you need to wrap the table in a form and have a delete/submit button:

echo "<td><input type=\"checkbox\" name=\"records[]\" value=\"$row[id]\"></td>";

Then on the receiving page you could implode() and use in an IN clause:

$ids = implode(',', array_map('intval', $_POST['records']));
//DELETE * FROM table_name WHERE id IN ($ids)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.