gpt4 book ai didi

php - 无法通过 mysqli_connect 存储数据

转载 作者:搜寻专家 更新时间:2023-11-01 08:43:27 24 4
gpt4 key购买 nike

大家好,目前我正在尝试为 Android 应用程序创建登录 Activity ,但我不知道为什么在注册时不存储用户数据。是不是下图的php文件有问题?

Register.php 存储用户注册信息

<?php
$con = mysqli_connect("mysql13.000webhost.com", "a1641537_fetch", "password", "a1641537_fetchdb");

$name = $_POST["name"];
$username = $_POST["username"];
$password = $_POST["password"];

$statement = mysqli_prepare($con, "INSERT INTO Fetch (name, username, password) VALUES (?, ?, ?) ");

mysqli_stmt_bind_param($statement, "sss", $name, $username, $password);
mysqli_stmt_execute($statement);

mysqli_stmt_close($statement);

mysqli_close($con);
?>

FetchUserData.php 获取用户的用户名和密码。

<?php
$con = mysqli_connect("mysql13.000webhost.com", "a1641537_fetch", "password", "a1641537_fetchdb");

$username = $_POST["username"];
$password = $_POST["password"];

$statement = mysqli_prepare($con, "SELECT * FROM Fetch WHERE username = ? AND password = ?");
mysqli_stmt_bind_param($statement, "ss", $username, $password);
mysqli_stmt_execute($statement);

mysqli_stmt_store_result($statement);
mysqli_stmt_bind_result($statement, $name, $username, $password);

$user = array();

while(mysqli_stmt_fetch($statement)) {
$user[name] = $name;
$user[username] = $username;
$user[password] = $password;
}

echo json_encode($user);

mysqli_stmt_close($statement);

mysqli_close($con);
?>

最佳答案

fetch 是一个 reserved word在 mysql 中,所以你需要在反引号中引用它。

例如:

$statement = mysqli_prepare($con, "INSERT INTO `Fetch` (name, username, password) VALUES (?, ?, ?) ");

要捕获这些类型的错误,添加错误处理很有用。我更喜欢通过让 msyqli 在出现任何问题时抛出异常来做到这一点。

要激活它,只需将 mysqli_report(MYSQLI_REPORT_STRICT); 添加到脚本顶部并确保显示错误。

如果您自己不添加任何 try - catch block ,php 将在出现问题时向您显示详细的未处理的异常错误,以便您可以从那里获取信息。显然,在实时代码中,这不是处理该问题的首选方式。

关于php - 无法通过 mysqli_connect 存储数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30661381/

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