I am trying to use one submission page to submit form data to various tables. I want to be able to use a text field or a select/menu in a form to determine which table the data will be submitted to. I have tables named men, women, youth, special_events, etc. All the tables have the same structure newsID, title, description, and date.
Also, you might want to wrap the table name in back-ticks, and escape the input variable just in case:
PHP Code:
"INSERT INTO `" . mysql_real_escape_string($news_table) . "` (newsID,..."
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
I remembered the spaces after I posted, that didn't help any...
I keep getting this error
HTML Code:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(newsID, title, `description`, `date`) VALUES (NULL, 'Test #1', 'This is a test ' at line 1
echo out the variable so you can see exactly what ended up in it.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
I figured it out...duh, and i'll explain it for those who maybe new or don't get it
PHP Code:
$_POST['table'] = $news_table;
$_POST['table'] is the value of whatever my text field with the name of "table".
$news_table is a variable just created, so it has no value.
PHP works left to right, up to down, like any normal code.
Well when you think about the statement above, instead of "is equal to", it actually means "is now equal to", and since $news_table is a blank, I have now set my value from "table" to nothing, hence the nulled (blank) output.
PHP Code:
$news_table = $_POST['table']
Now the $news_table variable "is now equal to" $_POST['table'], making the $news_table variable have the value of "table"
Solved.
PS> Have no idea why I just explained it, but sometimes we just get lost in code...
Bookmarks