Jump to content

UPDATE array...what's wrong with this short code?


TCombs

Recommended Posts

As you can see, this array is setting each colorvalue to each idvalue.

 

<?php

include("config.php");
include("db.php");

$arrid=$_POST['id'];
$arrcolor=$_POST['color'];

foreach ($arrid as $idvalue)
{
foreach ($arrcolor as $colorvalue)
{
	$sql = "UPDATE colors SET color='$colorvalue' WHERE id = '$idvalue'";
	mysql_query($sql) or die("Error: ".mysql_error());
	echo $sql;
}
}


header("Location: " . $config_basedir . "adminhome.php");
?>

Link to comment
Share on other sites

dcro2...glad you're here lol

 

Here is the form that is pulling colors from the 'color' table.

 

<table width="500" border="1" cellpadding="10">

<tr>
  	<td>Color Options:</td>
    <td>
    <?		
		$result = mysql_query("SELECT * FROM colors");
		while ($row = mysql_fetch_assoc($result)) {

			echo '<input type="text" name="color[]" value="' . $row['color'] . '"/><br />';
			echo '<input type="hidden" name="id[]" value="' . $row['id'] . '"/>';
		}

		?>
   </td>
</tr>

   <tr>
  	<td> </td>
  	<td><input type="Submit" value="Update Colors"></td>
  </tr>
</form>
</table>

 

 

 

 

All I want to do is edit color names.  For example, if someone misspells "yelow", I want them to be able to type in the correct spelling "yellow".  Once the correction is made, they click submit and the table is updated.

 

However, with the current setup, this is what I get when I echo the query:

UPDATE colors SET color='Candy Apple Red' WHERE id = '193'UPDATE colors SET color='WHITE' WHERE id = '193'UPDATE colors SET color='GREEN' WHERE id = '193'UPDATE colors SET color='Candy Apple Red' WHERE id = '192'UPDATE colors SET color='WHITE' WHERE id = '192'UPDATE colors SET color='GREEN' WHERE id = '192'UPDATE colors SET color='Candy Apple Red' WHERE id = '191'UPDATE colors SET color='WHITE' WHERE id = '191'UPDATE colors SET color='GREEN' WHERE id = '191'

Link to comment
Share on other sites

Haha, hi. I think this is what you want instead then:

for($i=0; $i<count($arrid); $i++)
{
$sql = "UPDATE colors SET color='".$arrcolor[$i]."' WHERE id = '".$arrid[$i]."'";
mysql_query($sql) or die("Error: ".mysql_error());
echo $sql;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.