将分数值发送到数据库

时间:2015-12-30 21:18:51

标签: java php android mysql mysqli

大家好我有一个类似于https://gyazo.com/240fce43cfcf45f123d64b76acc4093f的数据库设置,但是,我不知道如何与得分元素进行交互。要使用ID,Name等,我按照TechAcademy的youtube教程,在那里她使用PHP与数据库进行交互。 在我的android studio项目的java文件中,当单击一个按钮并满足某些条件时,计数器会递增并重新启动活动,但是我希望将此计数器直接发送到我的数据库,以便用户得分增加(例如用户单击按钮并满足条件,数据库中将显示1)。如果他们有任何帮助,我的php文件如下:User,Name等。

Register.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);
?>

1 个答案:

答案 0 :(得分:0)

您需要另外一个PHP脚本来处理分数更新。它将使用MySQL的“更新”语句。例如:

<?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);

&GT;

非常粗略的例子。但是应该告诉你足够的开始。