dongtui8593 2015-04-22 22:20
浏览 41

PHP从android应用程序中将空白行插入mysql表

I am inserting json data from my android app in my device into xampp server in my computer using POST. It simply inserts row with empty column. When I enter the column from my browser using GET it converts it to numbers only eventhough the data is mix of numbers and letters. The type of the table column is varbinary. I entered 'crap' from my browser and it was inserted as '63726170' in the table. I am puzzled by this. Here is the PHP code that inserts the data.

if (isset($_POST)){
    //sanitize the input
    filter_var_array($_POST);
    $ri=$_POST['regID'];
    $id=json_decode($ri);
    //$decoid= $id->regID;
    if(is_array($id)){
    foreach ($id as $key=>$value){
        $fu=$id[$key];
        };
    }
    try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "INSERT INTO regids (regID)
    VALUES ('$fu')";
    // use exec() because no results are returned
    $conn->exec($sql);
    echo "New record created successfully";
        }
    catch(PDOException $e)
    {
    echo $sql . "<br>" . $e->getMessage();
    }
   }else{
        echo "Error executing query!!!";
    }    
    $conn = null;

I am adding my android code that is sending the data to the server

URL url;
                    HttpURLConnection urlConn;
                    DataOutputStream printout;
                    url = new URL ("myurlhere");
                    urlConn = (HttpURLConnection)url.openConnection();
                    urlConn.setDoInput (true);
                    urlConn.setDoOutput (true);
                    urlConn.setUseCaches (false);
                    urlConn.setRequestProperty("Content-Type","application/json");   
                    urlConn.setRequestProperty("Accept", "application/json");
                    urlConn.setRequestMethod("POST");
                    urlConn.connect();

                    /*List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("regID", result));*/
                    String result = args.toString();
                    try{
                    //Create JSONObject here
                    JSONObject jsonParam = new JSONObject();
                    jsonParam.put("regID", result);
                    String postData="json="+jsonParam.toString();


                    // Send POST output.
                    printout = new DataOutputStream(urlConn.getOutputStream ());
                    printout.writeUTF(URLEncoder.encode(jsonParam.toString(),"UTF-8"));
                    Log.i("NOTIFICATION", "Data Sent");
                    printout.flush ();
                    printout.close ();

                    OutputStreamWriter os = new OutputStreamWriter(urlConn.getOutputStream(), "UTF-8");
                    os.write(postData);
                    Log.i("NOTIFICATION", "Data Sent");



                    BufferedReader reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream())); 
                    String msg=""; 
                    String line = ""; 
                    while ((line = reader.readLine()) != null) {
                        msg += line; } 
                    Log.i("msg=",""+msg);

                    os.close();
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 opencv图像处理,需要四个处理结果图
    • ¥15 无线移动边缘计算系统中的系统模型
    • ¥15 深度学习中的画图问题
    • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
    • ¥15 Python报错怎么解决
    • ¥15 simulink如何调用DLL文件
    • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
    • ¥30 线性代数的问题,我真的忘了线代的知识了
    • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
    • ¥188 需要修改一个工具,懂得汇编的人来。