两种形式之一是更新数据库而不是(PHP)

时间:2017-11-03 10:30:02

标签: php mysql database forms

我在一个网页上有两个表单 - 订阅简报和联系表单。我使用了类似的模式来将用户的输入提交给数据库。最重要的是使用validate.js我遇到了以下问题:

  • 简报订阅 - 向数据库提交输入并显示感谢信息,但validation.js停止工作,所以即使不是电子邮件也基本上提交。

  • 联系表单 - 提交后显示感谢信息,必须连接到数据库,但必须提交空白字段。

时事通讯提交表格:

<form novalidate type="" action="http://local.project/thankyou_newsletter.php?form=email" method="post" class="newsletter__form-js">

联系表格:

<form novalidate type="" name="contact_form" class="contact__form__wrapper" method="post" action="http://local.project/thankyou_contact.php?form=email">

thankyou_newsletter.php

<?php

require 'connection.php';
$conn    = Connect();
$email   = $conn->real_escape_string($_POST['email']);
$query   = "INSERT into newsletter (email) VALUES('" . $email . "')";
$success = $conn->query($query);

if (!$success) {
    die("Couldn't enter data: ".$conn->error);

}

echo "Thank you for subscribing to our newsletter. <br>";

$conn->close();

?>

thankyou_contact.php

<?php

require 'connection.php';
$conn    = Connect();
$name    = $conn->real_escape_string($_POST['name']);
$email   = $conn->real_escape_string($_POST['email']);
$message = $conn->real_escape_string($_POST['message']);
$query   = "INSERT into contactForm (name, email, message) VALUES('" . $name . "','" . $email . "','" . $message . "')";
$success = $conn->query($query);

if (!$success) {
    die("Couldn't enter data: ".$conn->error);

}

echo "Thank you for your message. We will get back to you shortly. <br>";

$conn->close();

?>

connection.php

<?php

function Connect()
{
 $dbhost = "localhost";
 $dbuser = "root";
 $dbpass = "root";
 $dbname = "responses";

 // Create connection
 $conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname) or die($conn->connect_error);

 return $conn;
}

?>

1 个答案:

答案 0 :(得分:0)

好的,问题出在我设置的数据库中。我是在phpMyAdmin中进行的,并且没有将字段类型更改为&#39; varchar&#39;。