警告:非法字符串偏移量';位置';在第57行的C:\xampp\htdocs\PMSS\login.php中
这是我的login.php警告:非法字符串偏移量';位置';在第57行的C:\xampp\htdocs\PMSS\login.php中,php,Php,这是我的login.php <?php //load and connect to MySQL database stuff require("config.inc.php"); if (!empty($_POST)) { if(empty($_POST['username']) || empty($_POST['password'])) { $response["success"] = 0; $response["message"] = "
<?php
//load and connect to MySQL database stuff
require("config.inc.php");
if (!empty($_POST)) {
if(empty($_POST['username']) || empty($_POST['password'])) {
$response["success"] = 0;
$response["message"] = "Please fill in the login details!";
die(json_encode($response));
}
$query = "SELECT email, password, position FROM user WHERE email = :email ";
$query_params = array(':email' => $_POST['username'],);
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
// For testing, you could use a die and message.
//die("Failed to run query: " . $ex->getMessage());
//or just use this use this one to product JSON data:
$response["success"] = 0;
$response["message"] = "Database Error1. Please Try Again!";
die(json_encode($response));
}
//This will be the variable to determine whether or not the user's information is correct.
//we initialize it as false.
$validated_info = false;
$login_ok = false;
//fetching all the rows from the query
$row = $stmt->fetch();
if ($row) {
//if we encrypted the password, we would unencrypt it here, but in our case we just
//compare the two passwords
if ($_POST['password'] === $row['password']) {
$login_ok = true;
}
// If the user logged in successfully, then we send them to the private members-only page
// Otherwise, we display a login failed message and show the login form again
if ($login_ok) {
$response["success"] = 1;
$response["message"] = "Login Successful!";
$response["posts"] = array();
foreach ($row as $rerow) {
$row = array(
$post["position"] = $rerow["position"]
);
array_push($response["posts"], $post);
}
die(json_encode($response));
}
else {
$response["success"] = 0;
$response["message"] = "Invalid Credentials!";
die(json_encode($response));
}
}
}
else {
?>
<h1>Login</h1>
<form action="login.php" method="post">
Username:<br />
<input type="text" name="username" placeholder="username" />
<br /><br />
Password:<br />
<input type="password" name="password" placeholder="password" value="" />
<br /><br />
<input type="submit" value="Login" />
</form>
<a href="register.php">Register</a>
<?php
}
?>
应该是
$row = array(
$post["position"] => $rerow["position"]
);
其实
foreach ($row as $rerow) {
$row = array(
$post["position"] = $rerow["position"]
);
array_push($response["posts"], $post);
}
也许你想做些别的事:
do {
$post = $row["position"]
array_push($response["posts"], $post);
} while ($row = $stmt->fetch());
这会将字段“position”添加到posts中,并继续获取其他记录。第57行:$post[“position”]=$rerow[“position”]
数组的键/值对是使用=>
设置的,而不仅仅是=
这并不能解决问题,另外还有一个错误是注意:在第57行的C:\xampp\htdocs\PMSS\login.php中使用了未定义的常量位置-假定的“位置”
上一个非法字符串偏移量
错误仍在显示?是非法字符串偏移量
仍然存在我同意你的怀疑。如果OP正在循环$row
,并且它有多个元素,那么循环不会在一次迭代后停止,因为他们在第一次迭代中将$row
设置为一个新数组?实际上,我试图做的是,当我使用电子邮件和密码登录时,一旦它与登录信息匹配,之后,我需要检索用户表的用户位置。同时验证用户的位置并确定Login.java将引导用户进入哪个主菜单。因为现在我使用PHP和by json数据将数据传递给我的移动应用程序的Login.java。@user3096526添加了我对您意图的解释。@JoopEggen但是如果我有几个帖子,比如$post=$row[“state”],$post=$row[“city”]等等,然后我只需在do{and array_push($response[“posts”],$post)?
do {
$post = $row["position"]
array_push($response["posts"], $post);
} while ($row = $stmt->fetch());