将数据更新到现有 sql 数据库中


Update data into existing sql database

我有一个应用程序,允许用户注册并将他们的数据保存到网络服务器,但是我在用户注册后无法访问他们保存的数据。我想做的是让他们注册,然后他们可以更新有关他们自己的更多信息,这是我从应用程序内调用的注册类 php 文件。

<?php
$con = mysqli_connect("xxx", "xxx", "xxx", "xxx");

    $username = $_POST["username"];
$password = $_POST["password"];
$statement = mysqli_prepare($con, "INSERT INTO user (username, password) VALUES (?, ?)");
mysqli_stmt_bind_param($statement, "ss", $username, $password);
mysqli_stmt_execute($statement);
$response = array();
$response["success"] = true;  
print_r(json_encode($response));
?>

这工作正常,并将用户名和密码保存到我的网络服务器上的新用户中。一旦他们登录,我将如何将新数据输入到他们在网络服务器中的特定条目中。有什么指示吗?

你必须执行()结果,结果是你的连接准备。

将您的代码编辑为此

第一个创建函数:

function saveUser($username, $password) {
    $cp = $this->conn->prepare("INSERT INTO user(username, password) VALUES(?, ?)");
    $cp->bind_param("ss",  $username, $password);
    $result = $cp->execute();
    $cp->close();
    if ($result) {
        $cp= $this->conn->prepare("SELECT * FROM user WHERE username = ?");
        $cp->bind_param("s", $username);
        $cp->execute();
        $user = $cp->get_result()->fetch_assoc();
        $cp->close();
        return $user;
    } else {
        return false;
    }}
你的

错误之一是返回你的结果或$user

然后你可以像这样调用这个类或函数:

    $user = saveUser($name, $email, $password, $phone);
    if ($user) {
        $response["error"] = FALSE;
        $response["uid"] = $user["unique_id"];
        $response["user"]["name"] = $user["name"];
        $response["user"]["email"] = $user["email"];
        $response["user"]["phone"] = $user["phone"];
        $response["user"]["password"] = $user["password"];
        $response["user"]["created_at"] = $user["created_at"];
        $response["user"]["updated_at"] = $user["updated_at"];
        echo json_encode($response);
    }