Android Studio: PHP mysql variable isnt being passed in RegisterAPI when adding 2 variables -
i trying use form , php pass variable php file post mysql. able of variables calculated one. can't work. can make toast show calculated number, not carry forward.
in mainactivity: trying calculate emptymilage
- loadedmilage
.
i created private textview or string, have tried everything, , know can't grasp yet.
i have attached mainactivity , registerapi class using along sql import.
mainactivity:
public class mainactivity extends appcompatactivity implements view.onclicklistener { //declaring views private webview webview; private edittext trucksid; private edittext tripreportnumber; private edittext entereddate; private edittext emptymilage; private edittext loadedmilage; private button buttonregister; private string esttotal; private textview estmilage; //this our root url public static final string root_url = "http://rooturl_for_php/php/"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //initializing views trucksid = (edittext) findviewbyid(r.id.trucksid); tripreportnumber = (edittext) findviewbyid(r.id.tripreportnumber); entereddate = (edittext) findviewbyid(r.id.entereddate); emptymilage= (edittext) findviewbyid(r.id.emptymilage); loadedmilage = (edittext) findviewbyid(r.id.loadedmilage); estmilage = (textview) findviewbyid(r.id.estmilage); buttonregister = (button) findviewbyid(r.id.buttonregister); //adding listener button buttonregister.setonclicklistener(this); } public boolean oncreateoptionsmenu(menu menu) { menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.main_menu,menu); return super.oncreateoptionsmenu(menu); } // took out menu options, since working // private void insertuser(){ //here handle http request insert user mysql db //creating restadapter restadapter adapter = new restadapter.builder() .setendpoint(root_url) //setting root url .build(); //finally building adapter //creating object our interface registerapi api = adapter.create(registerapi.class); //defining method insertuser of our interface api.insertuser( //passing values getting edittexts trucksid.gettext().tostring(), tripreportnumber.gettext().tostring(), entereddate.gettext().tostring(), emptymilage.gettext().tostring(), loadedmilage.gettext().tostring(), //can take out below estmilage.gettext().tostring(), //creating anonymous callback new callback<response>() { @override public void success(response result, response response) { //on success read server's output using bufferedreader //creating bufferedreader object bufferedreader reader = null; //an string store output server string output = ""; try { //initializing buffered reader reader = new bufferedreader(new inputstreamreader(result.getbody().in())); //reading output in string output = reader.readline(); } catch (ioexception e) { e.printstacktrace(); } //displaying output toast toast.maketext(mainactivity.this, output, toast.length_long).show(); } @override public void failure(retrofiterror error) { //if error occured displaying error toast toast.maketext(mainactivity.this, error.tostring(),toast.length_long).show(); } } ); } //overriding onclick method @override public void onclick(view v) { //calling insertuser on button click insertuser(); int value1=integer.parseint(emptymilage.gettext().tostring()); int value2=integer.parseint(loadedmilage.gettext().tostring()); int estmilage=value2-value1; toast.maketext(this, "estimated milage:"+string.valueof(estmilage), toast.length_short).show(); } }
here registerapi created
public interface registerapi { @formurlencoded @post("/insert.php") public void insertuser( @field("trucksid") string trucksid, @field("tripreportnumber") string tripreportnumber, @field("entereddate") string entereddate, @field("emptymilage") string emptymilage, @field("loadedmilage") string loadedmilage, @field("esttotal") string esttotal, callback<response> callback); }
and here final, after use $conn php file insert.php
<?php //checking if script received post request or not if($_server['request_method']=='post'){ //getting post data $trucksid = $_post['trucksid']; $tripreportnumber = $_post['tripreportnumber']; $entereddate = $_post['entereddate']; $emptymilage = $_post['emptymilage']; $loadedmilage = $_post['loadedmilage']; $esttotal = $_post['esttotal']; //checking if received values blank if($trucksid == '' || $tripreportnumber == '' || $emptymilage == '' || $loadedmilage == '' ){ //giving message fill values if values blank echo 'please fill values'; }else{ //if values not blank //connecting our database calling dbconnect script require_once('dbconnect.php'); //creating sql query insert database //here may need change drivers_report because table created //if have different table write table's name //this query check whether trip report registered or not $sql = "select * drivers_report trucksid='$trucksid'"; //if variable check has value mysqli fetch array //that means username or email exist $check = mysqli_fetch_array(mysqli_query($con,$sql)); //checking check has values or not if(isset($check)){ //if check has value means username exist echo 'trip report exists'; }else{ //if username not exist //creating insert query $sql = "insert drivers_report (trucksid,tripreportnumber,entereddate,emptymilage,loadedmilage,esttotal) values('$trucksid','$tripreportnumber','$entereddate','$emptymilage','$loadedmilage','$esttotal')"; //trying insert values db if(mysqli_query($con,$sql)){ //if inserted echo 'trip report submitted!'; }else{ //in case error occured echo 'oops! please try again!'; } } //closing database connection mysqli_close($con); } }else{ echo 'error'; }
the variable can change has populate esttotal in php file. thank help, have spent week trying change code 50 times work , still stuck.
also attached activity_main.xml
screen
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".mainactivity"> <textview android:text="enter truck id:" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <edittext android:id="@+id/trucksid" android:inputtype="number" android:layout_width="match_parent" android:layout_height="wrap_content" /> <textview android:text="enter trip report number:" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <edittext android:id="@+id/tripreportnumber" android:inputtype="number" android:layout_width="match_parent" android:layout_height="wrap_content" /> <textview android:text="enter trip date:" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <edittext android:id="@+id/entereddate" android:inputtype="date" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="mm/dd/yy" /> <textview android:text="enter empty milage:" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <edittext android:id="@+id/emptymilage" android:inputtype="number" android:layout_width="match_parent" android:layout_height="wrap_content" /> <textview android:text="enter loaded milage:" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <edittext android:id="@+id/loadedmilage" android:inputtype="date" android:layout_width="match_parent" android:layout_height="wrap_content" /> <button android:id="@+id/buttonregister" android:text="submit trip report" android:layout_width="match_parent" android:layout_height="wrap_content" /> <textview android:id="@+id/estmilage" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="invisible" /> </linearlayout>
re-adding in full mainactivity file:
package com.johncbell.atrixtrucking; import android.app.datepickerdialog; import android.content.intent; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.menu; import android.view.menuinflater; import android.view.menuitem; import android.view.view; import android.widget.button; import android.widget.datepicker; import android.widget.edittext; import android.widget.textview; import android.widget.toast; import android.webkit.webview; import org.w3c.dom.text; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; import retrofit.callback; import retrofit.restadapter; import retrofit.retrofiterror; import retrofit.client.response; //class our main activity onclicklistener public class mainactivity extends appcompatactivity implements view.onclicklistener { //declaring views private webview webview; private edittext trucksid; private edittext tripreportnumber; private edittext entereddate; private edittext emptymilage; private edittext loadedmilage; private button buttonregister; private string esttotal; private textview estmilage; //this our root url public static final string root_url = "http://urlofphpfile/php/"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //initializing views trucksid = (edittext) findviewbyid(r.id.trucksid); tripreportnumber = (edittext) findviewbyid(r.id.tripreportnumber); entereddate = (edittext) findviewbyid(r.id.entereddate); emptymilage= (edittext) findviewbyid(r.id.emptymilage); loadedmilage = (edittext) findviewbyid(r.id.loadedmilage); estmilage = (textview) findviewbyid(r.id.estmilage); buttonregister = (button) findviewbyid(r.id.buttonregister); //adding listener button buttonregister.setonclicklistener(this); } public boolean oncreateoptionsmenu(menu menu) { menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.main_menu,menu); return super.oncreateoptionsmenu(menu); } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case r.id.openwebsite: intent website = new intent(this,ourwebsite.class); this.startactivity(website); return true; case r.id.goto2: intent activity2 = new intent(this,mainactivity2.class); this.startactivity(activity2); return true; case r.id.about_us: intent = new intent(this,aboutus.class); this.startactivity(about); return true; default: return super.onoptionsitemselected(item); } } private void insertuser(){ //here handle http request insert user mysql db //creating restadapter restadapter adapter = new restadapter.builder() .setendpoint(root_url) //setting root url .build(); //finally building adapter //creating object our interface registerapi api = adapter.create(registerapi.class); //defining method insertuser of our interface int value1=integer.parseint(emptymilage.gettext().tostring()); int value2=integer.parseint(loadedmilage.gettext().tostring()); int estmilage=value2-value1; api.insertuser( api.insertuser( //passing values getting edittexts trucksid.gettext().tostring(), tripreportnumber.gettext().tostring(), entereddate.gettext().tostring(), emptymilage.gettext().tostring(), loadedmilage.gettext().tostring(), //can take out below integer.tostring(estmilage), //creating anonymous callback new callback<response>() { @override public void success(response result, response response) { //on success read server's output using bufferedreader //creating bufferedreader object bufferedreader reader = null; //an string store output server string output = ""; try { //initializing buffered reader reader = new bufferedreader(new inputstreamreader(result.getbody().in())); //reading output in string output = reader.readline(); } catch (ioexception e) { e.printstacktrace(); } //displaying output toast toast.maketext(mainactivity.this, output, toast.length_long).show(); } @override public void failure(retrofiterror error) { //if error occured displaying error toast toast.maketext(mainactivity.this, error.tostring(),toast.length_long).show(); } } ); } //overriding onclick method @override public void onclick(view v) { //calling insertuser on button click insertuser(); } }
before api.insertuser(
, try:
int value1=integer.parseint(emptymilage.gettext().tostring()); int value2=integer.parseint(loadedmilage.gettext().tostring()); int estmilage=value2-value1;
then api.insertuser(
should be:
api.insertuser( //passing values getting edittexts trucksid.gettext().tostring(), tripreportnumber.gettext().tostring(), entereddate.gettext().tostring(), emptymilage.gettext().tostring(), loadedmilage.gettext().tostring(), //can take out below integer.tostring(estmilage),