Page 1 of 1

Looping thru and updating a recordset

Posted: Mon Nov 11, 2002 7:04 pm
by lepreston
Hi all, I have battled the problem below for many moons now and have run into an immovable brick wall since the start. So I now pose it to my PHP brothers and sisters in the hopes of ending this painful task.

Problem: I have a databse of items. Each item has an id, name, and type. At times I need to change the types on a number of items. Rather then going into my database management tool and changing each item one at a time, I would like all the items to appear in a table with one editable box (type) for each record where I can change all the ones requiring a change, click a button, and have the form update each record that has changed.

I can easily get the data and display it in a table with each row having the id, name, and an editable box showing the current type. I can go to each record and change the type but when I click the Submit button it only updates the first record in the database or doesnt update anything at all. I have a suspition that I need to add all the changed values in the form to an array of some kind and loop thru the array and update each one independently. However, im so brain wracked from this process I cant figure out where to start.

Thanks in advance. 8O

Posted: Mon Nov 11, 2002 7:50 pm
by volka
try this script

Code: Select all

<html><body>
<pre><?php print_r($_POST); ?></pre>
<form method="POST">
	<table border="1">
	<tr>
		<td>0</td>
		<td>description<input type="text" name="recordї0]їdesc]" value="A"/></td>
		<td>something else<input type="text" name="recordї0]їelse]" value="1"/></td>
		<td>field #3<input type="text" name="recordї0]їf3]" value="i"/></td>
	</tr>
	<tr>
		<td>1</td>
		<td>description<input type="text" name="recordї1]їdesc]" value="B"/></td>
		<td>something else<input type="text" name="recordї1]їelse]" value="2"/></td>
		<td>field #3<input type="text" name="recordї1]їf3]" value="ii"/></td>
	</tr>
	<tr>
		<td>0</td>
		<td>description<input type="text" name="recordї2]їdesc]" value="C"/></td>
		<td>something else<input type="text" name="recordї2]їelse]" value="3"/></td>
		<td>field #3<input type="text" name="recordї2]їf3]" value="iii"/></td>
	</tr>
	<tr>
		<td colspan="4"><input type="submit" /></td>
	</tr>
</form>
you can create such a form from your datasource e.g. with a

Code: Select all

for ($i=0; $i!=mysql_num_rows($result); $i++)
loop

Posted: Mon Nov 11, 2002 11:39 pm
by lepreston
Thanks Volka, I had to edit the code slightly to work in my environment but you certainly gave me the kick start I was in need of. Much appreciated.