数据插入多次 - android,php

时间:2015-12-16 07:41:51

标签: php android mysql

在我的项目中,我从Android设备发送数据,并使用php脚本将其插入数据库。但是相同的数据被插入两次。 (please see here) 我的代码出了什么问题?

机器人:

try {
    String data=URLEncoder.encode("name",  "UTF-8")+"="+URLEncoder.encode(Name,  "UTF-8");
        data+="&"+URLEncoder.encode("family",  "UTF-8")+"="+URLEncoder.encode(Family,  "UTF-8");
        data+="&"+URLEncoder.encode("city",  "UTF-8")+"="+URLEncoder.encode(City,  "UTF-8");
        data+="&"+URLEncoder.encode("ostan",  "UTF-8")+"="+URLEncoder.encode(Ostan,  "UTF-8");
        data+="&"+URLEncoder.encode("tel",  "UTF-8")+"="+URLEncoder.encode(Tel,  "UTF-8");
        data+="&"+URLEncoder.encode("sef",  "UTF-8")+"="+URLEncoder.encode(sef1,  "UTF-8");
        data+="&"+URLEncoder.encode("bod",  "UTF-8")+"="+URLEncoder.encode(bodjeh1,  "UTF-8");
        data+="&"+URLEncoder.encode("tab",  "UTF-8")+"="+URLEncoder.encode(tabgh,  "UTF-8");
        data+="&"+URLEncoder.encode("img",  "UTF-8")+"="+URLEncoder.encode(imgs,  "UTF-8");
        data+="&"+URLEncoder.encode("imgf",  "UTF-8")+"="+URLEncoder.encode(fimage,  "UTF-8");
URL link=new URL(MainActivity.url+"/app/order.php");
URLConnection con=link.openConnection();
con.setDoOutput(true);
OutputStreamWriter wrw=new OutputStreamWriter(con.getOutputStream());
wrw.write(data);
wrw.flush();
BufferedReader br=new BufferedReader(new InputStreamReader(con.getInputStream()));
StringBuilder sb=new StringBuilder();
String l="";
while((l=br.readLine())!=null){
    sb.append(l);
}
r=sb.toString();
br.close();

插入代码 - php

   $name=$_POST['name'];
    $family_name=$_POST['family'];
    $city=$_POST['city'];
    $ostan=$_POST['ostan'];
    $tel=$_POST['tel'];
    $comment=$_POST['sef'];
    $bod=$_POST['bod'];
    $tab=$_POST['tab'];
    $img=$_POST['img'];
    $imgf=$_POST['imgf'];
 $sql="INSERT INTO `customer_table`(`id`, `name`, `family_name`, `city`, `ostan`, `tel`, `comment`,`bodjeh`,`tabagheh`,`imag_f`,`image`)
     VALUES ('','$name','$family_name','$city','$ostan','$tel','$comment','$bod','$tab','$imgf','$img')";

1 个答案:

答案 0 :(得分:1)

请使用以下方法将数据发送到服务器
首先将您的请求json转换为简单的地图,如

 private JSONObject sendRequest(String urlString, Map<String, String> map, String fileKey,  File file) {
            StringBuilder strData= null;
            JSONObject resObj = null;
            try {
                Log.i("Send request", urlString+"="+map);
                URL url = new URL(urlString);
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setReadTimeout(50000);
                conn.setConnectTimeout(50000);
                conn.setRequestMethod("POST");
                conn.setUseCaches(false);
                conn.setDoInput(true);
                conn.setDoOutput(true);

                if(map == null)
                {
                    map = new HashMap<>();
                }
                MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
                for (HashMap.Entry<String, String> entry : map.entrySet()) {
                    String k = entry.getKey();
                    String v = entry.getValue();
                    reqEntity.addPart(k, new StringBody(v));
                }

                if(file != null && !TextUtils.isEmpty(fileKey))
                {
                    FileBody filebody = new FileBody(file, "image/*");
                    reqEntity.addPart(fileKey, filebody);
                }

                conn.setRequestProperty("Connection", "Keep-Alive");
                conn.addRequestProperty("Content-length", reqEntity.getContentLength() + "");
                conn.addRequestProperty(reqEntity.getContentType().getName(), reqEntity.getContentType().getValue());
                OutputStream os = conn.getOutputStream();
                reqEntity.writeTo(os);
                os.close();
                conn.connect();
                if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                    String sResponse;
                    strData = new StringBuilder();
                    while ((sResponse = reader.readLine()) != null) {
                        strData = strData.append(sResponse);
                    }
                }
                if(strData != null)
                    resObj = new JSONObject(strData.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }

            return resObj;
        }

然后使用以下方法调用Webservice。

 $result = mysql_query($sql);

 if(count($result) > 0)
 {
    echo "1"; 
 }else
 { 
   echo "0";
 } 

你需要做的PHP代码:

render()