www.webdeveloper.com
Results 1 to 7 of 7

Thread: [RESOLVED] Using a text field to determine table?

  1. #1
    Join Date
    Dec 2011
    Posts
    163

    resolved [RESOLVED] Using a text field to determine table?

    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.

    This code
    PHP Code:
    $insertSQL sprintf("INSERT INTO men (newsID, title, `description`, `date`) VALUES (%s, %s, %s, %s)",
                           
    GetSQLValueString($_POST['newsID'], "int"),
                           
    GetSQLValueString($_POST['title'], "text"),
                           
    GetSQLValueString($_POST['description'], "text"),
                           
    GetSQLValueString($_POST['date'], "text")); 
    I want to change to something like this...
    PHP Code:
    $_POST['table'] = $news_table 
    PHP Code:
    $insertSQL sprintf("INSERT INTO" $news_table "(newsID, title, `description`, `date`) VALUES (%s, %s, %s, %s)",
                           
    GetSQLValueString($_POST['newsID'], "int"),
                           
    GetSQLValueString($_POST['title'], "text"),
                           
    GetSQLValueString($_POST['description'], "text"),
                           
    GetSQLValueString($_POST['date'], "text")); 
    But I can't seem to get php to work this way, is there another alternative?

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,225
    Don't forget you need spaces:
    PHP Code:
    "INSERT INTO " $news_table " (newsID,..."
    //          |                   | 
    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

    eBookworm.us

  3. #3
    Join Date
    Dec 2011
    Posts
    163
    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

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,225
    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

    eBookworm.us

  5. #5
    Join Date
    Dec 2011
    Posts
    163
    blank...that's wierd...

    PHP Code:
    if (isset($_POST['table']))
    {
    $_POST['table'] = $news_table;
    echo 
    $news_table;
    die;} 
    Blank page...ok now I'm confused...
    Last edited by Dragonfire2008; 04-03-2012 at 01:13 PM.

  6. #6
    Join Date
    Dec 2011
    Posts
    163
    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...

  7. #7
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by Dragonfire2008 View Post
    blank...that's wierd...

    PHP Code:
    if (isset($_POST['table']))
    {
    $_POST['table'] = $news_table;
    echo 
    $news_table;
    die;} 
    Blank page...ok now I'm confused...
    Looks like you got your assignment backwards?

    $var1 = $var2 assigns the value of $var2 to $var1....

    PHP Code:
    $news_table $_POST['table'];
    echo 
    $news_table

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles