michaelsanford
Translator, Web Developer
I've got a table of 44 columns and I don't particularly feel like hard-coding the INSERT statement. So, instead, I've done this:
My HTML form's id= and name= tags are all identical to the table's columns names, including two type="hidden" fields.
I've done all the text-based tests I can think of, including parsing out the entire query and looking for extra `` or commas at the end or in the middle and I can't find any. I even outputted $i and I'm outputting 44 iterations which is the number of columns.
Where the heck, then, does this error come from ?
There was an error: Unknown column '' in 'field list'
I also get that error if I reformat the query like this:
<font face="Courier New">
What's going on ?
PHP:
mysql_select_db('science', $dblink) or
die("Select database error: ". mysql_error($link));
$result = mysql_query("SELECT * FROM `garden_data`", $dblink) or
die("Can't select" . mysql_error($dblink));
$dbdata = NULL;
$dbfields = NULL;
for ($i = 0; $i < mysql_num_fields($result); $i++) {
$meta = mysql_fetch_field($result, $i);
$fieldname = $meta->name;
// Parse the fields array
$dbfields .= $fieldname;
if ( $i < (mysql_num_fields($result)-1)) $dbfields .= ", ";
// Parse the data array
$dbdata .= "`" . $_POST[$fieldname];
if ( $i < (mysql_num_fields($result)-1)) $dbdata .= "`, ";
else { $dbdata .= "`"; }
//echo $i . "<br />";
}
$query = "INSERT INTO `garden_data` (" . $dbfields . ") VALUES (" . $dbdata . ")";
My HTML form's id= and name= tags are all identical to the table's columns names, including two type="hidden" fields.
I've done all the text-based tests I can think of, including parsing out the entire query and looking for extra `` or commas at the end or in the middle and I can't find any. I even outputted $i and I'm outputting 44 iterations which is the number of columns.
Where the heck, then, does this error come from ?
There was an error: Unknown column '' in 'field list'
I also get that error if I reformat the query like this:
<font face="Courier New">
PHP:
$query = "INSERT INTO `garden_data` VALUES (" . $dbdata . ")";
What's going on ?