Dynamic Functions
| From: | Joseph Blythe | Date: | Mon, 18 Sep 2000 01:27:52 +0000 |
| Subject: | Dynamic Functions | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-17111@lists.php.net to get a copy of this message | ||
Hey,
I have revised the code slightly and now it insert all a's into the
database, I can't for the life of me figure out where it is getting them
from?
The array seems to be constructed alright and has the right key names and
values, but when I try to call array_keys($field_names); it says first
argument must be an array, I have tried the eval function on $field_names
but that seemed to do nothing??
<?php
// Connect to database and get fields
$link = mysql_connect($host, $user, $pass)
or die ("mysql_errno() : mysql_error()");
$fields = mysql_list_fields("$database", "$table", $link);
$columns = mysql_num_fields($fields);
// Create array with fieldname as key
// and data from the form on previous page
// variables are named $fieldname on form
$k = 0;
while ( $k < $columns )
{
$field_names = "array(";
for ($j = 0; $j < $columns; $j++)
{
$name = mysql_field_name($fields, $j);
$field_names .= '"' . $name . '"' . '=>' .
'"' . $$name . '"';
if ( $j < $columns -1 )
{
$field_names .= ',';
} else {
$field_names .= ');';
}
}
$k++;
}
// create mysql insert command and insert data
$sql = "INSERT INTO $table VALUES (";
for ($i = 0; $i < $columns; $i++)
{
$name = mysql_field_name($fields, $i);
$sql .= '"' . $field_names[$name] . '"';
if ($i < $columns - 1)
{
$sql .= ',';
} else {
$sql .= ');';
}
}
mysql_query($sql);
?>
If anyone can see the problem could they let me know, it would be mutch
appreciated.
Regards
Joseph