Jump to content

[SOLVED] Using mysql UPDATE inside For loop ????


GregL83

Recommended Posts

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!!

Link to comment
Share on other sites

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) {

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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???

Link to comment
Share on other sites

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!

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.