homepage Welcome to WebmasterWorld Guest from 54.205.168.88
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Trouble updating db using arrays - very confused!
Using arrays in forms to update arrays.
snowweb




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

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

1) id
2) man_name
3) display_index

THE DATABASE TABLE
It currently only has 4 records:

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

THE PROBLEM
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...

THE FORM
<form action='<? $_SERVER['PHP_SELF'];?>' method='post' style="display:inline;">
<table cellpadding="0" cellspacing="5" border="1">
<?php
$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>";
}
?>
</table>
<input type="submit" name="update_display_index" value="Update" style="width: 48px; font-size: 9px;">
</form>

THE PROCESSING SCRIPT
if(isset($_POST['update_display_index']))
{
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";
}
else
{
$status = $status."<br>There was an error updating the entry." .mysql_error();
}
}
}

THE SCREEN OUTPUT:

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

peter

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

 

coopster




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

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?

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved