Dynamic PHP Functions
| From: | Joseph Blythe | Date: | Fri, 15 Sep 2000 07:07:03 +0000 |
| Subject: | Dynamic PHP Functions | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-16921@lists.php.net to get a copy of this message | ||
Hey thanks Matt :-)
here is what I have come up with so far but it does not work of course and no errors are generated,
can anyone see why?
<?php
$host = "localhost";
$user = "webuser";
$pass = "";
$database = "test";
$table = "test_table";
$link = mysql_connect($host, $user, $pass)
or die ("mysql_errno() : mysql_error()");
$fields = mysql_list_fields("$database", "$table", $link);
$columns = mysql_num_fields($fields);
for ($k = 0; $k < $columns; $k++)
{
$name = mysql_field_name($fields, $k);
$field_names = "array(";
for ($j = 0; $j < $columns; $j++)
{
$field_names .= '"' . $name . '"' . '=>' .
'"' . $$name . '"';
if ( $j < $columns -1 )
{
$field_names .= ',';
$field_names .= ')';
}
}
}
function insertdata($tablename,$fields_array)
{
$fieldcount = count($fields_array);
$sql = "INSERT INTO $tablename VALUES (";
for ($i = 0; $i < $fieldcount; $i++)
{
$sql .= '"' . $fields_array[$i] . '"';
if ($i < $fieldcount - 1)
{
$sql .= ',';
$sql .= ')';
}
}
mysql_query($sql);
}
insertdata($table,$field_names);
?>
What I am trying to achieve is getting the field names from the database then creating an array with
field_name => $field_name as the form has passed all the field names as its variable so field
Address would be $Address etc..
Then using the function insert data construct the mysql insert command.
Any help much appreciated,
Regards,
Joseph