Page 1 of 1

php - mysql insert question/problem.

Posted: Sat Sep 20, 2008 1:17 am
by pavaus
All i am trying to do is insert values into a table.The part which is confusing me is that i am trying to do it for any number of fields and not knowing what those fields might be called (if that makes sense).

I was wondering if anyone had a good way to do this.
I am using this snippet of code to get the headers of the table and print them on the screen with a input box for each.

code:
#include 'opendb.php';

$getFields = mysql_query("SHOW COLUMNS FROM table");
if(!$getFields) {
echo 'could not run query: ' . mysql_error();
exit;
}

if(mysql_num_rows($getFields) > 0) {
while($field = mysql_fetch_assoc($getFields)) {
echo $field["Field"];
echo '<input type="text" name="'.$field[Field].'" value="" />';
}
}

if($_POST) {
// not sure what should be here to catch and insert.
}


What do i put into the if($_POST) for the page to INSERT correctly. I have a submit button on the page which posts back to the same page.

Any help/advice would be great.

Just Solved it.
Made two arrays and did an insert.

Re: php - mysql insert question/problem.

Posted: Sat Sep 20, 2008 11:39 am
by califdon
I don't know what you are trying to accomplish with this, but it sounds inherently dangerous, because if you don't even know what fields are in a table, you presumably wouldn't know what data types they were, or even the table name????? Among many other questions, how could you validate the data to know if the correct data is going in the correct fields????

Maybe you have some very special situation there, but without knowing what that might be, it's hard for me to suggest anything.