【发布时间】:2011-08-03 23:01:30
【问题描述】:
我在网页上的两个部分中有一个表单。每个部分包含可变数量的行(用户可以添加更多行)。我有一个 PHP 页面,它将表单作为电子邮件处理,但我现在正在寻求扩展它,以便将信息添加到我拥有的 My SQL 数据库中。下面是表单和php处理页面的代码。
我已经开始在 PHP 页面底部包含用于将数据提交到 MySQL 表的代码,但我不确定如何执行此操作,因为两个部分中的行数是可变的,并且将不胜感激任何帮助,使这项工作。
这是表单的 HTML:
<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>
这里是预订引擎.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\">";
}
?>
下面是connection.php的结构:
<?php
$hostname = "localhost";
$database = "****";
$username = "****";
$password = "****";
$conn = mysql_connect($hostname, $username, $password) or die ('Error connecting to mysql');
mysql_select_db($database);
?>
【问题讨论】: