您好我尝试将他们输入的用户名插入到logattempt表中 但它无法识别我已声明的$ username
<?php
$servername = "localhost";
$user = "root";
$dbpassword="";
$dbname = "hrms";
$username=$_POST['username'];
$password=$_POST['password'];
$conn = new mysqli($servername, $user, $dbpassword, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
header("location: main.php");
} else {
$sql = "INSERT INTO logattempt (id, username, timest, failedatt)
VALUES ('John', '$username', 'john@example.com','asdfasdf')";
$conn->query($sql);
header("location: index.php");
}
$conn->close();
?>
答案 0 :(得分:0)
假设您将Id字段设置为自动增量
从
改变$sql = "INSERT INTO logattempt (id, username, timest, failedatt)
VALUES ('John', '$username', 'john@example.com','asdfasdf')";
到
$sql = "INSERT INTO logattempt (id, username, timest, failedatt)
VALUES (null, '$username', 'john@example.com','asdfasdf')";