无法将数据从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";
dsc71976
dsc71976 只要没有其他程序配置为使用该端口,就没有理由不应该在3307上运行〜上面发布的链接是一个私有的,非本地的地址,因此无法从互联网访问!
接近 5 年之前 回复
doutao1939
doutao1939 这是我的Config类http://192.168.107.11580/Android/CRUD/addWorkDetails.php
接近 5 年之前 回复
doubleyou1001
doubleyou1001 另外-insert语句有五个字段,但是你使用的是bind_param和6
接近 5 年之前 回复
duanbi3786
duanbi3786 只是好奇-你的数据库连接是使用端口3307吗?
接近 5 年之前 回复

3个回答



你有</ p>

  $ sql =“INSERT INTO work_details 
(项目, work_description,percentage,timeIn,timeOut)
VALUES
(?,?,?,?,?)“; // 5 param
</ code> </ pre>

但是在绑定时</ p>

  $ stmt-&gt; bind_param(“ssssss”,$  item ['project'],$ item ['work_description'],$ item ['percentage'],$ item ['timeIn'],$ item ['timeOut']);  // 6 param 
</ code> </ pre>
</ div>

展开原文

原文

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

duanjuhuo8772
duanjuhuo8772 好。 没问题。
接近 5 年之前 回复
dqm74406
dqm74406 我已经解决了。 谢谢。 我稍后会发布我的回答
接近 5 年之前 回复
dongyongkui6329
dongyongkui6329 它是否在第一次插入正确的数据? 可能是你需要两个嵌套循环在php中进行多次执行。 你能粘贴print_r的输出($ listItems); 这里。
接近 5 年之前 回复
dongyan3853
dongyan3853 为什么它只允许我插入一次? 我将arraylist存储在listView中,并希望将它们一起发送。
接近 5 年之前 回复
dsiuz86842
dsiuz86842 完美的工作.. sssss做了一些技巧
接近 5 年之前 回复
dsklzerpx64815631
dsklzerpx64815631 我无法看到您的图像,只需将输出粘贴到显示屏中即可
接近 5 年之前 回复
douzhaobo6488
douzhaobo6488 我改为sssss但数据仍未插入。 dbConnection是否正确?
接近 5 年之前 回复
douyi5961
douyi5961 是的,因为您的其他参数是正确的。
接近 5 年之前 回复
doushichi3678
doushichi3678 我应该删除sssss吗?
接近 5 年之前 回复



最初的问题是 bind_param </ code>使用了6个占位符,但实际的sql只有5个字段 - 大概是 第6个是 twf </ code> </ p>

 &lt;?php 
if($ _SERVER ['REQUEST_METHOD'] =='POST'){
/ *典型端口是3306 * /
$ mysqli = new mysqli('127.0.0.1:3307','root','','androiddb');

if if($ mysqli-&gt; connect_errno)echo' 无法连接到MySQL'; / 从不透露太多细节 /
其他{

$ listItems = json_decode($ _POST ['listItems'],true);

$ sql ='INSERT INTOwork_detailsprojectwork_descriptionpercentagetimeIntimeOuttwf)VALUES(?,?,?,?,?) ;';

$ stmt = $ mysqli-&gt; prepare($ sql);

if if($ stmt){
foreach($ listItems as $ index =&gt; $ item){

$ project = $ item ['project'];
$ description = $ item ['work_description'];
$ percentage = $ item ['percentage'];
$ timeIn = $ item ['timeIn'];
$ timeOut = $ item ['timeOut'];
$ twf = $ item ['twf'];

$ stmt-&gt; bind_param('ssssss',$ project,$ description,$ percentage,$ timeIn,$ timeOut,$ twf);
$ res = $ stmt-&gt; execute();

}
$ stmt-&gt; close();
}
$ mysqli-&gt; close();

}
}
?&gt;
</ code> </ pre>
</ div>

展开原文

原文

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



下面的代码可以将 listView </ code>数据插入 php MySQL </ code>。</ p>

 &lt;?php 
if($ _ SERVER ['REQUEST_METHOD'] =='POST'){
$ mysqli = new mysqli(“127.0.0.1:3307”,“root” “,”“,”androiddb“);
if($ mysqli-&gt; connect_errno){
echo”无法连接到MySQL:(“。$ mysqli-&gt; connect_errno。”)“。 $ mysqli-&gt; connect_error;
}

$ listItems = json_decode($ _ POST ['listItems'],true);

$ sql =“INSERT INTO work_details
(project,work_description,percentage,timeIn,timeOut)
VALUES
(?,?,?,?,?)”;

print_r($ listItems);

if if($ stmt = $ mysqli-&gt; prepare($ sql)){
foreach($ listItems as $ item){
$ stmt-&gt; bind_param ('sssss',$ item ['project'],$ item ['work_description'],$ item ['percentage'],$ item ['timeIn'],$ item ['timeOut']);
if(n !$ stmt-&gt; execute()){
echo“执行失败:(”。$ stmt-&gt; errno。“)”。 $ stmt-&gt;错误;
}
}
}
$ mysqli-&gt; close();
}
?&gt;
</ code> </ pre>
</ div >

展开原文

原文

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

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐