无法在表格中插入$ username

时间:2016-02-26 07:24:05

标签: php mysql login

您好我尝试将他们输入的用户名插入到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();
?>

1 个答案:

答案 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')";