如何使用php将字符串日期列表插入mysql?
我想使用php在mysql中插入两个日期之间的日期列表 我使用asynctask在php中发布数据。 首先,我获取两个日期字符串,并从2个日期中获取日期列表如何使用php将字符串日期列表插入mysql?,php,android,Php,Android,我想使用php在mysql中插入两个日期之间的日期列表 我使用asynctask在php中发布数据。 首先,我获取两个日期字符串,并从2个日期中获取日期列表 List<Date> dates = getDates(mDate, mDate2); private static List<Date> getDates(String dateString1, String dateString2) { ArrayList<Date> dates = new
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;
}
并创建一个容器以在AsycntTask中传递多个参数
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;
}
}
这是我的任务
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();*/
}
}
在我的php中
<?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";
}
}
}
?>
如果你提出更多的信息性问题,你会很容易得到帮助。如果您对ArrayList
字符串有问题,我建议您将其转换为String
。将其作为字符串传递
,然后将其拆分为字符串[]
这个问题是关于php、android还是mysql的?另外,让我们看看你尝试了什么。我编辑了我的问题。如果你知道答案,请帮助我,这是关于android和php的
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();*/
}
}
wrong first argument type found java.util.list required java.lang string
<?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";
}
}
}
?>