PHP 错误一次插入导致两行 ANDROID
PHP ERROR One insert is causing two rows ANDROID
我的脚本有问题php
当我点击按钮 "Inscrire" 时,它调用了我的请求 INSERT,但他在我的 table 中创建了两行相同的数据。
这是我的代码php:
<?php
require_once('assistmysql.php');
$id_activite = $_POST['id_activite'];
$id_users = $_POST['id_users'];
$etat = $_POST['etat'];
if($etat == '1')
{
$sql = "INSERT INTO inscription_activite (id_activite,id_users) VALUES ('$id_activite','$id_users')";
$result=$con->query($sql);
if(mysqli_query($con, $sql))
{
echo'info enregistrée !!!!';
}
else
{
echo'dommage..';
}
mysqli_close($con);
}
else if ($etat == '2')
{
$sql = "DELETE FROM inscription_activite WHERE id_users = '$id_users' AND id_activite = '$id_activite'";
$result=$con->query($sql);
if(mysqli_query($con, $sql))
{
echo'vous etes bien desinscrit !';
}
else
{
echo'quelle tristesse..';
}mysqli_close($con);
}
?>
这是我的代码 android :
public class BackgroundWorker extends AsyncTask<String,Void,String> {
Context context;
AlertDialog alertDialog;
BackgroundWorker (Context ctx) {
context = ctx;
}
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "http://192.168.20.135/scriptphp/redirectionactivite.php";
if(type.equals("inscrire")) {
try {
String etat = params [1];
String id_user = params [2];
String id_activite = params[3];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data =URLEncoder.encode("etat", "UTF-8")+"="+URLEncoder.encode(etat,"UTF-8")+"&"
+URLEncoder.encode("id_users", "UTF-8")+"="+URLEncoder.encode(id_user,"UTF-8")+"&"
+URLEncoder.encode("id_activite", "UTF-8")+"="+URLEncoder.encode(id_activite,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";
while((line = bufferedReader.readLine())!= null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
else if (type.equals("desinscrire"))
{
try {
String etat = params [1];
String id_user = params [2];
String id_activite = params[3];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data =URLEncoder.encode("etat", "UTF-8")+"="+URLEncoder.encode(etat,"UTF-8")+"&"
+URLEncoder.encode("id_users", "UTF-8")+"="+URLEncoder.encode(id_user,"UTF-8")+"&"
+URLEncoder.encode("id_activite", "UTF-8")+"="+URLEncoder.encode(id_activite,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";
while((line = bufferedReader.readLine())!= null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Info status");
}
protected void onPostExecute(String result) {
alertDialog.setMessage(result);
alertDialog.show();
}
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
在您的 PHP 代码中,您似乎播放了两次查询:
$result=$con->query($sql);
if(mysqli_query($con, $sql))
要么:
$result = $con->query($sql);
if ($result) {
或:
if(mysqli_query($con, $sql)) {
我的脚本有问题php 当我点击按钮 "Inscrire" 时,它调用了我的请求 INSERT,但他在我的 table 中创建了两行相同的数据。
这是我的代码php:
<?php
require_once('assistmysql.php');
$id_activite = $_POST['id_activite'];
$id_users = $_POST['id_users'];
$etat = $_POST['etat'];
if($etat == '1')
{
$sql = "INSERT INTO inscription_activite (id_activite,id_users) VALUES ('$id_activite','$id_users')";
$result=$con->query($sql);
if(mysqli_query($con, $sql))
{
echo'info enregistrée !!!!';
}
else
{
echo'dommage..';
}
mysqli_close($con);
}
else if ($etat == '2')
{
$sql = "DELETE FROM inscription_activite WHERE id_users = '$id_users' AND id_activite = '$id_activite'";
$result=$con->query($sql);
if(mysqli_query($con, $sql))
{
echo'vous etes bien desinscrit !';
}
else
{
echo'quelle tristesse..';
}mysqli_close($con);
}
?>
这是我的代码 android :
public class BackgroundWorker extends AsyncTask<String,Void,String> {
Context context;
AlertDialog alertDialog;
BackgroundWorker (Context ctx) {
context = ctx;
}
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "http://192.168.20.135/scriptphp/redirectionactivite.php";
if(type.equals("inscrire")) {
try {
String etat = params [1];
String id_user = params [2];
String id_activite = params[3];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data =URLEncoder.encode("etat", "UTF-8")+"="+URLEncoder.encode(etat,"UTF-8")+"&"
+URLEncoder.encode("id_users", "UTF-8")+"="+URLEncoder.encode(id_user,"UTF-8")+"&"
+URLEncoder.encode("id_activite", "UTF-8")+"="+URLEncoder.encode(id_activite,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";
while((line = bufferedReader.readLine())!= null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
else if (type.equals("desinscrire"))
{
try {
String etat = params [1];
String id_user = params [2];
String id_activite = params[3];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data =URLEncoder.encode("etat", "UTF-8")+"="+URLEncoder.encode(etat,"UTF-8")+"&"
+URLEncoder.encode("id_users", "UTF-8")+"="+URLEncoder.encode(id_user,"UTF-8")+"&"
+URLEncoder.encode("id_activite", "UTF-8")+"="+URLEncoder.encode(id_activite,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";
while((line = bufferedReader.readLine())!= null) {
result += line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Info status");
}
protected void onPostExecute(String result) {
alertDialog.setMessage(result);
alertDialog.show();
}
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}
在您的 PHP 代码中,您似乎播放了两次查询:
$result=$con->query($sql);
if(mysqli_query($con, $sql))
要么:
$result = $con->query($sql);
if ($result) {
或:
if(mysqli_query($con, $sql)) {