我尝试过(作为我的第一个项目)决定进行简单的登录/注销。 这是我到目前为止所做的,它可以创造奇迹,但每次你访问页面时它都会注册一个空的usename并传递,只有你可以注册,传递显然是空的但我似乎无法利用empty(); 有什么想法吗?
<html>
<head>
<title>Quick Register</title>
</head>
<body>
<form action="/register.php" method = "post">
<b>Quick.</b>
<p>USERNAME</p>
<input type="text" name="usernameInput" size="30">
<p>PASSWORD</p>
<input type="password" name="passwordInput" size="30">
<p><input type='submit' name='submit' value = "Send"></p>
</form>
<?php
if (isset($_POST['submit']))
{
$date_missing = array();
if (empty($_POST['usernameInput']))
{
$data_missing[] = "Username";
}
else
{
$username = trim($POST['usernameInput']);
}
if (empty($_POST['passwordInput']))
{
$data_missing[] = "Password";
}
else
{
$password = trim($POST['passwordInput']);
}
if (empty($data_missing))
{
require_once ("config.php");
if(empty($password)!=0)
{
$query = "INSERT INTO users (username, password,created_at) VALUES(?, ?,NOW());";
$stmt = mysqli_prepare($link, $query);
mysqli_stmt_bind_param($stmt, "ss", $username, $password);
mysqli_stmt_execute($stmt);
$affected_rows = mysqli_stmt_affected_rows($stmt);
}
if ($affected_rows && empty($password)<>0)
{
echo ($password."Regee");
mysqli_stmt_close($stmt);
mysqli_close($link);
}
}
else
{
echo "Nu ma lasa gol boss";
foreach($data_missing as $missing)
echo($missing);
}
}
?>
</body>
</html>
答案 0 :(得分:-1)
您的代码中有很多错误。你真的应该适当缩进,这将有助于你抓住它们。
<html>
<head>
<title>Quick Register</title>
</head>
<body>
<form action="/register.php" method="post">
<b>Quick.</b>
<p>USERNAME</p>
<input type="text" name="usernameInput" size="30">
<p>PASSWORD</p>
<input type="password" name="passwordInput" size="30">
<p><input type='submit' name='submit' value="Send"></p>
</form>
<?php
if (isset($_POST['submit'])) {
// You had a spelling error here. You defined $datE_missing
$data_missing = array();
if (empty($_POST['usernameInput'])) {
$data_missing[] = "Username";
} else {
// You had syntax error on $_POST
$username = trim($_POST['usernameInput']);
}
if (empty($_POST['passwordInput'])) {
$data_missing[] = "Password";
} else {
// You had syntax error on $_POST
$password = trim($_POST['passwordInput']);
}
if (empty($data_missing)) {
require_once("config.php");
// Empty returns a boolean not an integer
if (!empty($password)) {
// Didn't check SQL logic here but you seem to be OK with that.
$query = "INSERT INTO users (username, password,created_at) VALUES(?, ?,NOW());";
$stmt = mysqli_prepare($link, $query);
mysqli_stmt_bind_param($stmt, "ss", $username, $password);
mysqli_stmt_execute($stmt);
// You are defining $affected_rows here and also trying to use it the next IF statement.
// What happens if $affected_rows isn't set here? It will blow up in the next statement.
// You need to change that. I have commented it out for now so that the rest of the code is OK.
$affected_rows = mysqli_stmt_affected_rows($stmt);
}
// empty is a boolean so empty($password) <> 0 is better written as empty($password) === true
// Also, you don't really need to worry about closing connections. The env. will handle that for you.
// if ($affected_rows && empty($password) === true) {
// echo($password . "Regee");
//
// // You really don'y need to close connections here in a web app. Let the env. handle that for you.
// mysqli_stmt_close($stmt);
// mysqli_close($link);
// }
} else {
echo "Nu ma lasa gol boss";
foreach ($data_missing as $missing)
echo($missing);
}
}
编辑#2
这是我构建代码以使其更清晰的方式。
<?php
// Move the form processing up here so that you can show validation errors above the form.
// Make this variable available right away.
$validationErrors = [];
if (isset($_POST['submit'])) {
// Do validation first.
$username = trim($_POST['usernameInput']);
$password = $_POST['passwordInput'];
if (empty($username)) {
$validationErrors['username'] = 'You must enter a username';
}
if (empty($password)) {
$validationErrors['password'] = 'You must enter a password';
}
if (empty($validationErrors)) {
// Insert row into DB here.
}
}
?>
<html>
<head>
<title>Quick Register</title>
</head>
<body>
<?php
// Output validation errors here.
if (!empty($validationErrors)) {
foreach ($validationErrors as $error) {
echo '<div style="">$error</div>';
}
}
?>
<form action="/register.php" method="post">
<b>Quick.</b>
<p>USERNAME</p>
<input type="text" name="usernameInput" size="30">
<p>PASSWORD</p>
<input type="password" name="passwordInput" size="30">
<p><input type='submit' name='submit' value="Send"></p>
</form>
答案 1 :(得分:-1)
看起来你有一个错字
$date_missing = array();
我认为应该是
$data_missing = array();
否则$data_missing
未定义
同样是@Jay Blanchard的建议
$POST
未定义且应为$_POST