我的问题: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.';
}
}
答案 0 :(得分:0)
我会使用预备语句,并且还会像这样重写你的函数:
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.';
}
}