Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 致命错误:未捕获PDO异常:SQLSTATE[23000]:完整性约束冲突:_Php_Mysql_Phpmyadmin - Fatal编程技术网

Php 致命错误:未捕获PDO异常:SQLSTATE[23000]:完整性约束冲突:

Php 致命错误:未捕获PDO异常:SQLSTATE[23000]:完整性约束冲突:,php,mysql,phpmyadmin,Php,Mysql,Phpmyadmin,我跟随youtube教程,我得到了这个错误,我不知道如何修复,因为我仍然是一个php初学者。你们能帮帮我吗?我也在使用phpmyadmin。我会发布一些代码 我的错误: 致命错误:未捕获PDOException:SQLSTATE[23000]:完整性约束冲突:1452无法添加或更新子行:外键约束失败(socialnetworklogin_令牌,约束login_令牌_ibfk_1外键(id)引用用户(id)在D:\xampp\htdocs\test\classes\DB.php中:14堆栈跟踪:#

我跟随youtube教程,我得到了这个错误,我不知道如何修复,因为我仍然是一个php初学者。你们能帮帮我吗?我也在使用phpmyadmin。我会发布一些代码

我的错误:

致命错误:未捕获PDOException:SQLSTATE[23000]:完整性约束冲突:1452无法添加或更新子行:外键约束失败(
socialnetwork
login_令牌
,约束
login_令牌_ibfk_1
外键(
id
)引用
用户
id
)在D:\xampp\htdocs\test\classes\DB.php中:14堆栈跟踪:#0 D:\xampp\htdocs\test\classes\DB.php(14):PDOStatement->execute(Array)#1 D:\xampp\htdocs\test\login.php(19):DB::query('INSERT INTO log…',Array)#2{main}在第14行的D:\xampp\htdocs\test classes\DB.php中抛出

我的login.php

<?php
include('classes/DB.php');
if(isset($_POST['login'])){
$username = $_POST['username'];
$password = $_POST['password'];

if (DB::query('SELECT username FROM users WHERE username=:username', array(':username'=>$username))){

    if(password_verify($password, DB::query('SELECT password FROM users WHERE username=:username', array(':username'=>$username))[0]['password'])){
        echo 'Logged in!';

        $cstrong = True;
        $token = bin2hex(openssl_random_pseudo_bytes(64, $cstrong)); //generate a hex of random bytes for token

        $user_id = DB::query('SELECT id FROM users WHERE username=:username', array(':username'=>$username))[0]['id'];
        DB::query('INSERT INTO login_tokens VALUES (\'\', :token, :user_id)', array(':token'=>sha1($token), 'user_id'=>$user_id)); // passing token into user_id, uses sha1 to hash token for encryption

        //SNID variable meaning social network id, expires date, current time, seconds, mins, 1 day, 7 valid for 1 week, server / for everywhere, domain for where you are hosting, SSL - set to true if it is using ssl, http only 
        setcookie("SNID", $token, time() + 60 * 60 * 24 * 7, '/', NULL, NULL, TRUE);   

        //second cookie that will expire every 3 days. Ask for server for another cookie. Without them knowing and they dont have to log in/out. 
        setcookie("SNID_", '1', time() + 60 * 60 * 24 * 3, '/', NULL, NULL, TRUE); 


    }else{
        echo 'username or password is incorrect!';  
    }   
}else {
    echo 'User not registered!';    
}
}
?>
my index.php

    if(isset($_COOKIE['SNID'])){


    if(DB::query('SELECT user_id FROM login_tokens WHERE token=:token', array(':token'=>sha1($_COOKIE['SNID'])))){

        $userid = DB::query('SELECT user_id FROM login_tokens WHERE token=:token', array(':token'=>sha1($_COOKIE['SNID'])))[0]['user_id'];

        if (isset($_COOKIE['SNID_'])){

            return $userid;


        }else{

            $cstrong = True;
            $token = bin2hex(openssl_random_pseudo_bytes(64, $cstrong));

            DB::query('INSERT INTO login_tokens VALUES (\'\', :token, :user_id)', array(':token'=>sha1($token), 'user_id'=>$userid));

            DB::query('DELETE FROM login_tokens WHERE token=:token', array(':token'=>sha1($_COOKIE['token'])));

            setcookie("SNID", $token, time() + 60 * 60 * 24 * 7, '/', NULL, NULL, TRUE);
            setcookie("SNID_", '1', time() + 60 * 60 * 24 * 3, '/', NULL, NULL, TRUE); 

            return $userid;

        }
    }




}



return false;
}
在phpMyAdmin的我的登录令牌中:

姓名:id 类型:int(11) 整理: 属性:无符号 空:没有 默认值:无 额外:自动增量 初级的

名称:令牌 类型:字符(64) 校对:拉丁文1_瑞典语(ci) 属性: 空:没有 默认值:无 关键字:令牌 独特:是的 没有 列标记 基数:0 校对:A 空:没有

名称:用户id 类型:int(11) 整理: 属性:无符号 空:没有
默认值:None

我在index.php中也有这两行。包括('classes/DB.php');函数isLoggedIn(){而且,我还取消了对setcookie的注释(“SNID_389;”,“1',time()+60*60*24*3',/”,NULL,NULL,TRUE);我知道它被注释了,我只是试图通过注释我最近添加的内容来进行调试,但我仍然得到了那个错误。在插入之前编辑你的问题而不是写注释检查
$user\u id
包含什么。只是
var\u dump()
it。嘿,Wolen!我只是想跟你澄清一下,你想让我去var\u dump()吗在我的login.php或index.php中?我在index.php中也有这两行。include('classes/DB.php');function isLoggedIn(){而且,我还取消了注释:setcookie(“SNID_“,'1',time()+60*60*24*3',/”,NULL,NULL,TRUE);我知道它被注释了,我只是试图通过注释我最近添加的内容来进行调试,但我仍然得到了那个错误。在插入之前编辑你的问题而不是写注释检查
$user\u id
包含什么。只是
var\u dump()
it。嘿,Wolen!我只是想跟你澄清一下,你想让我去var\u dump()吗在我的login.php或index.php中?
    if(isset($_COOKIE['SNID'])){


    if(DB::query('SELECT user_id FROM login_tokens WHERE token=:token', array(':token'=>sha1($_COOKIE['SNID'])))){

        $userid = DB::query('SELECT user_id FROM login_tokens WHERE token=:token', array(':token'=>sha1($_COOKIE['SNID'])))[0]['user_id'];

        if (isset($_COOKIE['SNID_'])){

            return $userid;


        }else{

            $cstrong = True;
            $token = bin2hex(openssl_random_pseudo_bytes(64, $cstrong));

            DB::query('INSERT INTO login_tokens VALUES (\'\', :token, :user_id)', array(':token'=>sha1($token), 'user_id'=>$userid));

            DB::query('DELETE FROM login_tokens WHERE token=:token', array(':token'=>sha1($_COOKIE['token'])));

            setcookie("SNID", $token, time() + 60 * 60 * 24 * 7, '/', NULL, NULL, TRUE);
            setcookie("SNID_", '1', time() + 60 * 60 * 24 * 3, '/', NULL, NULL, TRUE); 

            return $userid;

        }
    }




}



return false;
}