Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Trouble updating db using arrays - very confused!

Using arrays in forms to update arrays.

4:45 am on Apr 25, 2006 (gmt 0)

Junior Member

10+ Year Member

joined:Apr 20, 2006
votes: 0

Hi, I've got a db with just 3 columns.

1) id
2) man_name
3) display_index

It currently only has 4 records:

id man_name display_index
1 Kenwood 1
2 Motorola 2
3 Icom 3
4 Cobra 4

I need to be able to change the 'display_index' of each record using a form, updating all the records at the same time. I have made a form which lists the records with the 'display_index' as an editable text box. I have a submit button to 'POST' the form and a script at the top of the page which detects whether the button was pushed and does it's stuff if it was.

The problem is that I'm going around in circles trying to get the arrays to work. I'm a newby to PHP and am still struggling with the concept of arrays. The code I have is below...

<form action='<? $_SERVER['PHP_SELF'];?>' method='post' style="display:inline;">
<table cellpadding="0" cellspacing="5" border="1">
$query = "SELECT `id`, `man_name`, `display_index` FROM `manufacturers` ORDER BY display_index " ;
$result = mysql_query( $query );

while ($result2 = mysql_fetch_array($result))
$id = $result2['id'];
$man_name = $result2['man_name'];
$display_index = $result2['display_index'];

echo "<tr>";
echo "<td><b>$man_name</b></td>";
echo "<td><input type='hidden' name='id[]' value='$id'>";
echo "<input type='hidden' name='man_name[]' value='$man_name'>";
echo "<input type='text' name='display_index[]' value='$display_index' size='4' maxlength='4' style='font-size: 9px;'></td>";
echo "</tr>";
<input type="submit" name="update_display_index" value="Update" style="width: 48px; font-size: 9px;">

foreach($_POST['id'] as $id)
foreach($_POST['display_index'] as $display_index)
$query = "UPDATE `manufacturers` SET `display_index`='$display_index' WHERE `id`='$id'";
$result = mysql_query( $query );

// check for errors and print results to screen for diagnostics.
if ($result)
$status = $status."<br>" . "'s display index was is set to $display_index";
$status = $status."<br>There was an error updating the entry." .mysql_error();


Update display indexes module was activated.
Array's display index was is set to 2
Array's display index was is set to 2
Array's display index was is set to 2
Array's display index was is set to 2

This was assuming that I had typed '2' in the last record.

Please can someone help me to understand how to do this :)

Thanks in advance


[edited by: jatar_k at 6:52 pm (utc) on April 25, 2006]
[edit reason] fixed formatting [/edit]

10:15 pm on Apr 30, 2006 (gmt 0)


WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
votes: 2

Welcome to WebmasterWorld, snowweb.

Are you trying to update the display_index for a certain row and then process the other rows accordingly? Meaning, if the user wants to change the display_index currently stored as 2 to 3, you want 3 and 4 to be updated to 4 and 5?


Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members