I have a PHP engine page which processes the data entered on a form with variable rows. The PHP generates an email and should also enter the data into a MySQL table which is already created. The email is working fine, but for some reason no data is getting entered in my MySQL Database. I am sure that all of the settings for the database and table are correct. I am also not seeing any error messages. My code consists of the following:
html for 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>
connection.php:
<?php
$hostname = "localhost";
$database = "****";
$username = "****";
$password = "****";
$connection = mysql_connect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR);
?>
bookingengine.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']);
$values = array();
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";
//prepare the values for MySQL
$values[] = '(' . $name . ',' . $email . ',' . $organisation . ',' . $position . ')';
}
mysql_select_db($database, $connection);
$query = "INSERT INTO conference (Name, Email, Organisation, Position) VALUES " . implode(',', $values);
$values = array();
$body .= "Unwaged Rate:" . "\n\n";
for($i = 0; $i < $row_count; $i++) {
// variable sanitation...
$name = trim(stripslashes($_POST['name'][$i]));
$email = trim(stripslashes($_POST['email'][$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 . "\n\n";
//prepare the values for MySQL
$values[] = '(' . $name . ',' . $email . ')';
}
$query = "INSERT INTO conference (Name, Email) VALUES " . implode(',', $values);
// send email
$success = mail($emailTo, $subject, $body, "From: <$emailFrom>");
// redirect to success page
if ($success){
print "<meta http-equiv=\"refresh\" content=\"0;URL=/conference/payment.html\">";
}
else{
print "<meta http-equiv=\"refresh\" content=\"0;URL=error.htm\">";
}
?>
Can anyone spot why the data isn'y getting sent to the MySQL database?
Thanks,
Nick
Current Code:
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";
//prepare the values for MySQL
$values = "'" . $name . "','" . $email . "','" . $organisation . "','" . $position . "'";
}
mysql_select_db($database, $connection);
$query1 = "INSERT INTO conference (Name, Email, Organisation, Position) VALUES " . $values;
$result1 = mysql_query($query1);
if (!$result1) {
die('Invalid query: ' . mysql_error());
}