Forum Moderators: coopster

Message Too Old, No Replies

Inserting into one of 3 different tables problem

         

Ann_G

10:14 pm on Mar 9, 2005 (gmt 0)

10+ Year Member



I'm trying to insert data into one of three tables depending on which table is selected from a dropdown menu in a from. I tried several different approaches but the data goes into all three tables no matter what. As usual I appreciate any help I can get. Below is part of my script.

// Make the array
$bib_type = array ('mdbookclub, teen_bib, kids_bib');

// Check for a category.
if (($_POST['mdbookclub'] > 0)
OR ($_POST['teen_bib'] > 0) OR
($_POST['kids_bib'] > 0))
{
$bib_type = TRUE;
} else {
$bib_type = FALSE;
echo '<p><font color="red">Please select at least one category!</font></p>';
}

// Add the bib to the mdbookclub table.
if (is_array($_POST bib_type['mdbookclub'])) //if mother daughter bookclub selected

{
$query = "INSERT INTO mdbookclub (title, author, f_author, pub_year, annotation, call_number) VALUES (
'".mysql_escape_string($_POST['title'])."',
'".mysql_escape_string($_POST['author'])."',
'".mysql_escape_string($_POST['f_author'])."',
'".mysql_escape_string($_POST['pub_year'])."',
'".mysql_escape_string($_POST['annotation'])."',
'".mysql_escape_string($_POST['call_number'])."')";

if(!mysql_query($query) )

}

//Then I have the same for the other two tables substituting their table names.

The form is like this:
<p><strong>Please select a category:</strong>
<select name="bib_type">
<option value="mdbookclub">Mother Daughter Bookclub</option>
<option value="teen_bib">Teen Bibliography</option>
<option value="kids_bib">Kids Bibliography</option>
</select></p>

StupidScript

10:28 pm on Mar 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



if (is_array($_POST bib_type['mdbookclub']))

Almost ... :)

More like:

if ($_POST['bib_type']==='mdbookclub')

The form element being passed is 'bib_type'. It's value would be determined by the user selection.

Also, it's probably not a good idea to use a local variable with the same name as the form element ($bib_type). I'm not sure why you need that as an array to begin with.

How about:

if (!$_POST['bib_type']) {

 ... Select SOMEthing! ...

and

<select name="bib_type">

<option value=0 />Please choose ...

<option value="mdbookclub" />Moms & Daughters

etc.

If they don't change the default item, that form element's value becomes 'false' (hence if NOT $_POST['bib_type']).

If the three tables are identical, or at least the columns you are inserting this data into are identical, then you can use:

$this_table=$_POST['bib_type'];

...
INSERT INTO $this_table VALUES ...

Hope that helps!

Ann_G

4:53 pm on Mar 10, 2005 (gmt 0)

10+ Year Member



Thank you very much. You have been of great help. The three tables now receive the right data after I did what you told me. I did away with the array and used your suggestion.

if ($_POST['bib_type']=='mdbookclub')
followed by
if (!$_POST['bib_type']) {
my INSERT query ....

It works great. You saved my day.