gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 00:14:44 26 4
gpt4 key购买 nike

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

最佳答案

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 . "')";


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

关于php - 为什么我的数据没有提交到MySQL数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6940367/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com