Reputation: 499
I have reservation activity which allow users to reserving table. The problem is that doesn't save anything into database which is mysql. Doesn't show anything in LogCat and there is no errors at all. Just reload the same activity. The flow of activities is
Activity_1 -> Activity_2 -> Activity_3 -> Activity_4 -> Activity_5 (last and finish)
I've passed what data I need with intents between activities.
Activity_2
Intent newActivity = new Intent(Activity_2.this, Activity_3.class);
newActivity.putExtra("Position", Position);
newActivity.putExtra("resultServer", resultServer);
newActivity.putExtra("id", MyArrList.get(position).get("id").toString());
startActivity(newActivity);
Activity_3
Intent intent= getIntent();
table_id = intent.getStringExtra("id");
btnMenues.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Activity_3.this, Activity_4.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);
intent.putExtra("table_id", table_id);
startActivity(intent);
}
//}
});
}
Activity_4
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");
table_id = extras.getString("table_id");
}
Intent newActivity = new Intent(Activity_4.this, Activity_5.class);
newActivity.putExtra("Position", Position);
newActivity.putExtra("resultServer", resultServer);
newActivity.putExtra("table_id", MyArrList.get(position).get("table_id").toString());
startActivity(newActivity);
Activity_5
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.table_information);
Intent intent= getIntent();
curPosition = Integer.parseInt(intent.getStringExtra("Position"));
resultServer = String.valueOf(intent.getStringExtra("resultServer"));
try {
MyArrList = ConvertJSONtoArrayList(resultServer);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Log.d("ArrayList Size",String.valueOf(MyArrList.size()));
// Show Image Full
new DownloadFullPhotoFileAsync().execute();
btnFinish = (Button) findViewById(R.id.btnFinish);
btnFinish.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");
getTable = extras.getString("table_id");
}
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, String getTable) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://link.to.site/save.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("Date", getDateTimeData));
nameValuePairs.add(new BasicNameValuePair("numberOfPeople", getnumberOfPeopleData));
nameValuePairs.add(new BasicNameValuePair("table_id", getTable));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
}
catch(Exception e)
{
Log.e("log_tag", "Error: "+e.toString());
}
}
I've missed somewhere something but can't figured out what exactly.
Update with save.php
$icon = mysql_connect("localhost","user","pass");
if(!$icon)
{
die('Could not connect : ' . mysql_erroe());
}
mysql_select_db("database", $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'];
$DateTime= date("Y-m-d H:i:s", strtotime($DateTime));
$table_id = $_POST['table_id'];
mysql_query("INSERT INTO reservation (Name, Phone, Email, Comment, DateTime, numberOfPeople, table)
VALUES ('$Name', '$Phone', '$Email', '$Comment', '$DateTime', '$numberOfPeople', '$table_id')",$icon);
mysql_close($icon);
UPDATED with logcat
After Activity_3
it's lost intents.
11-30 03:40:59.497: D/Activity_3(1642): testName [email protected] 123456 testComment 30-11-2014 3:40 2 1
11-30 03:41:01.817: D/Activity_4(1642): null null null null null null 1
11-30 03:41:04.667: D/Actiovity_5(1642): org.apache.http.message.BasicHttpResponse@b2f5c9a0
On Activity_4
it hold only table_id
Upvotes: 0
Views: 88
Reputation: 45490
Your code is not complete you have only coded the happy case.
You are missing the following:
echo json_encode($data);
data is undefinedmysqli_
or at least use mysql_real_escape()
BasicNameValuePair("Date", getDateTimeData)
and $DateTime=$_POST['DateTime'];
won't matcherror_log()
) or use network tools to watch the requestLogCat
if(isset($_POST['Name'], $_POST['Phone'], $_POST['Email'], $_POST['Comment'],
$_POST['numberOfPeople'], $_POST['DateTime'])){
$icon = mysql_connect("localhost","user","pass", "database");
if(!$icon)
{
die('Could not connect : ' . mysql_error());
}
$name=$_POST['Name'];
$phone=$_POST['Phone'];
$email=$_POST['Email'];
$comment=$_POST['Comment'];
$number_of_people=$_POST['numberOfPeople'];
$date_time= date("Y-m-d H:i:s", strtotime($_POST['DateTime']));
$table_id = $_POST['table_id'];
$query = sprintf("
INSERT INTO reservation
(`Name`, `Phone`, `Email`, `Comment`,
`DateTime`, `numberOfPeople`, `table`)
VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s')",
mysql_real_escape_string($name),
mysql_real_escape_string($phone),
mysql_real_escape_string($email)),
mysql_real_escape_string($comment),
mysql_real_escape_string($date_time),
mysql_real_escape_string($number_of_people),
mysql_real_escape_string($table_id);
$result = mysql_query($query,$icon);
if(!$result)
{
die('Could not execute : ' . mysql_error());
}
if(mysql_num_rows($result) > 0){
echo "Success !";
}else{
echo "Fail !";
}
mysql_close($icon);
}else{
echo "missing values";
}
edit:
Don't forget to log the response in android:
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
Log.d(LOG_TAG, response.toString());
edit again
If you get null values in activity4
it means the editText are null in Activity3
, make sure they are initialized in onCreate()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1 = (EditText) findViewByID(R.id.editText1);
editText2 = (EditText) findViewByID(R.id.editText2);
editText3 = (EditText) findViewByID(R.id.editText3);
......
}
then on your button event do this:
@Override
public void onClick(View v) {
Intent intent = new Intent(Activity_3.this, Activity_4.class);
intent.putExtra("Name", editText1.getText());
intent.putExtra("Email", editText2.getText());
intent.putExtra("Phone", editText3.getText());
intent.putExtra("Comment", editText4.getText());
intent.putExtra("DateTime", datePicker.getText());
intent.putExtra("numberOfPeople", editText5.getText());
intent.putExtra("table_id", getIntent().getStringExtra("id"));
startActivity(intent);
}
Upvotes: 1