0

I have a array like this

$outputs:
  269 => string '   SUN: 2.495' (length=13)
  510 => string '   SUN: 1.416' (length=13)

Another Array like this

$filenames:
  0 => string 'Hi35
' (length=5)
  1 => string 'He_41
' (length=6)

And to update the respective values i tried writing a code like

foreach($outputs as $key => $value){
    $sql = "UPDATE Store SET D='".$value."' WHERE `Index` = '".$filenames[$key]."'";
    mysql_query($sql);
}

But then there is no $filenames[$key] value, because the key value for $outputs starts with 269. This is only one case, the key value could be anything.

I also tried the other way around. i.e.

I combined both the array first

$arr3 = array_combine($outputs, $filenames);

And then tried to Put the combined array in SQL query like

foreach($arr3 as $key => $value){
    $sql = "UPDATE Store SET D='".$key."' WHERE `Index` = '".$value."'";
    mysql_query($sql);
}

BUT this dint work.. A help from your side will really be appreciated...

2
  • Please do not use the mysql_ api! It has been deprecated! Either use the mysqli or PDO Extensions! See documentation here: php.net/manual/en/book.pdo.php Commented May 15, 2013 at 7:18
  • Is that what you want as query? ie UPDATE Store SET D='SUN: 2.495' WHERE Index = 'Hi35' Commented May 15, 2013 at 7:34

2 Answers 2

1

You can do something like this

$outputs = array(
  '269' => 'SUN: 2.495',
  '510' => 'SUN: 1.416'
  );

$filenames = array(
  '0' => 'Hi35',
  '1' => 'He_41'
);

$array_complete = array_combine($filenames, $outputs);

foreach($array_complete as $key => $val)
{
    echo "UPDATE Store SET D='".$val."' WHERE `Index` = '".$key."'" . '<br>';
}

This will output

UPDATE Store SET D='SUN: 2.495' WHERE `Index` = 'Hi35'
UPDATE Store SET D='SUN: 1.416' WHERE `Index` = 'He_41'

Then I would like to remember you that mysql_ functions are deprecated so i would advise you to switch to mysqli or PDO

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

Comments

0

Your code doesn't look good at all mate, but here is a hack for that:

$num_outputs = count($outputs);

$index = 0;
foreach($outputs as $key => $value) {
    $sql = "UPDATE Store SET D='".$value."' WHERE `Index` = '".$index."'";
    mysqli_query($sql);
    $index++;
}

2 Comments

sorry i a newbie .. is mysqli_query($sql) this means the same as mysql_query($sql) ??
Buddy '$i' value will stat with 0 right... and '$outputs[0]' is NULL. Because $outputs value starts with 269... As i mentioned above

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.