【问题标题】:PHP isset and empty problems whit html formPHP isset 和 html 表单的空问题
【发布时间】:2014-05-24 18:57:11
【问题描述】:

我的问题:isset() 不工作我在数据库中得到空白行!空()不起作用! 我试图将html页面放入,但无法正常工作!谢谢! 我的问题:isset() 不工作我在数据库中得到空白行!空()不起作用! 我试图将html页面放入,但无法正常工作!谢谢!

 <?php
include_once 'include/db_connect.php';
include_once 'include/functions.php';
if($_POST) {
newMember($mysqli);
}
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Add user</title>
</head>
<body>
<h1 class="text-center">Add User</h1>
<link class="cssdeck" rel="stylesheet" href="media/css/bootstrap.min.css">
<link class="cssdeck" rel="stylesheet" href="media/css/bootstrap-select.css">
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.1/css/bootstrap-responsive.min.css" class="cssdeck">
<div class="modal-body">
    <form id="tab" action="adduser.php" method="post">
        <label>Username</label>
        <input type="text"  name="username" class="input-xlarge">
        <label>Full Name</label>
        <input type="text"  name="fullname" class="input-xlarge">
        <label>Email</label>
        <input type="text"  name="email" class="input-xlarge">
        <label>Password</label>
        <input type="password" name="password" class="input-xlarge"><br>
         <div>
            <label>Role:</label>
            <select name="role" class="selectpicker" style="width:87%">
            <option value="admin">Admin</option>
            <option value="pm">Project Manager</option>  
            <option value="php">PHP Programmer</option>
            <option value="seo">SEO Programmer</option>
            <option value="webdesign">Web Designer</option>
            </select>
        </div>
        <div class="text-center" style="padding-top:20px">
            <button class="btn btn-primary">Create Account</button>
        </div>
    </form>
</div>

这是functions.php中的函数

function newMember($mysqli)
{ if(isset($_POST['fullname'],$_POST['username'],$_POST['password'],$_POST['email']))
    {   
        $fullname = $_POST['fullname'];
        $username = $_POST['username'];
        $password = $_POST['password'];
        $password = md5($password);
        $email = $_POST['email'];
        $role = $_POST['role'];
        $statement = $mysqli->query("INSERT into members(fullname,username,password,email,role) VALUES ('$fullname','$username','$password','$email','$role')");
        if($statement)
        {
            echo '<p class="alert alert-success text-center">User added.</p>';
        }
    } else {
        echo 'Complete all boxes.';
    }
}

【问题讨论】:

  • 变量可以设置但为空!
  • 您应该很高兴您没有因为将未转义的字符串插入 SQL 查询而变得更糟!谢谢!
  • 如何解决问题?我只想确保在设置 htmlentities 后不添加空输入
  • 使用mysqli_real_escape_string($connect, strip_tags($_POST['..']));
  • 那么isset或empty的范围是什么?

标签: php function public


【解决方案1】:

我会使用准备好的语句,并像这样重写你的函数:

function newMember($mysqli) {     
    $fullname = isset($_POST['fullname']) ? $_POST['fullname'] : '';
    $username = isset($_POST['username']) ? $_POST['username'] : '';
    $password = isset($_POST['password']) ? $_POST['password'] : '';
    $email = isset($_POST['email']) ? $_POST['email'] : '';
    $role = isset($_POST['role']) ? $_POST['role'] : '';

    if ($fullname != '' && $username != '' && $password != '' && $email != '' && $role != '') {
        $sql = 'INSERT INTO members (fullname, username, password, email, role) VALUES (?, ?, ?, ?, ?)';
        $stmt = $mysqli->prepare($sql);
        $stmt->bind_param('sssss', $fullname, $username, md5($password), $email, $role);
        $stmt->execute();

        if ($stmt->affected_rows == 1) {
            echo '<p class="alert alert-success text-center">User added.</p>';
        }
        $stmt->close();
    } else {
        echo 'Complete all boxes.';
    }
}

【讨论】:

    猜你喜欢
    • 2017-12-16
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    • 2020-10-25
    • 2022-01-16
    • 2015-10-14
    • 2017-10-25
    • 1970-01-01
    相关资源
    最近更新 更多