such as an array
$a =array ("a" = "1", "B" = "2", "C" and "3");
Now you want to execute the database INSERT statement INSERT INTO admin (a,b,c) values;
Reply content:
such as an array
$a =array ("a" = "1", "B" = "2", "C" and "3");
Now you want to execute the database INSERT statement INSERT INTO admin (a,b,c) values;
$keys;$value;$keys = implode(",", array_keys($a));$value = implode(",",array_values($a));$db->query("insert into admin(".$keys.") values(".$value.")")
Your question can be simplified to:
$a=array("a"=>"1","b"=>"2","c"=>"3");
Into the process of (' A ', ' B ', ' C ') and (all-in-one), you find a way to spell out the string. There are a number of methods, one of which can be:,
$a=array("a"=>"1","b"=>"2","c"=>"3");$values=implode(',',array_values($a));$keys="'".implode("','",array_keys($a))."'";$sql='insert into admin';$sql.='('.$keys.') ';$sql.='values ';$sql.='('.$values.') ';
Write a method
function insert($table,$data){ foreach($data as $k => $v){ $fields[] = $v; $keys[] = $k; } $values = "('".implode("','", $fields)."')"; $column = "(`".implode("`,`", $keys)."`)"; $sql = "insert into {$table} {$column} values {$values}"; $this->query($sql);}