I have a form in two sections on a webpage. Each section contains a variable number of rows (the user can add more rows). I have a PHP page which processes the form as an email, but I am now looking to extend this so that information is added to a My SQL database that I have in place. Below is the code for the form and the php processing page as it stands.
I have started to include the code for submitting the data to the MySQL table at the bottom of the PHP page, but I am not sure how to do this, given that there are a variable number of rows in two sections, and would be grateful for any help in getting this working.
Here is the HTML for the form:
<form method="post" name="booking" action="bookingengine.php">
<fieldset>
<h2>Waged/Organisation Rate</h2>
<p>
<input type="text" name="name[]">
<input type="text" name="email[]">
<input type="text" name="organisation[]">
<input type="text" name="position[]">
</p>
<p><span class="add">Add person</span></p>
</fieldset>
<fieldset>
<h2>Unwaged Rate</h2>
<p>
<input type="text" name="name2[]">
<input type="text" name="email2[]">
</p>
<p><span class="add">Add person</span></p>
</fieldset>
<p><input type="submit" name="submit" id="submit" value="Submit and proceed to payment page" class="submit-button" /></p>
</form>
And here is the booking engine.php:
<? include 'connection.php'; ?>
<?php
$emailFrom = "****";
$emailTo = "****";
$subject = "Booking for Soteria Conference";
$body = "The following people have booked for the Soteria Conference in Derby:" . "\n\n" . "Waged/Organisation Rate:" . "\n\n";
$row_count = count($_POST['name']);
$row_count2 = count($_POST['name2']);
for($i = 0; $i < $row_count; $i++)
{
// variable sanitation...
$name = trim(stripslashes($_POST['name'][$i]));
$email = trim(stripslashes($_POST['email'][$i]));
$organisation = trim(stripslashes($_POST['organisation'][$i]));
$position = trim(stripslashes($_POST['position'][$i]));
// this assumes name, email, and telephone are required & present in each element
// otherwise you will have spurious line breaks.
$body .= "Name: " . $name . " Email: " . $email . " Organisation: " . $organisation . " Position: " . $position . "\n\n";
}
$body .= "Unwaged Rate:" . "\n\n";
for($j = 0; $j < $row_count2; $j++)
{
// variable sanitation...
$name2 = trim(stripslashes($_POST['name2'][$j]));
$email2 = trim(stripslashes($_POST['email2'][$j]));
// this assumes name, email, and telephone are required & present in each element
// otherwise you will have spurious line breaks.
$body .= "Name: " . $name2 . " Email: " . $email2 . "\n\n";
}
// send email
$success = mail($emailTo, $subject, $body, "From: <$emailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=payment.html\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
Here is the structure of connection.php:
<?php
$hostname = "localhost";
$database = "****";
$username = "****";
$password = "****";
$conn = mysql_connect($hostname, $username, $password) or die ('Error connecting to mysql');
mysql_select_db($database);
?>
I’m not sure what you mean. But your HTML code should be more like this :
And then at the end of your PHP script
The same logic applies for Unwaged rates
edit if you want to keep your HTML