Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

In my android app. When i update the scores of teams, everything in the database updates. How do i set this to update one row at a time. Can i update this by id? if so how can i do it? Thank you!

PHP

<?php 

 $teamone = $_POST["teamone"];
 $teamtwo = $_POST["teamtwo"];
 $teamonepts = $_POST["teamonepts"];
 $teamtwopts = $_POST["teamtwopts"];



 require_once('init.php');


 $sql = "UPDATE matches SET teamone = '$teamone',  teamonepts = '$teamonepts', teamtwo = '$teamtwo', teamtwopts = '$teamtwopts'";


 if(mysqli_query($con,$sql)){
 echo 'Updated';
 }else{
 echo 'Failed';
 }


 mysqli_close($con);

?>

Here is my android code

else if(method.equals("send"))
        {

            String teamone = params[1];
            String teamonepts = params[2];
            String teamtwo = params[3];
            String teamtwopts = params[4];
            try {
                URL url = new URL(send_url);
                HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                httpURLConnection.setRequestMethod("POST");
                httpURLConnection.setDoOutput(true);
                OutputStream os = httpURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
                String data = URLEncoder.encode
                        ("teamone", "UTF-8")+"="+ URLEncoder.encode(teamone, "UTF-8")+"&"+
                        URLEncoder.encode("teamonepts", "UTF-8")+"="+ URLEncoder.encode(teamonepts, "UTF-8")+"&"+
                        URLEncoder.encode("teamtwo", "UTF-8")+"="+ URLEncoder.encode(teamtwo, "UTF-8")+"&"+
                URLEncoder.encode("teamtwopts", "UTF-8")+"="+ URLEncoder.encode(teamtwopts, "UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                os.close();
                InputStream IS = httpURLConnection.getInputStream();
                IS.close();
                httpURLConnection.disconnect();

                return "Data Sent";

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }


        }
        return null;
    }

Here is my table

Here is my table

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
134 views
Welcome To Ask or Share your Answers For Others

1 Answer

You need to use ID of the row you want to update in your SQL query.

In PHP it should be like:

<?php

$m_id = $_POST["m_id"];
$teamone = $_POST["teamone"];
$teamtwo = $_POST["teamtwo"];
$teamonepts = $_POST["teamonepts"];
$teamtwopts = $_POST["teamtwopts"];

// ... your code ...

$sql = "UPDATE matches SET teamone = '$teamone',  teamonepts = '$teamonepts', teamtwo = '$teamtwo', teamtwopts = '$teamtwopts' WHERE m_id = '$m_id'";

// ... your code ...

And in Android:

// ... your code ...
String m_id = params[0]; // I assume you have an ID as first element of params
String teamone = params[1];
String teamonepts = params[2];
String teamtwo = params[3];
String teamtwopts = params[4];

// ... your code ...

String data = URLEncoder.encode("m_id", "UTF-8")+"="+ URLEncoder.encode(m_id, "UTF-8")+"&"+
URLEncoder.encode("teamone", "UTF-8")+"="+ URLEncoder.encode(teamone, "UTF-8")+"&"+
URLEncoder.encode("teamonepts", "UTF-8")+"="+ URLEncoder.encode(teamonepts, "UTF-8")+"&"+
URLEncoder.encode("teamtwo", "UTF-8")+"="+ URLEncoder.encode(teamtwo, "UTF-8")+"&"+
URLEncoder.encode("teamtwopts", "UTF-8")+"="+ URLEncoder.encode(teamtwopts, "UTF-8");

// ... your code ...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...