php - SQL Update/Insert with loop for $_GET
I'm using ajax scripts for most of my site's functionality. In order to update or insert a row in particular table i send the request with $_GET, then retrieve each value with
foreach ($_GET as $item => $value){
$$item =addslashes($value);
}
and then i have this SQL Statement for the query
if($_GET['action']=='update'){
$sql =$conn->query("UPDATE `newDrivers` SET `idCompany`='$idCompany', ...
WHERE `id`='$id'");
}
else{
$sql =$conn->query("
INSERT INTO `newDrivers`(`idCompany`, ...)
VALUES ('$idCompany', ...)");
}
I see inefficiency, naming each column and creating new file for each table i have. Is there any way i can update/insert without naming each column individually so the script would be more of a universal one?
Answer
Solution:
It's depends from your database adapter. In your case you may write construction like this