Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/241.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 为什么我的数据没有提交到MySQL数据库?_Php_Mysql_Forms_Connect - Fatal编程技术网

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

Php 为什么我的数据没有提交到MySQL数据库?,php,mysql,forms,connect,Php,Mysql,Forms,Connect,我有一个PHP引擎页面,它处理在具有可变行的表单上输入的数据。PHP生成一封电子邮件,还应该将数据输入已经创建的MySQL表中。电子邮件工作正常,但由于某些原因,我的MySQL数据库中没有输入任何数据。我确信数据库和表的所有设置都是正确的。我也没有看到任何错误消息。我的代码包括以下内容: html格式: <form method="post" name="booking" action="bookingengine.php"> <fieldset>

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

我猜您在设置insert语句后没有执行查询。

mysql\u query$query;或者缺少执行查询的另一个函数。您正在使用$query=。。。但是你不用这个变量

$values = "('" . $name . "','" . $email . "','" . $organisation . "','" . $position . "')";
您可以添加如下内容:

$result = mysql_query($query);
if (!$result) {
  die('Invalid query: ' . mysql_error());
}
有关更多详细信息,请参阅:

此外,查询中的值缺少一些撇号:

您的代码:

 $values[] = '(' . $name . ',' . $email . ',' . $organisation . ',' . $position . ')';
应该是这样的:

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


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

以您的第一个查询值为例

$query = "INSERT INTO conference (Name, Email, Organisation, Position) VALUES (" . $values . ")";
这实际上不是执行查询,而是定义查询。您必须采取额外的步骤执行它,通过mysql\u查询传递它,如下所示

mysql_query($query);
不用说,在将数据插入数据库之前,您可能应该考虑对数据进行消毒。祝你好运

编辑:更改此行

$values[] = '(' . $name . ',' . $email . ',' . $organisation . ',' . $position . ')';
对这个。如您所见,您需要在各个值周围加引号,在本例中不需要数组。只要用一根绳子。也可以从查询字符串中删除内爆,只引用变量

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

您是否获得了$\u POST['name'][$i]的值?您实际在哪里运行查询?我看不到。而且,您不使用斜杠来清理输入。从数据库中提取数据时使用它。请改用mysq\u real\u escape\u字符串。您的问题是:无法连接到MySQL数据库。你有没有发现这个错误?或者你的问题应该是:为什么数据没有插入数据库?谢谢。是的,我实际上没有运行查询。现在我正在运行它们,我得到了错误:无效查询:“字段列表”中的未知列“asdasdas”,其中“asdasdas”是我在名称字段中输入的。谢谢。我添加了这个,现在得到了错误:无效查询:“字段列表”中的未知列“asdasdas”,其中“asdasdas”是我在名称字段中输入的。不客气。另外,我想说的是,这段代码可能需要进一步的改进。尤其是使用仅使用一个值的array+内爆是不必要的。一个简单的字符串就可以做到这一点。我想我确实需要一个数组,因为我正在处理一个具有可变行数的表单。您正在//variable…下将POST数组转换为单独的变量,但是,你能告诉我为什么我只会得到我的表单的每个部分的最后一行提交给MySQL,以及我上面发布的当前代码吗?谢谢。我已经添加了这个,现在得到了错误:无效查询:“字段列表”中的未知列“asdasdas”,其中“asdasdas”是我在名称字段中输入的。谢谢。我试着改成一个字符串,现在我在内爆函数上得到一个错误,你也说过我应该删除它。我如何“引用变量”?对不起,我有点傻!只需将查询分配字符串中的内爆“,”和$values更改为$values。谢谢。我现在得到一个错误:无效查询:您的SQL语法有错误;检查与MySQL服务器版本相对应的手册,以了解在第1行的1’、‘2’、‘3’、‘4’附近使用的正确语法。我已将当前代码的相关部分张贴在我原始问题的末尾。错误消息中的1’、‘2’、‘3’、‘4将对应于我用于表单的虚拟值。