Forum Moderators: coopster

Message Too Old, No Replies

update multiple mysql rows with dynamic html table

         

bilenkyj

9:41 am on Mar 26, 2008 (gmt 0)

10+ Year Member



Hi Guys, its been a very long time since i was on here. you helped me back at uni with some scripting for a website. Now im at my job and im stuck on a project - i can only think someone here can help as you have before.

Anyways heres what im trying to do -

I have a table in mysql with 3 rows and i would like to update them via an html form.

The form is generated based on the number of rows in the database - ie a query reads the number of rows and while it runes through it created the table/form.

In the form for each row is an editable text box and a check box.
At the end of the form is a submit button.

I need to be able to add text to any text box and check any check box and submit it and have the database update.

im not sure how to do it as my script so far will only remember the input from the last row andeven then i cant get it to update the database, only print out the contents of the varibles on screen.

it kinda reads like this

1.READ DATABASE TABLE - WHILE THERE ARE ROWS PRINT OUT THE TABLE/FORM
2.WHILST THE FORM IS BEING PRINTED OUT GIVE THE FORM ELEMENTS UNIQUE NAMES THROUGH THE USE OF A COUNTER - im a little unsure how this is done

<---<td width="0" bgcolor=""><input type = "text" size ="5" maxlength="10" name ="$pendingnumber"><font face="verdana" size="1"><?php echo $pendingnumber;?> (Actioned by)</font></td> --->

or

<---<td width="0" bgcolor=""><input type = "text" size ="5" maxlength="10" name ="<?php echo $pendingnumber;?>"><font face="verdana" size="1"><?php echo $pendingnumber;?> (Actioned by)</font></td> --->

Can i change the name of these forms elements with the use of a counter or do i need to submit all the values to an array and use the array to update the database? - how would i do this?

Anyways based on how im currently doing it i would need to pass the total counter number onto the part where i update the database.
so if there are 3 rows in the table the total counter is 3 - so when i run a query to update the database it will count on the form field names and update the database.

This isnt good i feel so maybe an array is the right way to go, problem is how do i populate it correctly with a dynamic form?

sorry about any rambling, can anyone help with this?

bilenkyj

11:12 am on Mar 26, 2008 (gmt 0)

10+ Year Member



ok im now looking at this method -

<------
<td width="0" bgcolor=""><input type = "checkbox" name ="yespending[]" value="Pending"><font face="verdana" size="1"><?php echo $number;?></font></td>
<td width="0" bgcolor=""><input type = "text" size ="5" maxlength="10" name ="user[]"><font face="verdana" size="1"><?php echo $pendingnumber;?> (Actioned by)</font></td>
<td width="0"><font face ="verdana" size="1"><input type = "hidden" name ="ipid[]" value ="<?php echo $companyresults['ipid'];?>" size ="40" maxlength="15"><font face="verdana" size="1"></font></td>
<td width="0"><font face ="verdana" size="1"><input type = "hidden" name ="company_name[]" value ="<?php echo $companyresults['company_name'];?>" size ="40" maxlength="15"><font face="verdana" size="1"></font></td>
---->

and

echo"<br>".$_POST['user']['0'];

echo"<br>".$_POST['ipid']['0'];

echo"<br>".$_POST['company_name']['0'];

echo"<br>". $_POST['yespending']['1'];
echo"<br>". $_POST['user']['1'];
echo"<br>". $_POST['yespending']['2'];
echo"<br>". $_POST['user']['2'];
echo"<br>";

is there a way i can create a counter to run through the array? should i do an array count and then use that value as a upper limited to a loop?

PHP_Chimp

11:39 am on Mar 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I am slightly confused about what you are doing (I'm easily confused ;) so I will try to give you some ideas.

Have a look at -
foreach [uk3.php.net] as this will help you iterate through an array and you dont need to worry about counting the elements and running them through a while or for loop.
mysql_fetch_array [uk3.php.net] and the example that shows the use of a while loop to iterate through all of the returned results.
count [uk3.php.net] as if all else fails you can use this with while [uk3.php.net] of for [uk3.php.net] loops to iterate through the array.

Hopefully the examples in the manual will point you in the correct direction.

Also you may want to have a look at the here doc syntax in echo [uk3.php.net]. As you can use it like this -


echo <<<CODE
<br>{$_POST['user']['0']}
<br>{$_POST['ipid']['0']}
<br>{$_POST['company_name']['0']}
<br>{$_POST['yespending']['1']}
<br>{$_POST['user']['1']}
<br>{$_POST['yespending']['2']}
<br>{$_POST['user']['2']}
<br>
CODE;

Just saves writing echo hundreds of times.
Although if you start iterating through the array then you wont need this anyway...so may not be so helpful after all ;)

bilenkyj

1:02 pm on Mar 26, 2008 (gmt 0)

10+ Year Member



Hey thanks for the info
I have now managed to get it to submit the form data and i can add it to the db like this

if($_POST['trigger']=="go"){

while($counter<=($_POST['number']-1)){

echo $_POST['yespending'][$counter].$_POST['user'][$counter].$_POST['ipid'][$counter].$_POST['company_name'][$counter];
echo"<br>";

$user=$_POST['user'][$counter];
$flag=$_POST['yespending'][$counter];
$ipid=$_POST['ipid'][$counter];
$company_name =$_POST['company_name'][$counter];

$reqstamp=date("d/m/y : H:i:s", time());
$q="UPDATE ipaddresses SET pendingstamp='$reqstamp', pendinguser='$user', assignedflag='$flag' WHERE company_name='$company_name' AND ipid='$ipid'";
$r=mysql_query($q);

$counter++;

}

}