php - mysql insert question/problem.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pavaus
Forum Newbie
Posts: 1
Joined: Sat Sep 20, 2008 12:59 am

php - mysql insert question/problem.

Post 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.
Last edited by pavaus on Sat Sep 20, 2008 12:16 pm, edited 1 time in total.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: php - mysql insert question/problem.

Post 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.
Post Reply