【发布时间】:2011-08-04 11:13:09
【问题描述】:
我有一个 PHP 引擎页面,它处理在具有可变行的表单上输入的数据。 PHP 生成一封电子邮件,还应该将数据输入到已经创建的 MySQL 表中。电子邮件工作正常,但由于某种原因,我的 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>
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\">";
}
?>
谁能发现为什么数据没有被发送到 MySQL 数据库?
谢谢,
尼克
当前代码:
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());
}
【问题讨论】:
-
您是否正在获取 $_POST['name'][$i] 的值
-
您实际在哪里运行查询?我没看到。
-
另外,您不要使用
stripslashes()来清理输入。当您从数据库中提取数据时使用它。请改用mysq_real_escape_string()。 -
您的问题是:“无法连接到 MySQL 数据库”。您是否在任何地方收到此错误?或者你的问题应该是:“为什么数据没有插入到数据库中?”
-
谢谢。是的,我实际上并没有运行查询。现在我正在运行它们,我收到错误:“无效查询:'字段列表'中的未知列 'asdasdas'”,其中 'asdasdas' 是我在名称字段中输入的内容。