gpt4 book ai didi

php 代码运行错误

转载 作者:行者123 更新时间:2023-12-01 00:01:36 25 4
gpt4 key购买 nike

嗯,这里出了点问题。我输入我的数据库中存在的用户名和密码。在这种情况下它应该回显这个字符串

need to activate

但它呼应了这个

You need to reg

初始化文件

<?php
//error_reporting(0);
session_start();

require 'dbconnect.php';//this works okay so i wouldn't post this file code
require 'users.php';

$errors = array();
?>

用户.php

        <?php

function user_exists($username){
$username = mysql_real_escape_string($username);

$query = mysql_query("SELECT COUNT('user_id') FROM `users` WHERE 'username' = '$username'");

if (!$query) {
die('Could not query:' . mysql_error());
}

return (mysql_result($query, 0) == 1) ? true : false;

}

function user_active($username){

$username = mysql_real_escape_string($username);

$querytoo = mysql_query("SELECT COUNT('user_id') FROM `users` WHERE 'username' = '$username' AND 'active' = 1");

if (!$querytoo) {
die('Could not query:' . mysql_error());
}

return (mysql_result($querytoo , 0) == 1) ? true : false;

}
?>

登录.php

<?php
include 'init.php';


if(empty($_POST) === false){
$username = $_POST['username'];
$password = $_POST['password'];

if(empty($username) === true || empty($password) === true){
$errors[]='You need to enter a username and password';
}
elseif(user_exists($username) === false){
$errors[]='You need to reg';
}
elseif(user_active($username) === false){
$errors[]='need to activate';
}
else {
//
}

print_r($errors);
}



?>

部分html

<form action="login.php" method = "post">
<ul id="login">
<li>
username:<br>
<input type="text" name="username" size="30" value=""/></li>
<li>password:<br>
<input type="password" name="password" size="30" value=""/></li>
<li><input type = "submit" value ="Log in"></li>
<li>
<a href="register.php"> Register</a>
</li>
</ul>

附言查询文本工作正常,我在 mysql 中检查过。在 php 代码中,当我在此处键入 `` 而不是 ''

SELECT COUNT('user_id') FROM users WHERE 'username' = '$username' AND 'active' = 1

出现

'Could not query:'

东西

而且我尝试了 elseifelse if 东西,所以我认为没有问题

最佳答案

你的 SQL 是错误的。您正在将静态字符串与您的值进行比较,而不是列名:

    SELECT COUNT('user_id') FROM users WHERE 'username' = '$username' AND 'active' = 1

应该是

SELECT COUNT(user_id) FROM users WHERE username = '$username' AND active = 1

请注意,我从列名称中删除了 '。使用反引号也是有效的:

SELECT COUNT(`user_id`) FROM users WHERE `username` = '$username' AND `active` = 1

关于php 代码运行错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12874961/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com