Insert into database from android using PHP -
i'm trying insert data android
application database
using php
, tried below script error
fail 3﹕ org.json.jsonexception: end of input @ character 0 of
php
script
<?php $servername = ""; $username = ""; $password = ""; $dbname = ""; // create connection $conn = new mysqli($servername, $username, $password, $dbname); // check connection if ($conn->connect_error) { die("connection failed: " . $conn->connect_error); } // insert values table $name=$_request['name']; $email=$_request['email']; $message=$_request['message']; $sql = "insert user_message (`nm_id_mes`, `nm_mail`, `nm_content`, `nm_datetime`, `nm_name`) values (null,$email,$message,now()+ interval 7 hour,$name)"; $flag['code']=0; if ($conn->query($sql) === true) { //echo "new record created successfully"; $flag['code']=1; } else { $flag[code']=0; } echo json_encode($flag); $conn->close(); ?>
android
part
@override protected void doinbackground(void... params) { namevaluepairs = new arraylist<namevaluepair>(); namevaluepairs.add(new basicnamevaluepair("name", contactusfragment.name)); namevaluepairs.add(new basicnamevaluepair("email", contactusfragment.email)); namevaluepairs.add(new basicnamevaluepair("message", contactusfragment.message)); log.d("name , email , message ", contactusfragment.name+"--- "+contactusfragment.email+"--- "+contactusfragment.message ); try { httpclient httpclient = new defaulthttpclient(); httppost httppost = new httppost("http://example.com/message.php"); httppost.setentity(new urlencodedformentity(namevaluepairs)); httpresponse response = httpclient.execute(httppost); httpentity entity = response.getentity(); = entity.getcontent(); log.e("pass 1", "connection success "); } catch (exception e) { log.e("fail 1", e.tostring()); } try { bufferedreader reader = new bufferedreader (new inputstreamreader(is, "iso-8859-1"), 8); stringbuilder sb = new stringbuilder(); while ((line = reader.readline()) != null) { sb.append(line + "\n"); } is.close(); result = sb.tostring(); log.e("pass 2", "connection success "); } catch (exception e) { log.e("fail 2", e.tostring()); } try { jsonobject json_data = new jsonobject(result); code = (json_data.getint("code")); if (code == 1) { log.d("inserted successfully", ""); } else { log.d("sorry try again", ""); } } catch (exception e) { log.e("fail 3", e.tostring()); } return null; }
you have got small error in php script
<?php $servername = ""; $username = ""; $password = ""; $dbname = ""; // create connection $conn = new mysqli($servername, $username, $password, $dbname); // check connection if ($conn->connect_error) { die("connection failed: " . $conn->connect_error); } // insert values table $name=$_request['name']; $email=$_request['email']; $message=$_request['message']; $sql = "insert user_message (`nm_id_mes`, `nm_mail`, `nm_content`, `nm_datetime`, `nm_name`) values (null,$email,$message,now()+ interval 7 hour,$name)"; $flag['code']=0; if ($conn->query($sql) === true) { //echo "new record created successfully"; $flag['code']=1; } else { //$flag[code']=0; !!!!!!!!!! error $flag['code']=0; } echo json_encode($flag); $conn->close(); ?>
Comments
Post a Comment