Log in

View Full Version : Should be simple



city_coder
04-07-2008, 04:24 PM
Hi guys its time for me to ask a question now...

Im trying to update many rows at once. My problem is of course how.

Il show you.



if (mysql_num_rows($insert_query) >0 ) {
//build headers
$header .= "<table border='1' bordercolor='#cccccc' cellspacing='0' cellpadding='3'>";
$header .= "<th bgcolor='#cccccc'>ID</th><th bgcolor='#cccccc'>First Name</th><th bgcolor='#cccccc'>Surname</th><th bgcolor='#cccccc'>Email</th><th bgcolor='#cccccc'>Paid?</th>";
echo $header;
//display information

while($row = mysql_fetch_assoc($insert_query)) {
echo '<tr><td>'.$row['ID'].'</td>';
echo '<td>'.$row['first_name'].'</td>';
echo '<td>'.$row['surname'].'</td>';
echo '<td>'.$row['email'].'</td>';
echo '<td></td></tr>';
}
echo "</table>";
}

So now at the minute i want a tick box to go in that end space. So that requires me to have a form.
That may mean i have a form for each loop, or i have a form for the whole thing and i set the checkbox to be an array and process it that way.

Is it possible to do something like UPDATE SET Edited = 'Y' WHERE ID '1,3,5' ie the numbers that are in the value of the checkbox?

Il be very grateful if anyone can just show me how, i know its completely possible, even if it requires a bit of javascript, but i would still prefer it to be php.

Also i would like to be able to have a checkboxbox at the top that selects all of the other checkboxes, that i know can be done in simple javascript but requires one form right??

jc_gmk
04-08-2008, 10:24 AM
You could just use one form if you put it outside the while statement

e.g.


<?php
if (mysql_num_rows($insert_query) >0 ) {
//build headers
$header .= "<table border='1' bordercolor='#cccccc' cellspacing='0' cellpadding='3'>";
$header .= "<th bgcolor='#cccccc'>ID</th><th bgcolor='#cccccc'>First Name</th><th bgcolor='#cccccc'>Surname</th><th bgcolor='#cccccc'>Email</th><th bgcolor='#cccccc'>Paid?</th>";
echo $header;
//display information
?>
<form action="mypage.php" method="post">
<?php
while($row = mysql_fetch_assoc($insert_query)) {
echo '<tr><td>'.$row['ID'].'</td>';
echo '<td>'.$row['first_name'].'</td>';
echo '<td>'.$row['surname'].'</td>';
echo '<td>'.$row['email'].'</td>';
echo '<td><input type="hidden" value="'.$row['ID'].'" name="id[]" /><input type="checkbox" name="check[]" /></td></tr>';
}
?>
</form>
<?php
echo "</table>";
}
?>

If you use name="id[]" it will put each value into an array.

This means when you submit this form you should have two arrays, one called id and one called check. you can then use those to enter into the database

ravi kumarr
04-24-2008, 12:01 PM
<?
include ("db1.php");
$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);
$query1="select * from manufacturers";
$sqlres1 = mysql_query($query1);
$count=mysql_num_rows($sqlres1);
$i=0;
while($rows=mysql_fetch_array($sqlres1))
{

?>
<tr><td align="center"><input name="checkbox1<? echo $i;?>" type="checkbox" value="<? echo $rows['manufacturers_id']; ?>"></td>
<td><input type="text" name="manufacturer<? echo $i;?>" value="<? echo $rows['manufacturers_name']; ?>" /></td>
<td><input type="checkbox<? echo $i;?>" name="AVAIL" value="" checked="checked" /></td> <td><a tabindex="-1" class="style1" href="javascript:var popup2=window.open('getinfo.php?action=manufacturer&id=<? echo $rows[
'manufacturers_id']; ?>','Manufacturer','width=400, height=400,location=no,menubar=no,status=no, toolbar=no,
scrollbars=yes,resizable=yes');">Details</a></td>
<!--<td>< ? echo $fetch1['item_price'];?></td>--></tr>
<?
$i++;
}
if(isset($_POST['edit']) && ($_POST['edit']=='Edit'))
{
for($i=0;$i<$count;$i++)
{
if(isset($_POST['checkbox1'.$i]))
{
$y=$_POST['manufacturer'.$i];
$ssql = "update manufacturers set manufacturers_name='".$y."' where manufacturers_id=".$_POST['checkbox1'.$i];
$rresult = mysql_query($ssql);
}
}
// if successful redirect to delete_multiple.php
if($rresult){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=manufacturer.php\">";
}
}
mysql_close();
#--------------------- End of Updation ---------------
?>

boogyman
04-24-2008, 01:34 PM
@ravi
Please refer to our Posting Policies and Regulations (http://dynamicdrive.com/forums/showthread.php?t=24866) especially in regards to #8