Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hi, I want the data that is ent

Status
Not open for further replies.

blizz

Programmer
Oct 4, 2002
58
US
Hi,

I want the data that is entered in my form to be submitted to the table that is selected in the drop down menu. How do I use php to use $dropdownselection as the table selected in the drop down menu and this is the the table to submit the form data too. Hope I explained my question well enough.


<?
$usr = &quot;me&quot;;
$pwd = &quot;mine&quot;;
$db = &quot;dnc&quot;;
$host = &quot;localhost&quot;;

# connect to database
$cid = mysql_connect($host,$usr,$pwd);
mysql_select_db($db);
if (mysql_error()) { print &quot;Database ERROR: &quot; . mysql_error(); }

?>

<html>
<head>
<title>Insert New NC Job</title>
</head>
<body bgcolor=&quot;#ffffff&quot;>

<h2> Add New Job </h2>

<?
# this is processed when the form is submitted
# back on to this page (POST METHOD)
if ($_SERVER['REQUEST_METHOD'] == &quot;POST&quot;)
{
# escape data and set variables
$PART_NUMBER = addslashes($_POST[&quot;PART_NUMBER&quot;]);
$MACHINE = addslashes($_POST[&quot;MACHINE&quot;]);
$NEED_DATE = addslashes($_POST[&quot;NEED_DATE&quot;]);
$NOTES = addslashes($_POST[&quot;NOTES&quot;]);

# setup SQL statement
$sql = &quot; INSERT INTO $dropdownselection &quot;;
$sql .= &quot; (PART_NUMBER, MACHINE, NEED_DATE, NOTES) VALUES &quot;;
$sql .= &quot; ('$PART_NUMBER','$MACHINE','$NEED_DATE','$NOTES') &quot;;

#execute SQL statement
$result = mysql_query($sql, $cid);

# check for error
if (mysql_error()) { print &quot;Database ERROR: &quot; . mysql_error(); }

print &quot;<h3><font color=red>New Job Added</font></h3>&quot;;
}

?>

<form name=&quot;fa&quot; action=&quot;record-add.php&quot; method=&quot;POST&quot;>

<table>
<tr><td><b>Project Name:</b></td><td>
<?
echo &quot;<select name='dropdownselection'>&quot;;
$result = mysql_list_tables($db);
while($row = mysql_fetch_row($result)){
echo&quot;<option value='$row[0]<br>\n'>$row[0]<br>\n&quot;;
}
echo &quot;</select>&quot;;
?></td></tr>

<tr><td><b>Part Number: </b> </td><td><input type=&quot;text&quot; name=&quot;PART_NUMBER&quot; size=40></td></tr>
<tr><td><b>Machine: </b> </td><td><input type=&quot;text&quot; name=&quot;MACHINE&quot; size=40></td></tr>
<tr><td><b>Need Date: </b> </td><td><input type=&quot;text&quot; name=&quot;NEED_DATE&quot; size=20></td></tr>
<tr><td valign=top><b>Notes: </b> </td><td> <textarea name=&quot;NOTES&quot; rows=5 cols=40></textarea></td></tr>
<tr><th colspan=2><p><input type=&quot;submit&quot; value=&quot;Add Job&quot;></p></th></tr>
</table>
</form>


</body>
</html>

Blizz
 
First, I'm just learning php. I have 9 tables in db. I currently have nine buttons on the page to add a new record. One for each table. I thought it would be nice to populate the drop down menu with the tables. So when the user selects a table from the drop down menu the form submits the data to this table.

I have this piece of code that populates the drop down menu with the tables:

echo &quot;<select name='dropdownselection'>&quot;;
$result = mysql_list_tables($db);
while($row = mysql_fetch_row($result)){
echo&quot;<option value='$row[0]<br>\n'>$row[0]<br>\n&quot;;
}
echo &quot;</select>&quot;;

The problem is how do I change this line -
$sql = &quot; INSERT INTO $dropdownselection &quot;;

So that the form uses the table selected in the drop down menu for the data insert line.






Blizz
 
The line you have or

sql = &quot; INSERT INTO &quot; . $_POST['dropdownselection]' . &quot; &quot;;

should do it. Does it not work?

However, I am curious about your schema. Why have 9 identical tables? Would 1 table with an extra identifier column do it?

Want the best answers? Ask the best questions: TANSTAAFL!!
 
Thanks for trying to help. They are not identical tables but the do all contain these particular fields.

NO it does not work. I'm thinking this line:

$sql = &quot; INSERT INTO $dropdownselection &quot;;

I guess it does not work because $dropdownselection would have to defined as a variable that is selected in the drop down menu. $dropdownselection is not a real table name it is representing the name of the table that's selected in the drop down. So when the user submits the form php needs to know that the table to insert the data into is the table name the user selected in the drop down menu.

hope i'm explaining the issue well enough...



Blizz
 
Actually, no, you're not explaining yourself well. Your lack of punctuation leaves a lot of ambiguity in your posts.


With the <SELECT> and <OPTION> tags you've shown in your code snippet, the values of the options will be the names of tables. If a table is selected and the form is submitted, then the superglobal array element $_POST['dropdownselection'] will have the value of the option that was selected at the time the form was submitted. That should be the name of a table.

Output the statement produced and look at it. Does it look right?

Want the best answers? Ask the best questions: TANSTAAFL!!
 
sleipnir214 ~

I tried your suggestion and replaced this line:
$sql = &quot; INSERT INTO $dropdownselection &quot;;

with yours

$sql = &quot; INSERT INTO &quot; . $_POST['dropdownselection'] . &quot; &quot;;

But I still get the same error:
Database ERROR: 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 '
(PART_NUMBER, MACHINE, NEED_DATE, NOTES) VALUES

The code as it stands now -
<?
$usr = &quot;me&quot;;
$pwd = &quot;mine&quot;;
$db = &quot;dnc&quot;;
$host = &quot;localhost&quot;;

# connect to database
$cid = mysql_connect($host,$usr,$pwd);
mysql_select_db($db);
if (mysql_error()) { print &quot;Database ERROR: &quot; . mysql_error(); }

?>

<html>
<head>
<title>Insert New NC Job</title>
</head>
<body bgcolor=&quot;#ffffff&quot;>

<h2> Add New Job </h2>

<?
# this is processed when the form is submitted
# back on to this page (POST METHOD)
if ($_SERVER['REQUEST_METHOD'] == &quot;POST&quot;)
{
# escape data and set variables
$PART_NUMBER = addslashes($_POST&quot;PART_NUMBER&quot;);
$MACHINE = addslashes($_POST&quot;MACHINE&quot;);
$NEED_DATE = addslashes($_POST&quot;NEED_DATE&quot;);
$NOTES = addslashes($_POST&quot;NOTES&quot;);

# setup SQL statement
$sql = &quot; INSERT INTO &quot; . $_POST['dropdownselection'] . &quot; &quot;;
$sql .= &quot; (PART_NUMBER, MACHINE, NEED_DATE, NOTES)VALUES &quot;;
$sql .= &quot; '$PART_NUMBER','$MACHINE','$NEED_DATE','$NOTES') &quot;;

#execute SQL statement
$result = mysql_query($sql, $cid);

# check for error
if (mysql_error()) { print &quot;Database ERROR: &quot; . mysql_error(); }

print &quot;<h3><font color=red>New Job Added</font></h3>&quot;;
}

?>

<form name=&quot;fa&quot; action=&quot;record-add.php&quot; method=&quot;POST&quot;>

<table>
<tr><td><b>Project Name:</b></td><td>
<?
echo &quot;<select name='dropdownselection'>&quot;;
$result = mysql_list_tables($db);
while($row = mysql_fetch_row($result)){
echo&quot;<option value='$row[0]<br>\n'>$row[0]<br>\n&quot;;
}
echo &quot;</select>&quot;;
?></td></tr>

<tr><td><b>Part Number: </b> </td><td><input type=&quot;text&quot; name=&quot;PART_NUMBER&quot; size=40></td></tr>
<tr><td><b>Machine: </b> </td><td><input type=&quot;text&quot; name=&quot;MACHINE&quot; size=40></td></tr>
<tr><td><b>Need Date: </b> </td><td><input type=&quot;text&quot; name=&quot;NEED_DATE&quot; size=20></td></tr>
<tr><td valign=top><b>Notes: </b> </td><td> <textarea name=&quot;NOTES&quot; rows=5 cols=40></textarea></td></tr>
<tr><th colspan=2><p><input type=&quot;submit&quot; value=&quot;Add Job&quot;></p></th></tr>
</table>
</form>
</body>
</html>

Thanks



Blizz
 
You are missing some bits of your SQL:

# setup SQL statement
$sql = &quot; INSERT INTO &quot; . $_POST['dropdownselection'] . &quot; &quot;;
$sql .= &quot; (PART_NUMBER, MACHINE, NEED_DATE, NOTES)VALUES &quot;;
$sql .= &quot; '$PART_NUMBER','$MACHINE','$NEED_DATE','$NOTES') &quot;;

should read:

# setup SQL statement
$sql = &quot; INSERT INTO &quot; . $_POST['dropdownselection'] . &quot; &quot;;
$sql .= &quot; (PART_NUMBER, MACHINE, NEED_DATE, NOTES) VALUES &quot;;
$sql .= &quot;('$PART_NUMBER','$MACHINE','$NEED_DATE','$NOTES') &quot;;

Hope this helps,

Phil Clare
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top