简体   繁体   中英

Update an array php mysql

I'm trying to update in my database two array that depends on each others. I want to know if it is possible to use foreach() to update the data?

[reponse] => Array
        (
            [0] => reponse 1
            [1] => reponse 2
            [2] => reponse 3
        )

file.php

foreach ($reponse as $key=>$value) {

  $values= mysql_real_escape_string($value);    
  $valuesch= mysql_real_escape_string($chimp[$key]);
  $query2 = mysql_query("UPDATE  reponses  SET nom_reponse=$values,id_categorie='$categorie',correct='$valuesch' where id_question='$id_question' ")  or die(mysql_error());

}

if ($query2) {

  echo "<br><div class='alert alert-info alert-dismissable'><button aria-hidden='true' data-dismiss='alert' class='close' type='button'>×</button>";
  echo "Reponse Modifer avec succes!! ";
  echo "</div> ";

} else {
  echo " Erreur  reponse!! ";
} 

I want to know what is the problem in this code? And how to update it correctly?

after google and understanding how to use an array i find the solution , to update an array in database i call each value with his key this is the way :

[reponse] => Array
        (
            [0] => reponse A
            [1] => reponse B
            [2] => reponse C
        )

    [id_reponse] => Array
        (
            [0] => 19
            [1] => 20
            [2] => 21
        )


foreach ($reponse as $key=>$value) {
        $values= mysql_real_escape_string($value);
        $valuescheck= mysql_real_escape_string($chimp[$key]);
        $valuesidr= mysql_real_escape_string($id_reponse[$key]);



        $query2 = mysql_query("UPDATE  reponses  SET nom_reponse='$values',correct='$valuescheck' where id_reponse='$valuesidr'  ")  or die(mysql_error());



            }
        if ($query2) {

            echo "<br><div class='alert alert-info alert-dismissable'><button aria-hidden='true' data-dismiss='alert' class='close' type='button'>×</button>";
            echo "Reponse Modifer avec succes!! ";
            echo "</div> ";


        } else {
            echo " Erreur  reponse!! ";
        } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM