0

I received two associative array and I want to update it's values in mysql db. So, I did this:

// for all other contacts
if(isset($_POST['otherAddress']) || isset($_POST['otherComments']))
{
    $otherAddress = $_POST['otherAddress'];
    $otherComments = $_POST['otherComments'];

    foreach (array_keys($otherAddress + $otherComments) as $key) 
    {
        $query = "UPDATE `boardcontacts` SET
        `address`='$otherAddress[$key]',`comments`='$otherComments[$key]'
        WHERE `memberID` = '$nID' AND `addressType`='other'";

        mysql_query($query);
    } // for end
} // if end

Problem is: It duplicate all updated rows with the last index of each array. Any help ?

9
  • can you please show your $otherAddress and $otherComments array . It will help us to resolve your problem. Commented Mar 16, 2015 at 8:38
  • what is $nID here. can you please specify me. Commented Mar 16, 2015 at 9:05
  • @NavjotSingh its the national ID the board members Commented Mar 16, 2015 at 9:51
  • @Asmaa your entire query emphasize on $nID. But in your query $nID not appear inside the loop. If it vary update unique row. Commented Mar 16, 2015 at 9:54
  • @anantkumarsingh ex: $otherAddress={@abc,@dfg} $otherAddress={twitter,instegram} Commented Mar 16, 2015 at 9:55

1 Answer 1

2

Just curious are you updating the value of $nID??
Because if the foreach executes without updating $nID the query will update the values in the same row every time because the where condition is same every time.

Sign up to request clarification or add additional context in comments.

5 Comments

You are write this is the issue, Now my question is how to solve it ?
That depends on your logic... from where you want to get the memberID?
I guess the fastest way is to add a column "contactID" as an auto increment that differentiate each row in boardContact table and pass it along with the $nID will solve the problem ^^
Yes you are correct. But if you clearly post your exact array format of $otherAddress and $otherComments, then your above code will be also modified for desired result.
Thank You so much your answer is accepted and if you can plz help me here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.