$array_values) $tmp[$key]=addslashes($array_values); $values = "'".implode(array_values($tmp), "','")."'"; # better $values = str_replace("'NULL'", 'NULL', $values); $query = 'INSERT INTO `'.$table.'` (`'.$fields.'`) VALUES ('.$values.');'; $res = mysql_query($query);// OR die(mysql_error()); if ($res!==false) { if (mysql_insert_id()>0) $res=mysql_insert_id(); else $res=true; } if ($debug) mysql_trace($query, $res); return $res; //-- Example of usage //$tToAdd = array('id'=>3, 'name'=>'Yo', 'salary' => 5000); //insertIntoDB('myTable', $tToAdd) } function mysql_update($table, $update, $where, $debug=false){ $fields = array_keys($update); $values = array_values($update); $i=0; $query='UPDATE `'.$table.'` SET '; while($fields[$i]){ if($i>0){$query.=', ';} $query.=' `'.$fields[$i]."`='".addslashes($values[$i])."'"; $i++; } $query = str_replace("'NULL'", 'NULL', $query); $query.=' WHERE '.$where.' LIMIT 1;'; if ($debug) mysql_trace($query, mysql_errno()); mysql_query($query);// or die(mysql_error()); return true; //Example // mysql_update('myTable', $anarray, "type = 'main'") } function mysql_select($table, $fields, $where, $debug=false, $assoc=MYSQL_BOTH) { $query="SELECT $fields FROM $table WHERE $where;"; // if ($debug) mysql_trace($query, count($tab)); $result=mysql_query($query) or die(mysql_error()); $tab=array(); while ($ligne = mysql_fetch_array($result, $assoc)) $tab[]=$ligne; mysql_free_result($result); if ($debug) mysql_trace($query, count($tab)); // echo $query; return $tab; } ?>