douruhu4282 2014-04-27 03:32
浏览 23

Isset和空函数不起作用

Why isn't my isset and/or empty functions working? Every time I click the submit button on the html form, even when the form is empty it inserts into database. Can someone provide me with a thorough explanation of what I'm doing wrong and how I can fix it?

<?php 
$host = "localhost";
$user = "root";
$db_password = "";
$database = "listings_db";

$link = mysqli_connect($host, $user, $db_password, $database) or die("Error " . mysqli_error($link));

      if (isset
     ($_POST['user_firstname'], 
      $_POST['user_lastname'], 
      $_POST['user_email'], 
      $_POST['user_password'], 
     $_POST['user_type'])) 
{
$firstname = $_POST['user_firstname'];
$lastname = $_POST['user_lastname'];
$email = $_POST['user_email'];
$password = $_POST['user_password'];
$type = $_POST['user_type'];

$errors = array();
if(empty($firstname) 
|| empty($lastname) 
|| empty($email) 
|| empty($email) 
|| empty($password) 
|| empty($type)) 
{$errors [] = '*All fields are required!';}     
else {
if(filter_var($email, FILTER_VALIDATE_EMAIL) === false) {$errors[] = '*Please enter a valid email address!' ;}

if(strlen($firstname) > 25) {$errors[] = '*The email address you entered contains too many characters!';}
if(strlen($lastname) > 25) {$errors[] = '*The first name you entered contains too many characters!';}
if(strlen($email) > 40) {$errors[] = '*The last name you entered contains too many characters!';}   
if(strlen($password) > 40) {$errors[] = '*The password you entered contains too many characters!';} 
if(strlen($type) != true){$errors[] = '*Please select an account type!';}
    }

$firstname = $_POST['user_firstname'];
$lastname = $_POST['user_lastname'];
$email = $_POST['user_email'];
$password = md5($_POST['user_password']);
$type = $_POST['user_type'];

$firstname = mysqli_real_escape_string($link, $firstname);
$lastname  = mysqli_real_escape_string($link, $lastname);
$email = mysqli_real_escape_string($link, $email);
$password = mysqli_real_escape_string($link, $password);
$type = mysqli_real_escape_string($link, $type);


$query = mysqli_query($link, "INSERT INTO users (user_id, user_firstname, user_lastname, user_email, user_password) VALUES ('', '$firstname', '$lastname', '$email', '$password')");
}
?>

Edit: Here's the form:

<form action="" method="post">
<p>First Name: <br><input type="text" name="user_firstname" size="25" maxlength="25"/></p>
<p>Last Name: <br><input type="text" name="user_lastname" size="25" maxlength="25"/></p>
<p>Email Address: <br><input type="email" id="email" name="user_email" size="25" maxlength="40"/><p>
<p>Create a Password: <br><input type="password" name="user_password" size="25" maxlength="40"/></p>
<p>Account Type: <br> <select name="user_type"> 
<option value="seller" selected>Seller</option>
<option value="Buyer">Buyer </option>
</select>


  • 写回答

1条回答 默认 最新

  • doujing1967 2014-04-27 03:44
    关注

    That´s because your form fields would always be submitted. No matter if they are filled with any content, or not.

    So, you don´t need to check if they are set, because they would be always set. You need to check, if they are not empty. To do so, you can use the empty PHP construct:

    if (!empty($_POST['user_firstname'])
        && !empty($_POST['user_lastname'])
        && !empty($_POST['user_email'])
        && !empty($_POST['user_password'])
        && !empty($_POST['user_type']))
    {
        // TODO
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分