Jump to content

check box used in table to edit mysql in php


tracy

Recommended Posts

I can display a table's contents (mysql) using php.  I'd like to display this info with a check box option (or similar) in each row.  When the check box is 'checked' and submit is clicked, I'd like that record selected for edit/delete...

how would one do that...

here is the display table data page, though not pretty.  It's just to learn for now...

<?php
include ("link.php");

$query= mysql_query("SELECT * FROM inventory") or die("ERROR:" . mysql_error()); 



$num = mysql_num_rows($query);

if ($num == '0') {
echo "Nothing Exist.";
die();
}
else {

?>
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<td><font face=Arial>Stock#</font>
</td>
<td><font face=Arial>Year</font>
</td>
<td><font face=Arial>Make</font>
</td>
<td><font face=Arial>Model</font>
</td>
<td><font face=Arial>Price</font>
</td>
<td><font face=Arial>Miles</font>
</td>
<td><font face=Arial>Photo</font>
</td>
</tr>
<?

while ($info = mysql_fetch_array($query)) {
$stock = $info['stock'];
$year = $info['year'];
$make = $info['make'];
$model = $info['model'];
$price = $info['price'];
$miles = $info['miles'];
$photo1 = $info['photo1'];
echo "
<tr>
<td> <font face=Arial>".$stock."</font>
</td>
<td> <font face=Arial>".$year."</font>
</td>
<td> <font face=Arial>".$make."</font>
</td>
<td> <font face=Arial>".$model."</font>
</td>
<td> <font face=Arial>".$price."</font>
</td>
<td> <font face=Arial>".$miles."</font>
</td>
<td> <font face=Arial>".$photo1."</font>
</td>
</tr>
";
}
?>
</table>
<?
}
?>
You have to create the checkboxes with the same name and an HTML array identifier like so:

<input type="checkbox" name="model[]" value="whatever_value" />

When you access this with your handler, access it as an array:

$checks = $_POST['model'];

$checks now contains an array with all checked boxes.  Don't forget to filter your input!
font face=Arial... use css and set it once.
to show a checkbox in php

<?
$checked = ($row['myBox']) ? 'CHECKED' : '';
$disp .= "<td><input type='checkbox' name='myBox' value='1' $checked /></td>";
?>

to see if a box is checked when your are processing a form... well use
[code]<?
echo "<pre>".print_r($_REQUEST,1);
?>[/code]
at the top of your form so you can figure out what you have to work with.
I appreciate both your responses.  I almost understand the first one, and have previously tried something quite similar.  It just did not work.  Would it be too much to ask that you show me one line of code for one line in the table with the checkbox or is that not possible due to the method being used? 

The second response regarding CSS, well I just don't understand that.  I'm kind of new to php.  Sorry and thanks to both of you.

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.