GregL83 Posted April 21, 2007 Share Posted April 21, 2007 Hello, I am new at PHP and I have a project due tonight. There is probably a really simple answer to my question but I can't figure it out... please help I have an array that stores pieces of information that I want to update to a database. I decided to use the for loop to retrieve the peices of information and one by one store them into the database. The last peice of the array is stored repeatedly for the other values....I have echoed the process the loop takes and i cannot figure out the problem...Here is a simplified version of my code: $update = array(array("2" => "A"),array("3" => "B"),array("4" => "C")); print_r($update); $t = count($update); echo "<br />"."Number in Array : ".$t."<br />"; for ($c = "0"; $c < $t; $c++) { echo "Count equals: ".$c."<br />"; $number = key($update[$c]); echo "Number equals: ".$number."<br />"; $letter = current($update[$c]); echo "Letter equals: ".$letter."<br />"; $query = "UPDATE test SET number = '".$number."', letter = '".$letter."' WHERE id = '".$id."'"; $result = mysql_query($query); if($result){ echo "Database has been updated!"."<br/>"; } } The result is that I have the number 4 and letter c stored three times...but my echoes show each alue correctly...why doesnt the update work like the echoed values in the loop?????? PLEASE HELP!! Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 21, 2007 Share Posted April 21, 2007 Well, part of the problem is that nowhere in your code are you setting $id. But, you have some inefficiencies as well. For starters you should be using a foreach loop for an array. That way you don't have to create a counter or have seperate lines to define the variables For example: foreach ($update as $number => $letter) { Quote Link to comment Share on other sites More sharing options...
GregL83 Posted April 21, 2007 Author Share Posted April 21, 2007 k, thanks for the response... I'll see if the foreach loop will work...I didn't submit the whole peice of code, just the section that is having trouble so the id is declared earlier...thanks for the help, I'll see if the foreach works.but I still don't understand why logically the first way wouldn't....??? I'll make the coded as easy as possible to debug Quote Link to comment Share on other sites More sharing options...
Psycho Posted April 21, 2007 Share Posted April 21, 2007 Well, if you set $id before the loop that would explain it as well. The loop will execute as many times as needed for the array, but each iteration through the loop would update ALL records that have that $id. So if you have three records with the id being updated, all three records would be updated the first iteration of the loop, and they would all be updated the 2nd iteration, etc. Perhaps you want to update the recored where id = $id AND number = $number??? Quote Link to comment Share on other sites More sharing options...
GregL83 Posted April 21, 2007 Author Share Posted April 21, 2007 Yes! After you first response i went back and changed the loop. Thats when I noticed that the WHERE statemnet needed to be further delcared as you stated in your second response. So now the new forech loop works and well...looks far more efficient than my original. Thanks for the help! Here is what I changed it to $id = "01"; $update = array("1" => "Z", "2" => "X", "3" => "Y"); foreach($update as $number => $letter) { $query = "UPDATE test SET letter = '".$letter."' WHERE id = '".$id."' AND number = '".$number."'"; $result = mysql_query($query) or die(mysql_error()); } No echo statements = working code!!! Thanks again! Quote Link to comment 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.