1

Hey guys i want to use two arrays in on mysql UPDATE query. So here is what i have:

For example:

$ergebnis:
Array ( [0] => 100 [1] => 200 [2] => 15 )


$Index:
Array ( [0] => 3 [1] => 8 [2] => 11 )

And this is what i tried:

UPDATE `lm_Artikel` 
SET Bestand='".$ergebnis."' 
WHERE `Index` = '".$Index."'

This query seems not to work. I don't know why i enabled php error reporting and there are no errors and when i run the query it doesn't change anything in my database. Can anyone see what i did wrong?

1
  • Try printing the value of your query string and seeing what comes out. No matter how many times I make this mistake, the result always seems to surprise me. Commented Nov 28, 2012 at 13:17

5 Answers 5

4

You need to do it for each element of your arrays, hence, you can use the foreach() function:

foreach($ergebnis as $key => $value){
    $sql = "UPDATE lm_Artikel SET Bestand='".$value."' WHERE `Index` = '".$Index[$key]."'";
    mysqli_query($sql);
}

P.S. There could well be a pure-sql alternative but I'm not too SQL-hot, so I'll leave it to someone who has more expertise.

Also, please note that it may be easier for you to set the index as the array keys:

$ergebnis = Array(3=>100, 8=>200, 11=>15);

And then the foreach() would look a little better:

foreach($ergebnis as $key => $value){
    $sql = "UPDATE lm_Artikel SET Bestand='".$value."' WHERE `Index` = '".$key."'";
    mysqli_query($sql);
}
Sign up to request clarification or add additional context in comments.

Comments

3

Fellow,

it looks like that your database field is an int value so you can try doing it value by value, like this:

foreach( $Index as $key => $i ) :

    $query = "UPDATE lm_Artikel SET Bestand=".$ergebnis[$key]." WHERE Index = " . $i;
    mysqli_query($query);

endforeach;

Try it.

2 Comments

Please don't use the mysql_* functions as they are in the deprecation process. Use MySQLi or PDO instead and be a better PHP Developer.
Sorry for my mistake @JasonMcCreary. I was just illustrating a way of doing the query. I don't know what method is he using, I am used to use some framework to do it, like doctrine. Anyway, thanks.!
2
  1. You are susceptible to SQL injections
  2. You cannot use arrays in queries. A query is a string, arrays are not.

You either need to use a loop or use a CASE statement:

UPDATE `lm_Artikel`
SET `Bestandteil` = CASE `Index`
  WHEN <insert id> THEN <insert value>
  WHEN <insert other id> THEN <insert other value>
  <etc>
END

1 Comment

Interesting use of the CASE statement.
1
$data_db = array( '3' => 100,
          '8' => 200,
          '11' => 15);


foreach($data_db as $key=>$value) {
    $q = 'UPDATE lm_Artikel SET Bestand=' . $value . ' WHERE `Index` = ' . $key;
    mysqli_query($sql);
}

3 Comments

What's the point of creating $data_db when the other two arrays exist?
It's a visual aid. And why use two if you can combine them and make it more readable and manageble?
It's an extra, unnecessary step. Added readability is debatably, especially with a proper variable name, e.g. $indexes. If you're going to suggest it, you should provide code to create it.
0

Assuming these are value pairs, i.e. $ergebnis[0] is for $Index[0] and so forth.

foreach ($ergebnis as $key => $value) {
    $sql = 'UPDATE lm_Artikel SET Bestand=' . (int)$value . ' WHERE `Index` = ' . (int)$Index[$key];
    // execute query...
}

A few notes:

  • You are open to SQL Injection. I used (int) as a quick patch.
  • I would encourage you to look into Prepared Statements.
  • You should avoid naming your columns SQL keywords, e.g. Index.

1 Comment

Technically their openness to SQL Injection is debatable. We don't know how those arrays are being generated. If it isn't user submitted, they could be safe. Regardless, prepared statements is probably a better way to go.

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.