<?php
session_start();
include ("dbconnectie.php");
if(isset($_SESSION['on'])) {
header("location:homepage.php"); }
if(isset($_POST['login'])) {
$username = $_POST['username'];
$password = sha1($_POST['password']);
$query = $db->prepare("SELECT * FROM account
WHERE username = :user
AND password = :pass");
$query->bindParam("user", $username);
$query->bindParam("pass", $password);
$query->execute();
$result = $query->fetchALL(PDO::FETCH_ASSOC);
$id1 = $result['id_u'];
if($query->rowCount() == 1) {
$_SESSION['on'] = $username;
$_SESSION['id_u'] = $id1;
header("location:homepage.php");
} else {
echo "The username and password do not match";
}
echo "<br>";
}
?>
第12行的我正在尝试从表“account”中选择自动增量id_u。但取而代之的是它给了我一个错误说:
注意:未定义索引:第16行/storage/ssd2/719/5658719/public_html/signin.php中的id_u
警告:无法修改标题信息 - 已在/storage/ssd2/719/5658719/public_html/signin.php中发送的标题(输出从/storage/ssd2/719/5658719/public_html/signin.php:16开始)第20行
我无法理解为什么
答案 0 :(得分:0)
您在查询执行之前尝试分配:
$id1 = $query['id_u'];
尝试在$query->execute();
试试这个:
<?php
session_start();
include ("dbconnectie.php");
if(isset($_SESSION['on'])) {
header("location:homepage.php"); }
if(isset($_POST['login'])) {
$username = $_POST['username'];
$password = sha1($_POST['password']);
$query = $db->prepare("SELECT * FROM account
WHERE username = :user
AND password = :pass");
$query->bindParam("user", $username);
$query->bindParam("pass", $password);
$query->execute();
$result = $query->fetch(PDO::FETCH_ASSOC);
$id1 = $result['id_u'];
if($query->rowCount() == 1) {
$_SESSION['on'] = $username;
$_SESSION['id_u'] = $id1;
header("location:homepage.php");
}
else {
echo "The username and password do not match";
}
echo "<br>";
}
?>