在尝试登录或从我的Android应用程序注册时,我在PHP中不断收到错误未定义索引。我尝试更改PHP版本,但它没有解决问题。这是我在error_log中得到的错误:
class user
{
public function login()
{
$data = [];
require $_SERVER['DOCUMENT_ROOT'].'/socialnetwork-api/Config/db.php';
$DB = new DB();$db=$DB->connection;
$username =htmlentities($_POST['username'],ENT_QUOTES,"UTF-8");
$password =htmlentities($_POST['password'],ENT_QUOTES,"UTF-8");
$sql = "SELECT * FROM `tbl_users` WHERE `username`='$username' AND `password`='$password'";
$result = $db->query($sql);
$result = $result->fetch();
if($result != null)
{
$data["result"]=$result['id'];
}
else
{
$data["result"]="0";
}
echo json_encode($data);
}
public function signup()
{
$data = [];
require $_SERVER['DOCUMENT_ROOT'].'/socialnetwork-api/Config/db.php';
$DB = new DB();$db=$DB->connection;
$fullname = htmlentities($_POST['full_name'],ENT_QUOTES,"UTF-8");
$username = htmlentities($_POST['username'],ENT_QUOTES,"UTF-8");
$email = $_POST['email'];
$password = htmlentities($_POST['password'],ENT_QUOTES,"UTF-8");
$sql = "SELECT * FROM `tbl_users` WHERE `username`='$username'";
$result = $db->query($sql);
$result = $result->fetch();
if($result != null)
{
$data["result"] = "username";
}
else
{
$sql = "SELECT * FROM `tbl_users` WHERE `email`='$email'";
$result = $db->query($sql);
$result = $result->fetch();
if($result != null)
{
$data["result"] = "email";
}
else
{
$sql = "INSERT INTO `tbl_users` (`full_name`,`username`,`email`,`password`) VALUES ('$fullname','$username','$email','$password')";
$result = $db->prepare($sql);
$result = $result->execute();
if($result)
{
$data["result"]="1";
}
else
{
$data["result"]="0";
}
}
}
echo json_encode($data);
}
以下是我的PHP代码:
{{1}}
我希望有人帮助我,因为我对PHP不太熟悉,而且我在大学的项目中需要这个。
答案 0 :(得分:0)
未定义的索引错误对应于您在班级中使用WITH
dataset
AS
(SELECT 'T1' AS child_table, 'T0' AS parent_table
FROM DUAL
UNION ALL
SELECT 'X1' AS child_table, 'T0' AS parent_table
FROM DUAL
UNION ALL
SELECT 'X2' AS child_table, 'T0' AS parent_table
FROM DUAL
UNION ALL
SELECT 'T2' AS child_table, 'T1' AS parent_table
FROM DUAL
UNION ALL
SELECT 'Y1' AS child_table, 'T1' AS parent_table
FROM DUAL
UNION ALL
SELECT 'Y2' AS child_table, 'X1' AS parent_table
FROM DUAL
UNION ALL
SELECT 'T3' AS child_table, 'T2' AS parent_table
FROM DUAL
UNION ALL
SELECT 'T4' AS child_table, 'T3' AS parent_table
FROM DUAL),
findset (parent_table, child_table)
AS
(SELECT parent_table, child_table
FROM dataset
WHERE child_table = 'T4'
UNION ALL
SELECT dataset.parent_table, dataset.child_table
FROM findset INNER JOIN dataset ON dataset.child_table =
findset.parent_table)
SELECT *
FROM findset;
。这意味着当您调用这些类函数时,不会设置这些值。
通常,如果您要使用$_POST
,您将拥有一个调用PHP脚本的表单,并且每个表单输入都对应一个$_POST
值。
例如,如果您的表单包含输入,例如:
$_POST
然后,当用户提交表单时,您可以使用<input type="text" name="username" value="">
获取值。
但是,如果您没有提交表单,那么您将没有任何$_POST['username']
值。因此未定义的索引错误。
另一种方法是将您需要的值传递给类方法。然后,您需要确保在调用方法时传递这些值。
示例:
$_POST
然后您可以传递数据:
public function login($username = '', $password = '')
{
$data = [];
require $_SERVER['DOCUMENT_ROOT'].'/socialnetwork-api/Config/db.php';
$DB = new DB();$db=$DB->connection;
$username =htmlentities($username,ENT_QUOTES,"UTF-8");
$password =htmlentities($password,ENT_QUOTES,"UTF-8");
$sql = "SELECT * FROM `tbl_users` WHERE `username`='$username' AND `password`='$password'";
$result = $db->query($sql);
$result = $result->fetch();
if($result != null)
{
$data["result"]=$result['id'];
}
else
{
$data["result"]="0";
}
echo json_encode($data);
}
然后您可以传递$class = new user();
$class->login($username, $password);
和$username
的值。