我的问题太长了,无法找到答案。请原谅我。也许我只是不知道如何缩写我的问题。那么这就是问题和问题。请参阅以下代码:
<?php
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$server = mysqli_connect('localhost', 'root', '', 'newsletter') or die("The Server Cannot Be Reached at this Time!");
//$dbc = mysqli_select_db($server, 'newsletter') or die("There is a problem accessing the databse!");
$query = mysqli_query($server, "INSERT INTO subscribers (name, email) VALUES('$name','$email')");
$name = strip_tags($name);
$email = strip_tags($email);
if(empty($name) || empty($email)) {
echo "All Fields Must Be Filled In!";
} else {
echo "You have been successfully subscribed";
}
}
mysqli_close($server);
?>
此代码有效。当用户填写表单时,它会将信息提供给数据库。我注意到,当用户只填写一个字段,即名称并将电子邮件字段留空时,if语句将返回 “所有字段必须填写” 行。然后你返回页面并填写电子邮件字段,一切都很好。但是当我检查数据库时,我注意到 电子邮件 在表格上没有填写“strong”。这是正常的吗?或者我做错了什么。感谢
答案 0 :(得分:3)
您在插入后正在检查empty
。将插入代码移动到else部分
<?php
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$name = strip_tags($name);
$email = strip_tags($email);
if(empty($name) || empty($email)) {
echo "All Fields Must Be Filled In!";
} else {
$server = mysqli_connect('localhost', 'root', '', 'newsletter') or die("The Server Cannot Be Reached at this Time!");
//$dbc = mysqli_select_db($server, 'newsletter') or die("There is a problem accessing the databse!");
$query = mysqli_query($server, "INSERT INTO subscribers (name, email) VALUES('$name','$email')");
mysqli_close($server);
echo "You have been successfully subscribed";
}
}
?>
答案 1 :(得分:2)
您希望将用户的插入移动到if语句的成功分支中:
<?php
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$name = strip_tags($name);
$email = strip_tags($email);
if(empty($name) || empty($email)) {
echo "All Fields Must Be Filled In!";
} else {
$server = mysqli_connect('localhost', 'root', '', 'newsletter') or die("The Server Cannot Be Reached at this Time!");
//$dbc = mysqli_select_db($server, 'newsletter') or die("There is a problem accessing the databse!");
$query = mysqli_query($server, "INSERT INTO subscribers (name, email) VALUES('$name','$email')");
mysqli_close($server);
echo "You have been successfully subscribed";
}
}
?>
答案 2 :(得分:0)
更改此
if(empty($name) || empty($email))
进入这个
if(empty($name) && empty($email))
||管道标志意味着或在您的情况下,您告诉任何一个名称为空或电子邮件是空的,但在&amp;&amp;和签名我们告诉任何一个名称为空,电子邮件是空的返回错误。
$name = strip_tags($_POST['name']);
答案 3 :(得分:0)
以下行应写在else close
$query = mysqli_query($server, "INSERT INTO subscribers (name, email) VALUES('$name','$email')");
并使用
if(empty($name) && empty($email))
不是
if(empty($name) || empty($email))
答案 4 :(得分:0)
您应该在else条件下运行insert查询,因为在这里您要检查字段是否为空。
<?php
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$name = strip_tags($name);
$email = strip_tags($email);
if(empty($name) || empty($email)) {
echo "All Fields Must Be Filled In!";
} else {
$server = mysqli_connect('localhost', 'root', '', 'newsletter') or die("The Server Cannot Be Reached at this Time!");
$query = mysqli_query($server, "INSERT INTO subscribers (name, email) VALUES('$name','$email')");
mysqli_close($server);
echo "You have been successfully subscribed";
}
}
?>