我有一个保留表单,必须将信息保存到MySQL
数据库。在第一个Axtivity用户填写表单然后白色意图我保存信息并passit到第二个活动,其中是按钮保存。当我点击按钮保存没有任何东西去数据库。
这是活动1
static String Name;
static String Email;
static String Phone;
static String Comment;
static String DateTime;
static String numberOfPeople;
private EditText editText1, editText3, editText2, editText4, datePicker, editText5; //, txtTime;
private Button btnMenues, btnTimePicker, btnCalendar;
CustomDateTimePicker custom;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reservation);
custom = new CustomDateTimePicker(this,
new CustomDateTimePicker.ICustomDateTimeListener() {
@Override
public void onSet(Dialog dialog, Calendar calendarSelected,
Date dateSelected, int year, String monthFullName,
String monthShortName, int monthNumber, int date,
String weekDayFullName, String weekDayShortName,
int hour24, int hour12, int min, int sec,
String AM_PM) {
((EditText) findViewById(R.id.datePicker)).setText(calendarSelected.get(Calendar.DAY_OF_MONTH)
+ "/" + (monthNumber+1) + "/" + year
+ ", " + hour12 + ":" + min
+ " " + AM_PM);
}
@Override
public void onCancel() {
}
});
/**
* Pass Directly current time format it will return AM and PM if you set
* false
*/
custom.set24HourFormat(false);
/**
* Pass Directly current data and time to show when it pop up
*/
custom.setDate(Calendar.getInstance());
findViewById(R.id.btnCalendar).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View v) {
custom.showDialog();
}
});
editText1 = (EditText) findViewById(R.id.personName);
editText3 = (EditText) findViewById(R.id.personPhone);
editText2 = (EditText) findViewById(R.id.personEmail);
editText4 = (EditText) findViewById(R.id.personComment);
datePicker = (EditText) findViewById(R.id.datePicker);
editText5 = (EditText) findViewById(R.id.editText5);
btnCalendar = (Button) findViewById(R.id.btnCalendar);
btnMenues = (Button) findViewById(R.id.continueWithReservation);
btnMenues.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Reservation.this, ReservationSecond.class);
String Name = editText1.getText().toString();
String Email = editText2.getText().toString();
String Phone = editText3.getText().toString();
String Comment = editText4.getText().toString();
String DateTime = datePicker.getText().toString();
String numberOfPeople = editText5.getText().toString();
intent.putExtra("Name", Name);
intent.putExtra("Email", Email);
intent.putExtra("Phone", Phone);
intent.putExtra("Comment", Comment);
intent.putExtra("DateTime", DateTime);
intent.putExtra("numberOfPeople", numberOfPeople);
startActivity(intent);
}
});
}
这是活动2
String getName;
String getEmail;
String getPhone;
String getComment;
String getDateTime;
String getnumberOfPeople;
private Button btnMenues;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reservation_second);
btnMenues = (Button) findViewById(R.id.finish);
btnMenues.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bundle extras = getIntent().getExtras();
if (extras != null) {
getName = extras.getString("Name");
getEmail = extras.getString("Email");
getPhone = extras.getString("Phone");
getComment = extras.getString("Comment");
getDateTime = extras.getString("DateTime");
getnumberOfPeople = extras.getString("numberOfPeople");
Log.e("err", getName + " " + getEmail + " " + getPhone + " " + getComment + " " + getDateTime + " " + getnumberOfPeople );
}
new SummaryAsyncTask().execute((Void) null);
startActivity(getIntent());
}
});
}
class SummaryAsyncTask extends AsyncTask<Void, Void, Boolean> {
private void postData(String getNameToData, String getEmailData, String getPhoneData,
String getCommentData, String getDateTimeData, String getnumberOfPeopleData) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://link.to.site/saveReservation.php");
try {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("Name", getNameToData));
nameValuePairs.add(new BasicNameValuePair("Email", getEmailData));
nameValuePairs.add(new BasicNameValuePair("Phone", getPhoneData));
nameValuePairs.add(new BasicNameValuePair("Comment", getCommentData));
nameValuePairs.add(new BasicNameValuePair("DateTime", getDateTimeData));
nameValuePairs.add(new BasicNameValuePair("numberOfPeople", getnumberOfPeopleData));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
}
catch(Exception e)
{
Log.e("log_tag", "Error: "+e.toString());
}
}
@Override
protected Boolean doInBackground(Void... params) {
postData(getName, getEmail, getPhone, getComment, getDateTime, getnumberOfPeople );
return null;
}
}
我在意图部分日志中添加了第二个活动来检查Log.e("err", getName + " " + getEmail + " " + getPhone + " " + getComment + " " + getDateTime + " " + getnumberOfPeople );
,在LogCat
中,当点击保存到数据库的按钮时,我看到此错误
11-08 17:04:44.654: E/err(1112): Test Test@myEmail.com 33733721 Hello 26/11/2014, 4:50 PM 3
这是第一个活动的xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/personName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="@string/NameForReservation"
android:inputType="textPersonName"
android:maxLength="25" />
<EditText
android:id="@+id/personPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="@string/ContactPhone"
android:inputType="phone"
android:maxLength="16" />
<EditText
android:id="@+id/personEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="@string/CustomerEmailContact"
android:inputType="textEmailAddress"
android:maxLength="50" />
<EditText
android:id="@+id/personComment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="@string/CustomerCommentar"
android:maxLength="100" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout1" >
<Button
android:layout_height="wrap_content"
android:layout_weight="0"
android:id="@+id/btnCalendar"
android:text="@string/ReservationDate"
android:layout_width="100dp" />
<requestFocus></requestFocus>
<EditText
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.27"
android:inputType="datetime" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/numberOfPeople"
android:layout_width="190dp"
android:layout_height="36dp"
android:layout_weight="0.04"
android:layout_marginLeft="10dp"
android:text="@string/numberOfPeople" />
<requestFocus></requestFocus>
<EditText
android:id="@+id/editText5"
android:layout_width="10dp"
android:layout_height="41dp"
android:layout_weight="0.02"
android:ems="10"
android:inputType="number"
android:maxLength="2"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/continueWithReservation"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="center"
android:text="@string/reservationContinue" />
</LinearLayout>
</LinearLayout>
知道我想念的是什么吗?
从服务器端更新php
文件
<?php
$icon = mysql_connect("localhost","user","pass");
if(!$icon)
{
die('Could not connect : ' . mysql_erroe());
}
mysql_select_db("db", $icon)or die("database selection error");
echo json_encode($data);
$Name=$_POST['Name'];
$Phone=$_POST['Phone'];
$Email=$_POST['Email'];
$Comment=$_POST['Comment'];
$DateTime=$_POST['DateTime'];
$numberOfPeople=$_POST['numberOfPeople'];
date_default_timezone_set("Europe/Bucharest"); // time zone
$DateTime= date("Y-m-d H:i:s");
mysql_query("INSERT INTO reservation (Name, Phone, Email, Comment, DateTime, numberOfPeople)
VALUES ('$Name', '$Phone', '$Email', '$Comment', '$DateTime', '$numberOfPeople')",$icon);
mysql_close($icon);
答案 0 :(得分:1)
您错过了单引号'$ DateTime'
mysql_query("INSERT INTO reservation (Name, Phone, Email, Comment, DateTime, numberOfPeople) VALUES ('$Name', '$Phone', '$Email', '$Comment', '$DateTime', '$numberOfPeople')");
不推荐使用mysql_query不要在新项目中使用它。试试mysqli_或PDO
在你的php中将它放在查询上方
date_default_timezone_set("Asia/Calcutta"); // choose your time zone
$DateTime= date("Y-m-d H:i:s");