I have multiple forms that I am trying to insert data from into MySql. Each form has a different table associated with that information. I am using an insert.php file to insert the data. The first form works great. However when I add any other variables to the initial file it errors out. This is my insert file:
<?php # NAME OF PROGRAM GOES HERE
require_once 'config.php';
// Get values from form
$Fname = $_POST['first_name'];
$email = $_POST['email'];
// Insert data into mysql
$sql="INSERT INTO entry (first_name, email)
VALUES ('$Fname', '$email')";
$result = mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful Entry";
}
else {
echo "ERROR";
}
mysql_close();
?>
This is the first form:
<form action="insert.php" method="post" name="admin" onSubmit="return validateForm()">
<p>Please enter First Name <input type="text" name="first_name" required="required"/>
<p>Please enter your Email<input type="text" name="email" required="required" ><br />
<input type="submit" name="submit" value="Submit" />
</form>
The second form is:
<form action="insert.php" method="post" onSubmit="return validateForm()">
<p>Favorite Winery Name <input type="text" name="fav_winery" /> <br />
<p>Favorite White Wine<input type="type" name="fav_white" /> <br />
<p>Favorite Red Wine <input type="type" name="fav_red" /> <br /><br />
<input type="submit" name="submit" value="Submit" />
</form>
How do I INSERT the information from the second form into another table already created?
I would suggestion adding a hidden variable to each form for their action and then do a if statement in insert.php
:
if($_GET['action'] == 'insertName'){
##insert code and form validation goes here
}elseif($_GET['action'] == 'insertFav'){
##insert code and form validation goes here
}
and add hidden inputs to the forms change ACTION-NAME to something to differentiate between forms:
<input type="hidden" name="action" value="insert*ACTION-NAME*" />
How do I INSERT the information from the second form into another table already created?
Have a hidden variable in each form with the table name...
<input type="hidden" id+"tableName" value="entry" />
Your PHP script will need to be smart enough to know what each table expects. You either do not specify the expression list in the left hand side of the INSERT statement:
INSERT INTO entry VALUES (...)
instead of
INSERT INTO entry (col1, col2) VALUES (...)
Or you have if checks to set the expression list based on the table name. This is a maintenance issue:
if($tableName == "entry")
{
$columns = "(first_name, email)";
}
Then use $columns in your insert query:
INSERT INTO $tableName $columns VALUES (...)
There are several ways you can do this.
Have the different forms submit to a different script. So you could have an insert_user.php
script for the first form, insert_winery.php
for the second form.
Use a hidden input field to distinguish the forms, as in Ahouri Ghotbi's answer.
Use the value of the submit
field to distinguish them. So your script can do if ($_POST['submit'] == 'user') ...
.
Use a URL parameter in the action attribute: action="insert.php?form=user"
. The script can then do if ($_GET['form'] == 'user') ...
.
All you need to do is that, use a unique hidden field in each form that you check before processing the form.
For example:
<form action="process.php" method="post">
<input type="hidden" name="formType" value="1">
<!-- Other input items here -->
<input type="submit" value="Submit">
</form>
<form action="process.php" method="post">
<input type="hidden" name="formType" value="2">
<!-- Other input items here -->
<input type="submit" value="Submit">
</form>
Now for processing of multiple form, you might do something like the following in process.php:
<?php
if (isset($_POST['submit'])) {
if ($_POST['formType'] == 1) {
// Processing for form 1
} else if ($_POST['formType'] == 2) {
// Processing for form 2
}
}
?>
Maybe you got the idea. Thanks