TCombs Posted April 13, 2011 Share Posted April 13, 2011 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"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/233565-update-arraywhats-wrong-with-this-short-code/ Share on other sites More sharing options...
dcro2 Posted April 13, 2011 Share Posted April 13, 2011 Could you explain what you want your code to do? Right now all it's doing is setting each row with one id with all the color values, which means only the last one sticks. Quote Link to comment https://forums.phpfreaks.com/topic/233565-update-arraywhats-wrong-with-this-short-code/#findComment-1200949 Share on other sites More sharing options...
TCombs Posted April 13, 2011 Author Share Posted April 13, 2011 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' Quote Link to comment https://forums.phpfreaks.com/topic/233565-update-arraywhats-wrong-with-this-short-code/#findComment-1200951 Share on other sites More sharing options...
dcro2 Posted April 13, 2011 Share Posted April 13, 2011 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; } Quote Link to comment https://forums.phpfreaks.com/topic/233565-update-arraywhats-wrong-with-this-short-code/#findComment-1200954 Share on other sites More sharing options...
TCombs Posted April 13, 2011 Author Share Posted April 13, 2011 dcro2 - as usual, it's working pefectly now! Thanks!!!! I have a calculation issue I might throw your way in a bit. Quote Link to comment https://forums.phpfreaks.com/topic/233565-update-arraywhats-wrong-with-this-short-code/#findComment-1200955 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.