Forum Moderators: coopster
Now this is how I diplay the rows:
print "<br><br>There are $num_rows records.<P>";
print "<table width=500 border=0>\n";
while ($get_info = mysql_fetch_row($result)) {
print "<tr>\n";
foreach ($get_info as $field)
print "\t<td><font face=arial size=1/>$field</font></td>\n";
print "</tr>\n";
How would I go about attaching this:
<input type="checkbox" value="..." name="...">
print "<br><br>There are $num_rows records.<P>";
print "<table width=500 border=0>\n";
while ($get_info = mysql_fetch_row($result)) {
print "<tr>\n";
print "<td><input type=\"checkbox[]\" value=\"" . $get_info['row_id'] . "\" name=\"check_" . $get_info['row_id'] . "\"></td>\n";
foreach ($get_info as $field)
print "\t<td><font face=arial size=1/>$field</font></td>\n";
print "</tr>\n";
And your php to access the checkbox's value would be:
if (isset($_POST['checkbox']))
{
foreach ($_POST['checkbox'] as $checkBoxName=>$checkBoxValue)
{
echo 'The database id: ' . $checkBoxValue . ' has been checked';
}
}
I want to be able to delte the item which is 'checked', so this is how I am attempting to go about it:
$num_rows = mysql_num_rows($result);
print "<br><br>There are $num_rows records.<P>";
print "<table width=200 border=0>\n";
while ($get_info = mysql_fetch_row($result)) {
print "<tr>\n";
print "<td><input type=\"checkbox\" value=\"" . $get_info['idtable'] . "\" name=\"check_" . $get_info['idtable'] . "\"></td>\n";
foreach ($get_info as $field)
print "\t<td><font face=arial size=1/>$field</font></td>\n";
print "</tr>\n";
}
print "</table>\n";
echo "<input name=Submit type=submit>";
With this being the delete code:
if(isset($_POST['submit'])){
$sql = "DELETE FROM items";
$result = mysql_query($sql);
echo "Record deleted!";
}
I think I am missing something, but I am not sure what exactly. Any suggestions?
print "<td><input type=\"checkbox\" value=\"" . $get_info['row_id'] . "\" name=\"check[]\"></td>\n";
This way there's an array of checked checkboxes, and you don't have to loop through all of them.
Best wishes
Michal Cibor
BTW. Welcome to webmasterworld!
To delete:
$ids = implode(',', $_POST["check"]); // I'm not sure if this'll do the trick
DELETE FROM items WHERE id IN ($ids); // IN (1,3,6) will erase rows with id = 1, 3 and 6
$num_rows = mysql_num_rows($result);(change is bold)
print "<br><br>There are $num_rows records.<P>";
print "<table width=200 border=0>\n";
while ($get_info = mysql_fetch_row($result)) {
print "<td><input type=\"checkbox\" value=\"" . $get_info['row_id'] . "\" name=\"check[]\"></td>\n";
foreach ($get_info as $field)
print "\t<td><font face=arial size=1/>$field</font></td>\n";
print "</tr>\n";
}
print "</table>\n";
echo "<input name=Submit type=submit>";
delete code:
if(isset($_POST['submit'])){
$ids = implode(',', $_POST["check"]);
$sql = "DELETE FROM items WHERE id IN ($ids)";
if($result = mysql_query($sql)) echo "Records deleted!"; //there are as many records deleted as there are checkboxes checked
}
Hope this helps!
Michal Cibor
if(isset($_POST['submit'])){
$ids = implode(',', $_POST["check"]);
$sql = "DELETE FROM items WHERE id IN ($ids)";
if($result = mysql_query($sql)) echo "Records deleted!"; //there are as many records deleted as there are checkboxes checked
}
print "<td><input type=\"checkbox\" value=\"" . $get_info['row_id'] . "\" name=\"check[]\"></td>\n";
I tried it the way that you posted it, then I tried it with 'id' being changed to 'row_id', however it doesn't seem to be going. I mean, it doesn't seem that the action is being instantained once the button is clicked.
<body><form action="#" method="POST">
<?php
for($i = 1; $i <= 10; $i++)
{
echo $i.". "?>
<input type="checkbox" value="<?php echo $i;?>" name="check[]">
<?
}
$ids = implode(',', $_POST["check"]);
print "<p>Ids: $ids</p>";
$ask = "UPDATE items SET idtable='8' WHERE row_id IN ($ids)";//just exemplary
if(my_query($ask)) echo "Done"; //function my_query first connects to a db, then asks a query
else echo "Not good:(";?>
<input type="submit">
</form>
</body>
The problem may be, that you don't connect to the db for the second time. I'm not sure how it is done, but that could be it (the php forgets what to connect).
Michal Cibor