drmticpet66231422
2015-12-30 08:43
浏览 115

无法将数据从android插入MySQL

Problem description

Data not able insert into MySQL but the value get displayed using `print_r($listItems).

I wanted to send arraylist(SearchResults) to MySQL from android to Php MySQL. And the issue I facing now is the data are not inserted into MySQL.

WorkDetails.java

  public void addWorkDetails(ArrayList<SearchResults> listItems)
    {
        JSONArray jsonArray = new JSONArray();
        try
        {
            for (SearchResults s : listItems)
            {
                JSONObject object= new JSONObject();
                object.put("project", s.getProject());
                String des=s.getDescription();
                String [] description=des.split(":");
                object.put("work_description", description[1]);
                Toast.makeText(getApplicationContext(), description[1],Toast.LENGTH_LONG).show();
                String per=s.getProgress();
                String [] progress=per.split(":");
                object.put("percentage", progress[1]);
                String in=s.getTimeIn();
                String []IN=in.split(":");
                object.put("timeIn", IN[1]);
                String out=s.getTimeOut();
                String []OUT=out.split(":");
                object.put("timeOut",OUT[1]);
                jsonArray.put(object);
            }
        }catch(JSONException e)
        {
            e.printStackTrace();
        }

        AddWorkDetails ru = new AddWorkDetails(jsonArray);
        ru.execute();
    }

    class AddWorkDetails extends AsyncTask<String, Void, String> {
        ProgressDialog loading;

        JSONArray jsonArray;
        AddWorkDetails(JSONArray jsonArray){
            this.jsonArray = jsonArray;
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            loading = ProgressDialog.show(WorkDetailsTable.this, "Please Wait",null, true, true);
        }

        @Override
        protected String doInBackground(String... params) {
            HashMap<String, String> data = new HashMap<String,String>();
            data.put("listItems",jsonArray.toString());
            RequestHandler rh=new RequestHandler();
            String result = rh.sendPostRequest(Config.ADD_WORKDETAILS,data);
            return  result;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            loading.dismiss();
            Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
        }
    }

Php

<?php
 if($_SERVER['REQUEST_METHOD']=='POST'){
    $mysqli = new mysqli("127.0.0.1:3307", "root", "", "androiddb");
    if ($mysqli->connect_errno) {
        echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
    }

    $listItems = json_decode($_POST['listItems'], true); 
    $sql="INSERT INTO work_details 
    (project, work_description, percentage, timeIn, timeOut) 
    VALUES 
    (?, ?, ?, ?, ?)"; 

    print_r($listItems);

    if (!($stmt = $mysqli->prepare($sql))) {
         echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
         foreach($listItems as $item){ 
            $stmt->bind_param("ssssss", $item['project'], $item['work_description'], $item['percentage'], $item['timeIn'], $item['timeOut']);
            if (!$stmt->execute()) {
                echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
            }
        } 
    }
    $mysqli->close();
}
?>

Output

enter image description here

Value get displayed using

print_r($listItems);

Config

 public static final String ADD_WORKDETAILS="http://192.168.107.115:80/Android/CRUD/addWorkDetails.php";
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongtan1845 2015-12-30 08:54
    已采纳

    You have

    $sql="INSERT INTO work_details 
    (project, work_description, percentage, timeIn, timeOut) 
    VALUES 
    (?, ?, ?, ?, ?)"; // 5 param
    

    But while binding

    $stmt->bind_param("ssssss", $item['project'], $item['work_description'], $item['percentage'], $item['timeIn'], $item['timeOut']); // 6 param
    
    点赞 评论
  • dongwei4096 2015-12-30 08:57

    The initial problem was that the bind_param was using 6 placeholders but the actual sql had only 5 fields - presumably the 6th is twf

    <?php
        if( $_SERVER['REQUEST_METHOD']=='POST' ){
            /* Typical port is 3306 */
            $mysqli = new mysqli('127.0.0.1:3307', 'root', '', 'androiddb');
    
            if ( $mysqli->connect_errno ) echo 'Failed to connect to MySQL';/* never reveal too much detail */
            else {
    
                $listItems = json_decode( $_POST['listItems'], true); 
    
                $sql='INSERT INTO `work_details` ( `project`, `work_description`, `percentage`, `timeIn`, `timeOut`, `twf` ) VALUES ( ?, ?, ?, ?, ? );'; 
                $stmt=$mysqli->prepare( $sql );
    
                if( $stmt ){
                    foreach( $listItems as $index => $item ){
    
                        $project=$item['project'];
                        $description=$item['work_description'];
                        $percentage=$item['percentage'];
                        $timeIn=$item['timeIn'];
                        $timeOut=$item['timeOut'];
                        $twf=$item['twf'];
    
                        $stmt->bind_param('ssssss', $project, $description, $percentage, $timeIn, $timeOut, $twf );
                        $res=$stmt->execute();
    
                    }
                    $stmt->close();
                }
                $mysqli->close();
            }
        }
    ?>
    
    点赞 评论
  • dongshanji3102 2015-12-30 15:04

    Code below can insert listView data to php MySQL.

     <?php
         if($_SERVER['REQUEST_METHOD']=='POST'){
            $mysqli = new mysqli("127.0.0.1:3307", "root", "", "androiddb");
            if ($mysqli->connect_errno) {
                echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
            }
    
            $listItems = json_decode($_POST['listItems'], true); 
            $sql="INSERT INTO work_details 
            (project, work_description, percentage, timeIn, timeOut) 
            VALUES 
            (?, ?, ?, ?, ?)"; 
    
            print_r($listItems);
    
            if ($stmt = $mysqli->prepare($sql)) {
                 foreach($listItems as $item){ 
                    $stmt->bind_param('sssss', $item['project'], $item['work_description'], $item['percentage'], $item['timeIn'], $item['timeOut']);
                    if (!$stmt->execute()) {
                        echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
                    }
                } 
            }
            $mysqli->close();
        }
        ?>
    
    点赞 评论

相关推荐 更多相似问题