从数据库中提取自动增量到php var

时间:2018-05-27 18:44:16

标签: php

<?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行

我无法理解为什么

1 个答案:

答案 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>";
}
?>