为什么我的数据没有提交到MySQL数据库?

时间:2011-08-04 11:13:09

标签: php mysql forms connect

我有一个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());
}

3 个答案:

答案 0 :(得分:3)

我猜你在设置插入语句后没有执行你的查询。

答案 1 :(得分:2)

缺少执行查询的mysql_query($query);或其他函数。您正在使用$query = ...构建查询,但之后不使用此变量。

您可以添加以下内容:

$result = mysql_query($query);
if (!$result) {
  die('Invalid query: ' . mysql_error());
}

有关详细信息,请参阅:http://www.php.net/manual/en/function.mysql-query.php

此外查询缺少某些值的撇号

您的密码:

 $values[] = '(' . $name . ',' . $email . ',' . $organisation . ',' . $position . ')';

应该是这样的:

 $values[] = '(\'' . $name . '\',\'' . $email . '\',\'' . $organisation . '\',\'' . $position . '\')';

 $values[] = "('" . $name . "','" . $email . "','" . $organisation . "','" . $position . "')";

否则,值将被解释为字段名称。

答案 2 :(得分:2)

以您的第一个查询值为例......

$query = "INSERT INTO conference (Name, Email, Organisation, Position) VALUES (" . $values . ")";

这实际上并不是在执行查询,只是简单地定义它。你必须通过mysql_query()像这样传递执行它的额外步骤......

mysql_query($query);

毋庸置疑,在将数据插入数据库之前,您应该考虑对数据进行消毒。祝你好运!

编辑:更改此行...

$values[] = '(' . $name . ',' . $email . ',' . $organisation . ',' . $position . ')';

对此。如您所见,您需要围绕各个值的引号,并且在此实例中不需要数组。只需使用一个字符串。您还可以从查询字符串中删除implode(),然后只引用该变量。

$values = "('" . $name . "','" . $email . "','" . $organisation . "','" . $position . "')";