gpt4 book ai didi

php - 订阅表单不会将信息发送到数据库

转载 作者:行者123 更新时间:2023-11-29 18:03:51 27 4
gpt4 key购买 nike

我有一个订阅表单,其中只有两个文本框,分别用于输入姓名和电子邮件。我希望这些信息存储在 MySql 数据库中。提交表单时,我可以连接到数据库,但信息未发送。这是表单的代码(只是感兴趣的部分):

<div class="jumbotron text-center">
<h1>Community Garden</h1>
<p>We are a local community that
grows organic vegetables and fruit</p>
<img src="Front.jpg" class="img-rounded" alt="front" > <br><br>
<form class="form-inline" action="news.php" method="post'>
<div class="input-group">
<input type="text" name="name" class="form-control" size="50" placeholder="Name" required>
<input type="text" name="email" class="form-control" size="50" placeholder="Email Address" required>
<br><br> <div class="input-group-btn">
<button type="submit" name="submit" value="submit" class="btn btn-danger">Subscribe</button>
</div>
</div>
</form>
</div>

这是用于数据库连接和信息存储的php脚本。该文件名为 news.php。非常感谢任何帮助。

<?php
//include database connection details
$username = "root";
$password = "password";
$hostname = "127.0.0.1";

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";

//select a database to work with
$selected = mysql_select_db("Community Garden List",$dbhandle)
or die("Could not select examples");

if(isset($_POST['submit'])){

//collect data from form and remove any tags and make safe for database entry
$name = strip_tags(mysql_real_escape_string($_POST['name']));
$email = strip_tags(mysql_real_escape_string($_POST['email']));

//check name lengh
if (strlen($name) < 3) {
$error[] = 'Name must be more then 3 characters.';
}

//if name is equal to name then give an error
if ($name == 'Name') {
$error[] = 'Please enter your name.';
}

// check for valid email address
$error = "";
$pattern = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if (!preg_match($pattern, trim($email))) {
$error[] = 'Please enter a valid email address';
}

// if validation is okay then carry on
if (!$error ) {

//insert into database
$sql = mysql_query("INSERT INTO newsletter (name,email) VALUES ('$name','$email')")or die(mysql_error());

//submission successful show a message
echo "<p>Thank you, submission was successful.</p>";

} // end validation
}// end submit

//show any errors
if (!empty($error))
{
$i = 0;
while ($i < count($error)){
echo '<div class="msg-error">".$error[$i]."</div>';
$i ++;}
}// close if empty errors

?>

最佳答案

莫非你的开头<form>标签格式错误?

具体方法部分:

 method="post'

看看开盘报价是双倍,但收盘报价是单倍。 W3C 验证器无法验证此类表单标记。考虑到这一点,浏览器可能会忽略此属性并使用默认方法(GET)提交表单。 (或者它可能只是完全忽略 <form> 。)

您可以在此处验证您的 HTML:http://validator.w3.org/

关于php - 订阅表单不会将信息发送到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48086280/

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