gpt4 book ai didi

java - 将分值发送到数据库

转载 作者:行者123 更新时间:2023-11-29 21:40:47 26 4
gpt4 key购买 nike

嗨,我设置了一个如下所示的数据库 https://gyazo.com/240fce43cfcf45f123d64b76acc4093f ,但是,我不知道如何与分数元素交互。为了使用 ID、名称等,我遵循了 TechAcademy 的 YouTube 教程,其中她使用 PHP 与数据库交互。在我的 android studio 项目的 java 文件中,当单击按钮并满足某些条件时,计数器会递增并重新启动 Activity ,但是我希望将此计数器直接发送到我的数据库,以便用户得分递增(示例是用户单击按钮并且满足条件,数据库中将显示 1)。我的 php 文件如下,用于用户、名称等(如果有帮助的话)。

注册.php

<?php
$con = mysqli_connect("your_mysql_hostname" , "your_username" , "your_password" , "your_database_name");

$name = $_POST["Name"];
$email = $_POST["Email"];
$username = $_POST["Username"];
$password = $_POST["Password"];


$insertquery = mysqli_prepare($con , "INSERT INTO contacts ( Name ,Email,Username,Password) VALUES (?,?,?,?)");
mysqli_stmt_bind_param($insertquery , "ssss" , $name , $email , $username , $password);
mysqli_stmt_execute($insertquery);


mysqli_stmt_close($insertquery);

mysqli_close($con);

?>

获取用户数据.PHP

<?php

$con = mysqli_connect("your_mysql_hostname" , "your_username" , "your_password" , "your_database_name");

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

$selectquery = mysqli_prepare($con , "SELECT * FROM contacts WHERE Username = ? AND Password = ?");
mysqli_stmt_bind_param($selectquery , "ss" , $username , $password);
mysqli_stmt_execute($selectquery);

mysqli_stmt_store_result($selectquery);
mysqli_stmt_bind_result($selectquery , $ID , $name , $email,$username , $password);

$user = array();

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

echo json_encode($user);

mysqli_stmt_close($selectquery);
mysqli_close($con);
?>

最佳答案

您需要另一个 php 脚本来处理分数更新。它将使用 MySQL 'Update' 语句来代替。例如:

<?php
$con = mysqli_connect("your_mysql_hostname" , "your_username" , "your_password" , "your_database_name");

$username = $_POST["Username"];
$score= $_POST["Score"];


$query = mysqli_prepare($con , "UPDATE contacts SET score=? WHERE username=?");
mysqli_stmt_bind_param($query, $score, $username);
mysqli_stmt_execute($query);


mysqli_stmt_close($query);

mysqli_close($con);

?>

非常粗略的例子。但应该向您展示足以开始使用的内容。

关于java - 将分值发送到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34537392/

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