In response to Sergiu's function - implode() would make things a lot easier ... as below:
<?php
function mysql_insert_assoc ($my_table, $my_array) {
$columns = array_keys($my_array);
$values = array_values($my_array);
$sql = "insert into `$my_table` ";
$sql .= "(\"" . implode("\", \"", $column_names) . "\")";
$sql .= " values ";
$sql .= "(" . implode(", ", $values) . ")";
$result = mysql_query($sql);
if ($result)
{
echo "The row was added sucessfully";
return true;
}
else
{
echo ("The row was not added<br>The error was" . mysql_error());
return false;
}
}
?>
Thus, a call to this function of:
mysql_insert_assoc("tablename", array("col1"=>"val1", "col2"=>"val2"));
Sends the following sql query to mysql:
INSERT INTO `tablename` ("col1", "col2") VALUES ("val1", "val2")