automated my-sql-data insertion???

is there a way to hand over a varying number of arguments to a function, which then inserts these arguments into a mysql-table?

the call would be something like:

insertNewRecord(‘name_of_field_1’, $content_to_write_1, ‘name_of_field_2’, $content_to_write_2);

and the function:

function insertNewRecord() {
$num_args = func_num_args();
$args = func_get_args();

for ($n = 0; $n <= $num_args; $n = $n + 2) {

   $name_of_field = $args[$n];
       $content_to_write = $args[$n+1];
   mysql_query("INSERT INTO table_name ($name_of_field) VALUES ('$content_to_write')",  connectionID)
		or die ("Schreiben des Eintrags in die Datenbank nicht m?glich.");



what happens is, that the for-command, logically, creates a new entry into the database with every loop.

what i need, instead, is a code that gives the same result, as

mysql_query(“INSERT INTO table_name ($name_of_field_1, $name_of_field_2 … n) VALUES (’$content_to_write_1’, ‘$content_to_write_2’ … n)”, connectionID)

would do.

thank you very much in advance for any helpful idea.
cheers, andre

I would solve this by entering an array of values:

function insertRecord($myFields, $myValues) {
  $sql = "INSER INTO table_name (".implode(", ", $myFields).") VALUES ('".implode(", ", $myValues)."')";
  mysql_query($sql) or die(mysql_error());

And then just run the function multiple times for multiple records.

smashing… great!

now, just one little refinement to shorten the code:

imagine, the field’s name is the same as the name of the var containing the content, e.g.


is there any way i can change the functions code in such a way that i only have to pass the names once, and then use them as the field’s and var’s names?

cheers, andre

No, that’s not possible, as variable names are not carried over into the function, only the values they contain.

thank you… but still one question :-)

it is about reading the content of a table. this is the old code that now has to be generalised, so that it works with arrays of variable sizes:

$result = mysql_query(“SELECT * FROM $table WHERE id = $id”, $connectionID)
$aaa = mysql_result($result, 0, “aaa”);
$bbb = mysql_result($result, 0, “bbb”);

is has to be transformed somehow into this direction (which doesnt work):

foreach($db as $field => $value) {
$sql = ‘$result, 0, "’.$field.’"’;
$value = mysql_result($sql);

the final aim is the definition of an array in the beginning of the document, representing the fields and values of the table. then i can call the 3 different functions to update (solved, see below), insert (solved, see below) and read.

$db = array(‘aaa’ => $_POST[‘aaa’], ‘bbb’ => $_POST[‘bbb’]);

function insertRecord($db) {
foreach($db as $field => $value) {
$fields .= $field.",";
$values .= “’”.$value."’,";

($fields) VALUES ($values)”;

$success = mysql_query($sql, $GLOBALS[connectionID]);

function updateRecord($db) {
SET ";
foreach($db as $field => $value) {
$sql .= “$field = ‘$value’,”;
$sql .= " WHERE id = ‘$GLOBALS[id]’ ";
$success = mysql_query($sql, $GLOBALS[connectionID])

Sponsor our Newsletter | Privacy Policy | Terms of Service