单击按钮时。我想使用register_button从表单中获取值。但是,它不能进入if子句。
我无法检查在if语句中的表格上键入的任何内容。 我还有其他选项可以用来从表单中检索数据吗?还是我做错了?
var_dump($_POST)
返回
array(6) {
["reg_fname"]=> string(1) "a"
["reg_lname"]=> string(3) "asd"
["reg_email"]=> string(5) "ad@ad"
["reg_pass"]=> string(4) "dasd"
["reg_pass2"]=> string(5) "sadsa"
["register_button"]=> string(8) "Register"
}
Invalid format
代码:
$fname = "";
$lname = "";
$em = "";
$em2 = "";
$password = "";
$password2 = "";
$date = "";
$error_array = "";
if (isset($_POST['register_button'])) {
$fname = strip_tags($_POST['reg_fname']);
$fname = str_replace(' ', ' ', $fname);
$lname = $_POST['reg_lname'];
$lname = str_replace(' ', ' ', $lname);
$em = $_POST['reg_email'];
$em = str_replace(' ', ' ', $em);
$password = strip_tags($_POST['reg_pass']);
$password2 = strip_tags($_POST['reg_pass2']);
$date = date("Y-m-d");
if(filter_var($em, FILTER_VALIDATE_EMAIL)){
$em = filter_var($em, FILTER_VALIDATE_EMAIL);
$e_check = mysqli_query($con, "SELECT email FROM users WHERE email='$em");
$num_rows = mysqli_num_rows($e_check);
if(num_rows > 0){
echo "Email already in use";
}
}else {
echo "Invalid format";
}
if(strlen(fname)>25 || strlen(fname)<2 ){
echo "Your fi";
}
}
<form method="post" action="index.php">
<input type = "text" name="reg_fname" placeholder="First Name" required>
<br>
<input type = "text" name="reg_lname" placeholder="Last Name" required>
<br>
<input type = "email" name="reg_email" placeholder="Email" required>
<br>
<input type = "password" name="reg_pass" placeholder="Password" required>
<br>
<input type = "password" name="reg_pass2" placeholder="Confirm Password" required>
<br>
<input type = "submit" name="register_button" value="Register">
</form>
答案 0 :(得分:0)
它进入了第一个if
语句。您可以知道,因为在var_dump($_POST)
时,转储输出之后会显示文本“无效格式”。这意味着它将进入页面else
下方的echo "Invalid format";
语句中。因此,实际上是您的filter_var
if
语句失败。
答案 1 :(得分:0)
任何人回答后,请不要编辑您的问题 您不检查查询是否成功。 您是否曾使用过过滤变量,最好是直接将其设置为变量 您忘记在查询字符串后添加逗号。
请尝试以下
<?php
$fname = "";
$lname = "";
$em = "";
$em2 = "";
$password = "";
$password2 = "";
$date = "";
$error_array = "";
if (isset($_POST['register_button'])) {
$fname = strip_tags($_POST['reg_fname']);
$fname = str_replace(' ', ' ', $fname);
$lname = $_POST['reg_lname'];
$lname = str_replace(' ', ' ', $lname);
$em = $_POST['reg_email'];
$em = str_replace(' ', ' ', $em);
$password = strip_tags($_POST['reg_pass']);
$password2 = strip_tags($_POST['reg_pass2']);
$date = date("Y-m-d");
// No need to use if
$em = filter_var($em, FILTER_VALIDATE_EMAIL);
/// $con = mysqli_connect("localhost", "user", "pass", "dbname");
/// using if to be sure query success
if ($e_check= mysqli_query($con, "SELECT email FROM users WHERE email='$em'")) {
$num_rows= mysqli_num_rows($e_check);
if ($num_rows> 0) {
echo "Email already in use";
} else {
/// here your insert query
}
}
else
{
echo "No query ";
}
if(strlen($fname) >25 || strlen($fname)< 2 )
{
echo "Your first name must be at least 2 char and maximum 25 char ";
}
}
答案 2 :(得分:0)
为什么使用“ register_button”作为您的支票?为什么您的其中一个字段不是必需的?