简体   繁体   English

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

[英]Extracting auto increment from database into php var

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

on line 12 i'm trying to Select the auto-increment id_u from the table "account". 在第12行上,我正在尝试从表“帐户”中选择自动递增的id_u。 but in stead of that it gives me an error saying: 但是相反,它给了我一个错误:

Notice: Undefined index: id_u in /storage/ssd2/719/5658719/public_html/signin.php on line 16 注意:未定义的索引:第16行的/storage/ssd2/719/5658719/public_html/signin.php中的id_u

Warning: Cannot modify header information - headers already sent by (output started at /storage/ssd2/719/5658719/public_html/signin.php:16) in /storage/ssd2/719/5658719/public_html/signin.php on line 20 警告:无法修改标头信息-第20行的/storage/ssd2/719/5658719/public_html/signin.php中已发送的标头(输出始于/storage/ssd2/719/5658719/public_html/signin.php:16)

I am unable to understand why 我不明白为什么

You're trying to assign before the query has been executed: $id1 = $query['id_u']; 您正在尝试在执行查询之前进行分配: $id1 = $query['id_u'];

Try moving that after $query->execute(); 尝试在$query->execute();之后移动它$query->execute();

Try this: 尝试这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM