java - unable to insert a row of data into a mysql table using and android application -
im making project in android studio uni can insert , receive data sql database hosted on godaddy. have been trying many tutorials little progress. project in current state capable of connecting , can query database existing inputs; unable perform insert action. believe issue lies java code rather php scripts. tutorial using is: http://easyway2in.blogspot.co.uk/2015/07/android-mysql-database-connect.html#comment-form
appreciated.
backgroundtask.java
within backgroundtask.java file, method called register believe issue.
import android.app.alertdialog; import android.content.context; import android.os.asynctask; import android.widget.toast; import java.io.bufferedreader; import java.io.bufferedwriter; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.io.outputstream; import java.io.outputstreamwriter; import java.net.httpurlconnection; import java.net.malformedurlexception; import java.net.url; import java.net.urlencoder; public class backgroundtask extends asynctask<string,void,string> { alertdialog alertdialog; context ctx; backgroundtask(context ctx) { this.ctx =ctx; } @override protected void onpreexecute() { alertdialog = new alertdialog.builder(ctx).create(); alertdialog.settitle("login information...."); } @override protected string doinbackground(string... params) { string reg_url = "http://example/register.php"; string login_url = "http://example/login.php"; string method = params[0]; if (method.equals("register")) { string name = params[1]; string user_name = params[2]; string user_pass = params[3]; try { url url = new url(reg_url); httpurlconnection httpurlconnection = (httpurlconnection) url.openconnection(); httpurlconnection.setrequestmethod("post"); httpurlconnection.setdooutput(true); //httpurlconnection.setdoinput(true); outputstream os = httpurlconnection.getoutputstream(); bufferedwriter bufferedwriter = new bufferedwriter(new outputstreamwriter(os, "utf-8")); string data = urlencoder.encode("user", "utf-8") + "=" + urlencoder.encode(name, "utf-8") + "&" + urlencoder.encode("user_name", "utf-8") + "=" + urlencoder.encode(user_name, "utf-8") + "&" + urlencoder.encode("user_pass", "utf-8") + "=" + urlencoder.encode(user_pass, "utf-8"); bufferedwriter.write(data); bufferedwriter.flush(); bufferedwriter.close(); os.close(); inputstream = httpurlconnection.getinputstream(); is.close(); //httpurlconnection.connect(); httpurlconnection.disconnect(); return "registration success..."; } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } else if(method.equals("login")) { string login_name = params[1]; string login_pass = params[2]; try { url url = new url(login_url); httpurlconnection httpurlconnection = (httpurlconnection)url.openconnection(); httpurlconnection.setrequestmethod("post"); httpurlconnection.setdooutput(true); httpurlconnection.setdoinput(true); outputstream outputstream = httpurlconnection.getoutputstream(); bufferedwriter bufferedwriter = new bufferedwriter(new outputstreamwriter(outputstream,"utf-8")); string data = urlencoder.encode("login_name","utf-8")+"="+urlencoder.encode(login_name,"utf-8")+"&"+ urlencoder.encode("login_pass","utf-8")+"="+urlencoder.encode(login_pass,"utf-8"); bufferedwriter.write(data); bufferedwriter.flush(); bufferedwriter.close(); outputstream.close(); inputstream inputstream = httpurlconnection.getinputstream(); bufferedreader bufferedreader = new bufferedreader(new inputstreamreader(inputstream,"iso-8859-1")); string response = ""; string line = ""; while ((line = bufferedreader.readline())!=null) { response+= line; } bufferedreader.close(); inputstream.close(); httpurlconnection.disconnect(); return response; } catch (malformedurlexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } return null; } @override protected void onprogressupdate(void... values) { super.onprogressupdate(values); } @override protected void onpostexecute(string result) { if(result.equals("registration success...")) { toast.maketext(ctx, result, toast.length_long).show(); } else { alertdialog.setmessage(result); alertdialog.show(); } } }
register.java
import android.app.activity; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.view.view; import android.widget.edittext; public class register extends activity { edittext et_name,et_user_name,et_user_pass; string name,user_name,user_pass; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.register_layout); et_name = (edittext)findviewbyid(r.id.name); et_user_name= (edittext)findviewbyid(r.id.new_user_name); et_user_pass = (edittext)findviewbyid(r.id.new_user_pass); } public void userreg(view view) { name = et_name.gettext().tostring(); user_name = et_user_name.gettext().tostring(); user_pass =et_user_pass.gettext().tostring(); string method = "register"; backgroundtask backgroundtask = new backgroundtask(this); backgroundtask.execute(method,name,user_name,user_pass); finish(); } }
register_layout.xml
it possible issue lies layout used registration.
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 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="register" android:orientation="vertical" android:background="#0bb990" > <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="registration form" android:textappearance="?android:textappearancelarge" android:textstyle="bold" android:layout_gravity="center_horizontal" android:layout_margintop="10dp" /> <edittext android:layout_width="250dp" android:layout_height="wrap_content" android:hint="name" android:id="@+id/name" android:layout_gravity="center_horizontal" android:layout_margintop="40dp" /> <edittext android:layout_width="250dp" android:layout_height="wrap_content" android:hint="user name" android:id="@+id/new_user_name" android:layout_gravity="center_horizontal" android:layout_margintop="20dp" /> <edittext android:layout_width="250dp" android:layout_height="wrap_content" android:hint="password" android:id="@+id/new_user_pass" android:layout_gravity="center_horizontal" android:layout_margintop="20dp" android:inputtype="textpassword" /> <button android:layout_width="130dp" android:layout_height="wrap_content" android:text="register" android:layout_gravity="center_horizontal" android:layout_margintop="30dp" android:onclick="userreg" /> </linearlayout>
init.php
to establish connection database init.php used. appears functional, allows login request approved.
<?php $db_name = "itemdata"; $mysql_user = ""; $mysql_pass = ""; $server_name = "localhost"; $con = mysqli_connect($server_name,$mysql_user,$mysql_pass,$db_name); ?>
register.php
i have tried many variants register script none seem work. issue may lie here.
<?php require "init.php"; $name = $_post["name"]; $user_name = $_post["user_name"]; $user_pass = $_post["user_pass"]; $sql_query = "insert `itemdata`.`users`(`name`,`user_name`,`user_pass`,`id`) values('$name','$user_name','$user_pass',null);"; ?>
Comments
Post a Comment