你好,我一直试图读取属性“名称”的数组错误,在我的登录为laravel。我们一直在尝试不同的方法来使它发挥作用,但到目前为止没有取得成功。
@extends("Layout")
<?php
if($_SERVER["REQUEST_METHOD"] == "POST") {
echo '<p>TEST</p>';
$sql = DB::select('select * from users where Name = ?', [$_POST["txtuser"]]);
if(!$sql->Name == "") {
echo 'No user found!';
} else {
if($sql->password == $_POST["txtpass"]) {
session_start();
$_SESSION["loggedin"] = true;
header("location: home.blade.php");
} else {
echo 'wrong password!';
}
}
}
?>
<div class="card2">
<div class="center">
<button><a href="{{url('/home')}}" >Home</a> </button>
<button> <a href="{{url ('/b2b') }}" >Business to business</a> </button>
<button><a href="{{url ('/b2c') }}" >Business to consumer </a> </button>
<button><a href="{{url ('/c2c')}}" >Consumer to consumer</a> </button>
<button><a href="{{url ('login-system/login')}}" >Login</a> </button>
<button><a href="{{url ('/store')}}">Store</a></button>
</div>
<H1>Login</H1>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
@csrf
<input type="text" name="txtuser" value='' />
<input type="password" name="txtpass" />
<input type="submit" name="login" value="submit"/>
</form>
</div>发布于 2022-06-27 15:49:26
您需要首先遍历您的选择。如果我没有弄错,即使你选择了一个特定的用户,你也会得到一个数组。
示例:
$users = DB::select('select * from users');
foreach ($users as $user) {
echo $user->name;
}或者,您可以尝试从返回的数组中获取第一项,并将其存储在变量$user中,然后使用$user->name
而且,在使用之前清点返回的项目并不是件坏事。可能是返回了0项,因此可以显示用户未找到(我在没有测试的情况下快速编写了下面的代码)。
$users = DB::select('select * from users where Name = ?', [$_POST["txtuser"]]);
if(count($users) == 0) {
echo 'No user found!';
}另外,将密码签入sql语句本身也是一件好事。最好在一起检查用户名和密码,而不是说用户是否存在
https://stackoverflow.com/questions/72774452
复制相似问题