在PHP的何处添加“插入”查询

时间:2019-06-11 11:25:52

标签: php

我具有要向其添加日志文件的登录页面,这意味着当用户登录时,会将用户详细信息插入数据库。

第一个代码是我的工作页面

第二个代码是我想在代码中插入的语句,但是在我添加该代码的任何地方,我的页面停止工作或没有数据传递到数据库

我将不胜感激,谢谢您。

<?php //http://bootsnipp.com/snippets/56A0W - shource code for login page.
require("includes/config/config.php"); 
$submitted_username = ''; 
if(!empty($_POST)){ 
    $query = "SELECT id, username, password, salt, email FROM susers WHERE username = :username"; 
    $query_params = array(':username' => $_POST['username']);         
    try{ 
        $stmt = $db->prepare($query); 
        $result = $stmt->execute($query_params);
        } 
    catch(PDOException $ex){ die("Failed to run query: " . $ex->getMessage()); } 
    $login_ok = false; 
    $row = $stmt->fetch(); 
    if($row){ 
        $check_password = hash('sha256', $_POST['password'] . $row['salt']); 
        for($round = 0; $round < 65536; $round++){
            $check_password = hash('sha256', $check_password . $row['salt']);
        } 
        if($check_password === $row['password']){
            $login_ok = true;
        } 
    } 
    if($login_ok)
    { 
        unset($row['salt']); 
        unset($row['password']); 
        $_SESSION['user'] = $row;
        header ("Location: includes/Sales/".$_POST["username"]."/".$_POST["username"].".php");
        die("Redirecting to: sales.php");
    }
    else {$err[]='Wrong username and/or password!'; //1
            header("Location: sales.php");  //2
    } if($err)  //3
$_SESSION['msg']['login-err'] = implode('<br />',$err); // 1 and 2 and 3 http://tutorialzine.com/2009/10/cool-login-system-php-jquery/ - source code for Error message
exit;
} ?>

第二个代码

$query = "INSERT INTO suserlog (username) VALUES ( :username)";

我相信代码应该放在其中

if($login_ok)

1 个答案:

答案 0 :(得分:0)

弄清楚了,谢谢

<?php 
require("includes/config/config.php"); 
$submitted_username = ''; 
if(!empty($_POST)){ 
    $query = "SELECT id, username, password, salt, email FROM susers WHERE username = :username;"; 
    $query .= "INSERT INTO suserlog (username) VALUES ( :username)";