0

I have multiple array queries, but I want to write a code where I can convert al the queries into 1 long query.

if(in_array($_POST[$veldnaam],$idarr))
{
   $queries[] = "UPDATE ".$dba."config SET value='".$_POST[$veldnaam]."' WHERE id='".$idarr[$veldnaam]."' AND code='".$CONFIG['site_id']."')"; 
}   
else 
{
   $queries[] = "INSERT INTO ".$dba."config (`code`,`key`,`value`) VALUES ('".$CONFIG['site_id']."','".$veldnaam."','".$_POST[$veldnaam]."')" ; 
}

}
else
{
}   
}
else
{
   $queries[] = "INSERT INTO ".$dba."config (`code`,`key`,`value`) VALUES ('".$CONFIG['site_id']."','".$veldnaam."','".$_POST[$veldnaam]."')";
            }
        }
    }
// convert queries into 1 long query
Raptor
  • 53,206
  • 45
  • 230
  • 366
Munlau Yau
  • 21
  • 6
  • 2
    Do you really need one query? You can also just foreach through the array and query these then. – Jordy Mar 20 '15 at 09:29
  • 1
    You can execute multiple updates or inserts in the same query. It shouldn't be an issues. However your code is not complete here. You should also consider using prepared statements. As it stands your code is pretty vulnerable to sql injection. – Andrei P. Mar 20 '15 at 09:32
  • Check my answer in this link :- https://stackoverflow.com/questions/28958844/how-to-i-execute-this-two-query-in-php/28958924#28958924. hope it help you. – Alive to die - Anant Mar 20 '15 at 09:32
  • code formatting , still have code errors – Raptor Mar 20 '15 at 10:05
  • @warn: mysql injection detected ;) Also when combining queries you need the ending `;` to tell sql it's the end of the query. – Luceos Mar 20 '15 at 10:10
  • Why not something like `$db->multi_query( implode(";", $queries) );` See: http://stackoverflow.com/a/6461110/3000179 – ʰᵈˑ Mar 20 '15 at 10:45

0 Answers0