为什么我的数据没有提交到MySQL数据库? [英] Why is my data not submitted to MySQL database?

查看:264
本文介绍了为什么我的数据没有提交到MySQL数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP引擎页面,该页面处理在具有可变行的表单上输入的数据。 PHP会生成一封电子邮件,还应该将数据输入到已经创建的MySQL表中。电子邮件工作正常,但是由于某种原因,没有数据输入到我的MySQL数据库中。我确定数据库和表的所有设置都是正确的。我也没有看到任何错误消息。我的代码包含以下内容:

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格式:

<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:

connection.php:

<?php

$hostname = "localhost";
$database = "****";
$username = "****";
$password = "****";
$connection = mysql_connect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR);

?>

bookingengine.php:

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数据库?

Can anyone spot why the data isn'y getting sent to the MySQL database?

谢谢

尼克

当前代码:

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());
}


推荐答案

mysql_query($ query); 或另一个执行查询的函数丢失。您正在使用 $ query = ... 来构建查询,但是随后就不用此变量。

The mysql_query($query); or another function which executes the query is missing. You are building your query with $query = ... but then you don't use this variable.

您可以添加如下内容:

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

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

For more Details see: 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 . "')";

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

Otherwise the values would be interpreted as field names.

这篇关于为什么我的数据没有提交到MySQL数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆