0

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";
4
  • just curious - is your db connection is uing port 3307? Commented Dec 30, 2015 at 8:49
  • 1
    Also - the insert statement has five fields but you are using bind_param with 6 Commented Dec 30, 2015 at 8:52
  • @RamRaider This is my Config class http://192.168.107.115:80/Android/CRUD/addWorkDetails.php Commented Dec 30, 2015 at 8:56
  • There is no reason that it shouldn't run on 3307 so long as no other program has been configured to use that port too ~ the link you posted above is a private, non-natted address and thus not accessible from the interwebs! Commented Dec 30, 2015 at 9:00

3 Answers 3

2

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
Sign up to request clarification or add additional context in comments.

6 Comments

should I remove to sssss?
I changed to sssss but data still not inserted. Did the dbConnection is correct?
I am not able to see your image can you just paste your output here as it is in your display
why it only allows me to insert one time ? I stored the arraylist in listView and want to send them together.
is it inserting correct data in first time? May be you need two nested loops in php for multiple execution. Can you please paste your output of print_r($listItems); here.
|
1

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();
        }
    }
?>

Comments

0

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();
    }
    ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.