Certainly, one of the very nice features of MySQL is the ability to send every value wrapped in quotes. For example, if you have a int type field, you can set a value of '1', for example, and MySQL converts it to an integer. This makes looping through an array of key/value pairs and dynamically writing SQL possible. Here's an example of how you might go about this.
<?php
class DataAutomator
{
public $pairs = array();
public $sql = '';
function __construct(){
/*** code to connect and initialize data connection goes here ***/
}
function makepair($key, $value){
$this->pairs[$key]=mysql_real_escape_string($value);
}
function fixupquery($table, $update=false){
$this->sql .= "INSERT INTO `$table` (`";
$this->sql .= implode("`,`", array_keys($this->pairs));
$this->sql .= "`) VALUES ('";
$this->sql .= implode("','", $this->pairs);
$this->sql .= "')";
if ($update) {
$count = 0;
$this->sql .= " ON DUPLICATE KEY UPDATE ";
foreach ($this->pairs as $key=>$value) {
$this->sql .= "`$key` = '$value'";
$this->sql .= ( ++$count < count($this->pairs) ) ? ", " : "";
}
}
}
function exec() {
/*** You implement this part. ***/
}
}
$d = new DataAutomator();
$d->makepair("column_name1", "some value");
$d->makepair("column_name2", 3);
/*** ...and so on... ***/
$d->fixupquery("table_name");
$d->exec();
?>