Code: Select all
//find out column names from db defined by mysql_list_fields()
$result_handle = mysql_list_fields ("some_db", $table_name) or die("mysql_list_fields () failed with this error message: '". mysql_error () . "'");
//determine total number of fields for table
$number_fields = mysql_num_fields ($result_handle);
//begin HTML table formatting
echo '<table width="100%" cellspacing="0" cellpadding="5" border="0">', "\n";
for ($index = 0; $index < $number_fields; ++$index) {
echo '<tr><td width="150"><b>', mysql_field_name ($result_handle, $index), '</b></td>';
echo '<td><input type="text" name="', mysql_field_name ($result_handle, $index), '" size="30"></td></tr>', "\n";
}I can successfully build the first part of the SQL statement up to here:
INSERT into table_name (field1_name, field2_name, field3_name) VALUES (
Following is the code I'm using to build the INSERT statement:
Code: Select all
$sql = "INSERT INTO '$table_name' (";
//find out column names from db defined by mysql_list_fields() query
$result_handle = mysql_list_fields ("some_db", $table_name) or die("mysql_list_fields () failed with this error message: '". mysql_error () . "'");
//determine total number of fields for table
$number_fields = mysql_num_fields ($result_handle);
//loop through column-field names and add to SQL statement
for ($index = 0; $index < $number_fields; ++$index) {
$sql .= "mysql_field_name ($result_handle, $index),";
}
//remove trailing comma
$sql .= substr($sql, 0, -1);
//add closing parentheses and begin VALUES part of statement
$sql .= ") VALUES (";Thanks in advance for any assistance or advice.