在我的Android应用程序中。当我更新团队的分数时,数据库中的所有内容都会更新。如何将其设置为一次更新一行。我可以通过id更新吗?如果是的话我该怎么办?谢谢!
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);
?>
这是我的安卓代码
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;
}
这是我的表
答案 0 :(得分:0)
您需要在SQL查询中使用要更新的行的ID。
在PHP中它应该是:
<?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 ...
在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 ...