我正在尝试制作一个可以将日期和时间保存到mysql中的android应用程序。当我尝试保存时出现错误。这是android代码:
public class CustomerBookingActivity extends AppCompatActivity {
private static final String TAG = "CustomerBookingActivity";
private Button pickupDate,pickupTime;
private DatePickerDialog.OnDateSetListener onDateSetListenerPickup;
private DatePickerDialog.OnDateSetListener onDateSetListenerDropoff;
private TextView viewPickupDate,viewPickupTime;
private Button submitBooking;
String format;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customer_booking);
pickupDate=(Button) findViewById(R.id.buttonPickupDate);
viewPickupDate=(TextView) findViewById(R.id.viewPickupDate);
pickupTime =(Button) findViewById(R.id.buttonPickupTime) ;
viewPickupTime=(TextView) findViewById(R.id.viewPickupTime);
submitBooking = (Button) findViewById(R.id.submitDateTime);
pickupDate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar cal = Calendar.getInstance();
int year = cal.get(Calendar.YEAR);
int month = cal.get(Calendar.MONTH);
int day = cal.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(CustomerBookingActivity.this,android.R.style.Theme_Holo_Light_Dialog_MinWidth,
onDateSetListenerPickup,year,month,day);
datePickerDialog.show();
datePickerDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
datePickerDialog.show();
}
});
pickupTime.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar time1 = Calendar.getInstance();
int hour= time1.get(Calendar.HOUR);
int minute=time1.get(Calendar.MINUTE);
selectedTimeFormat(hour);
final TimePickerDialog timePickerDialog= new TimePickerDialog(CustomerBookingActivity.this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
viewPickupTime.setText(hourOfDay + " : " + minute);
}
},hour,minute,true);
timePickerDialog.show();
}
});
onDateSetListenerPickup = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
month = month + 1;
Log.d(TAG, "onDateSet: yyyy/mm/dd: " + year + "/" + month + "/" + day);
String date = year + "/" + month + "/" + day;
viewPickupDate.setText(date);
}
};
onDateSetListenerDropoff = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
month = month + 1;
Log.d(TAG, "onDateSet: yyyy/mm/dd: " + year + "/" + month + "/" + day);
String date = year + "/" + month + "/" + day;
viewDropoffDate.setText(date);
}
};
submitBooking.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AddBooking();
}
});
}
public void selectedTimeFormat(int hour) {
if (hour == 0) {
hour += 12;
format = "AM";
} else if (hour == 12) {
format = "PM";
} else if (hour > 12) {
hour -= 12;
format = "PM";
} else {
format = "AM";
}
}
public void AddBooking(){
final String pickupDate = this.viewPickupDate.getText().toString().trim();
final String pickupTime = this.viewPickupTime.getText().toString().trim();
final String dropoffDate = this.viewDropoffDate.getText().toString().trim();
final String dropoffTime = this.viewDropoffTime.getText().toString().trim();
StringRequest stringRequest = new StringRequest(Request.Method.POST, ConstantURL.URL_CREATE_BOOKING,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try{
JSONObject jsonObject = new JSONObject(response);
String success = jsonObject.getString("success");
if (success.equals("1")) {
Toast.makeText(CustomerBookingActivity.this, "Success!", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(CustomerBookingActivity.this, "Error! " + e.toString(), Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(CustomerBookingActivity.this, "Error! " + error.toString(), Toast.LENGTH_SHORT).show();
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<>();
params.put("pickupDate",pickupDate);
params.put("pickupTime",pickupTime);
params.put("dropoffDate",dropoffDate);
params.put("dropoffTime",dropoffTime);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
}
这是我的php代码:
<?php
if ($_SERVER['REQUEST_METHOD'] =='POST'){
$pickup_date = $_POST['pickup_date'];
$pickup_time = $_POST['pickup_time'];
$reference_no = uniqid();
require_once 'connect.php';
$sql = "INSERT INTO booking (reference_no, pickup_date, pickup_time) VALUES ('$reference_no', '$pickup_date','$pickup_time')";
if ( mysqli_query($conn, $sql) ) {
$result["success"] = "1";
$result["message"] = "success";
echo json_encode($result);
mysqli_close($conn);
} else {
$result["success"] = "0";
$result["message"] = "error";
echo json_encode($result);
mysqli_close($conn);
}
}
?>
当我在邮递员中运行代码时,数据可以保存到数据库中。但是当我尝试在android studio中运行代码时,数据无法保存。我已经在数据库中检查了我的格式。数据库中的日期格式为yy / mm / dd。
请帮助我将用户日期和时间保存到数据库中。预先谢谢你。
答案 0 :(得分:0)
确保您的PHP和MYSQL中存在以下设置。 MYSQL端 -在您的MYSQL表中(预订)-2列Pickup_date-日期类型应为日期(yyyy-mm-dd)格式的输入,预计将要插入,Pickup_time-此列的数据类型应为时间。
PHP方面: 您的发布日期必须采用yyyy-mm-dd格式,否则必须仅以这种格式进行转换。您正在从Android代码中以以下格式发布信息:yyyy / mm / dd 时间-仅预期为hh:ii:ss格式,您以AM / PM格式发布时间。
只需更新您的输入,它就会起作用。
我假设您已经在Android中正确编写了代码以在服务器上进行HTTP POST(无论您的PHP和DB位于何处,都是Localhost或Remote。)
谢谢。