首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用php将字符串日期列表插入到mysql中?

如何使用php将字符串日期列表插入到mysql中?
EN

Stack Overflow用户
提问于 2018-08-19 22:09:42
回答 1查看 43关注 0票数 0

我想在mysql中使用php和im在两个日期之间插入日期列表,使用asynctask在php中发布数据。首先,我获取两个日期字符串,并从两个日期中获取日期列表

代码语言:javascript
复制
List<Date> dates = getDates(mDate, mDate2);

private static List<Date> getDates(String dateString1, String dateString2)
{
    ArrayList<Date> dates = new ArrayList<Date>();
    DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");

    Date date1 = null;
    Date date2 = null;

    try {
        date1 = df1 .parse(dateString1);
        date2 = df1 .parse(dateString2);
    } catch (ParseException e) {
        e.printStackTrace();
    }

    Calendar cal1 = Calendar.getInstance();
    cal1.setTime(date1);


    Calendar cal2 = Calendar.getInstance();
    cal2.setTime(date2);

    while(!cal1.after(cal2))
    {
        dates.add(cal1.getTime());
        cal1.add(Calendar.DATE, 1);
    }
    return dates;
}

去找其他的帕玛拉特

代码语言:javascript
复制
String customername = getPpname();
String customerroom = getRrname();
String customerid = getRrid();

并创建一个容器,在asycntask中传递多个参数

代码语言:javascript
复制
private static class MYTASK {
    List<Date> dates;
    String customername;
    String customerroom;
    String customerid;

    MYTASK(List<Date> dates, String customername, String customerroom, String customerid) {
        this.dates = dates;
        this.customername = customername;
        this.customerroom = customerroom;
        this.customerid = customerid;
    }
}

然后执行asyctank

代码语言:javascript
复制
MYTASK params = new MYTASK(dates, customername, customerroom,customerid);
        JSONTask_ListDates jsonTaskListDates = new JSONTask_ListDates();
        jsonTaskListDates.execute(params);

这是我的异步任务

代码语言:javascript
复制
class JSONTask_ListDates extends AsyncTask<MYTASK, Void, Void> {

    @Override
    protected void onPreExecute() {
        /* rlllogin.setVisibility(View.VISIBLE);*/
    }
    @Override
    protected void doInBackground(MYTASK... params) {
        String urlreserve = "http://alar-regulations.000webhostapp.com/reservation_insert_date.php";

        String id = params[3].customerid;
        String rname = params[1].customerroom;
        String pname = params[2].customername;
        List<Date> dates = params[0].dates;


        try {
            URL url = new URL(urlreserve);
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            httpURLConnection.setRequestMethod("POST");
            httpURLConnection.setDoOutput(true);
            //new
            httpURLConnection.setDoInput(true);
            OutputStream OS = httpURLConnection.getOutputStream();
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));

            String data = URLEncoder.encode("id", "UTF-8")+"="+URLEncoder.encode(id, "UTF-8")+"&"
                    +URLEncoder.encode("rname", "UTF-8")+"="+URLEncoder.encode(rname, "UTF-8")+"&"
                    +URLEncoder.encode("pname", "UTF-8")+"="+URLEncoder.encode(pname, "UTF-8")+"&"
                    +URLEncoder.encode("dates", "UTF-8")+"="+URLEncoder.encode(dates, "UTF-8");

            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            OS.close();
            InputStream IS = httpURLConnection.getInputStream();
            //new
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(IS, "iso-8859-1"));
            String response ="";
            String line = "";
            while ((line = bufferedReader.readLine()) !=null) {
                response+=line;
            }
            bufferedReader.close();
            IS.close();
            return response;

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
        /*pDialog.dismiss();*/
        /* rlllogin.setVisibility(View.GONE);*/
        Intent intent = new Intent(Booking.this, Reservation_List.class);
        startActivity(intent);
        Booking.this.finish();
        /* dialog2.dismiss();*/
    }
}

我的异步任务中有一个错误,列表日期= params.dates无法在URLEncoder中传递,它说

代码语言:javascript
复制
wrong first argument type found java.util.list required java.lang string

在我的php中

代码语言:javascript
复制
            <?php
require "connection.php";

$res_rname= $_POST["rname"];
$res_per_name= $_POST["pname"];
$res_id= $_POST["id"];
$dates= $_POST["dates"];
$sql_query = "Insert into res_event_table ( res_id, res_check_in_out, 
res_room_name, res_name) values ( '$res_id','$val', '$res_rname', 
'$res_per_name')";

if($dates>0)
 {
    foreach($dates as $val){
        $result = mysqli_query($conn ,$sql_query);
            if (!$result){
            echo "failed" .mysqli_connect_error;
            }else{
            echo "Reservation Success";
         }
       }
    }

 ?>

我还没试过php,因为我的java还是有问题。请帮我驱散

EN

回答 1

Stack Overflow用户

发布于 2018-08-20 01:39:26

如果你给出更多的信息性问题,它将很容易帮助你。如果您在使用ArrayList字符串时遇到问题,我可以建议您仅将其转换为String。将其作为String传递,然后在String[]中再次拆分它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51918483

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档