douxitao8170 2015-12-30 21:18
浏览 35
已采纳

将分数值发送到数据库

Hi so I have a database set up that looks like this https://gyazo.com/240fce43cfcf45f123d64b76acc4093f , however, I don't know how to interact with the score element. To use the ID, Name etc. I followed a youtube tutorial by TechAcademy, where she uses PHP to interact with the database. In the java file of my android studio project, when a button is clicked and certain conditions are met, a counter increments and the activity is restarted, however I want this counter to be sent straight to my database so the users score increments (Example is user clicks the button and the conditions are met, a 1 will be displayed in the database). My php files are as follows for the User, Name etc if they're any help.

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

?>

Fetch User Data.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条回答 默认 最新

  • douqiao8032 2015-12-30 21:36
    关注

    You need to have another php script to handle score update. And it will be using MySQL 'Update' statement instead. For example:

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

    ?>

    Very rough example. But should show you enough to get started.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)