Unable to connect to MySQL with PHP - Android Studio -


i having difficulties trying set connection database php... have tried many things, double-checked sql queries, , don't see why not working... i'm still newbie, guess i'm missing out of range yet. trying create app take user registration.

this error i'm getting in android studio:

org.json.jsonexception: value <!doctype of type java.lang.string cannot converted jsonobject 

my php code is:

<?php $servername = "my server here"; $username = "my username here"; $password = "my password here"; $dbname = "my db here";  // create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // check connection if (!$conn) { die("connection failed: " . mysqli_connect_error()); }  $sql = "insert user (username, email, passcode) values (?, ?, ?)";  if (mysqli_query($conn, $sql)) { $last_id = mysqli_insert_id($conn); echo "new record created successfully. last inserted id is: " . $last_id; } else { echo "error: " . $sql . "<br>" . mysqli_error($conn); }  mysqli_close($conn); ?> 

another important point, checking in postman plugin , verifying url this:

error: insert user (username, email, passcode) values (?, ?, ?) have error in sql syntax; check manual corresponds mysql server version right syntax use near '?, ?, ?)' @ line 1 

i don't understand why error? since expecting input user in form...

as in of more information, code registeractivity:

protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_register);      final edittext etusername = (edittext)findviewbyid(r.id.etusername);     final edittext etemail = (edittext)findviewbyid(r.id.etemail);     final edittext etpassword = (edittext)findviewbyid(r.id.etpassword);     final button btnregister = (button)findviewbyid(r.id.btnregister);      if (btnregister != null) {         btnregister.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view v) {                  final string username = etusername.gettext().tostring();                 final string email = etemail.gettext().tostring();                 final string passcode = etpassword.gettext().tostring();                  response.listener<string> responselistener = new response.listener<string>() {                     @override                     public void onresponse(string response) {                         try {                             jsonobject jsonresponse = new jsonobject(response);                             boolean success = jsonresponse.getboolean("success");                             if (success) {                                 intent intent = new intent(registeractivity.this, loginactivity.class);                                 registeractivity.this.startactivity(intent);                             } else {                                 alertdialog.builder builder = new alertdialog.builder(registeractivity.this);                                 builder.setmessage("register failed")                                         .setnegativebutton("retry", null)                                         .create()                                         .show();                             }                         } catch (jsonexception e) {                             e.printstacktrace();                         }                     }                 };                  registerrequest registerrequest = new registerrequest(username, email, passcode, responselistener);                 requestqueue queue = volley.newrequestqueue(registeractivity.this);                 queue.add(registerrequest); 

so, know where's coming , else should into?

cheers!

you forgot bind values sql-statement.

here's code 'should' work (i didn't test android...and i'm happy improve answer if doesnt work...):
note, switched object oriented style. more information please read manual!

<?php $servername = "my server here"; $username = "my username here"; $password = "my password here"; $dbname = "my db here";  // create connection $mysqli = new mysqli($servername, $username, $password, $dbname); // check connection if (!$mysqli) {     // display error }  $sql = "insert user (username, email, passcode) values (?, ?, ?)";   if ($stmt=$mysqli->prepare($sql)) {    // here's you're missing:    $stmt->bind_param("sss", $_post['username'], $_post['email'], $_post['passcode']);    $stmt->execute();    // defenitely want more (error-)checks here    $last_id = $mysqli->insert_id($conn);    // , here     // return json android. add data want (the whole new record f.e.)    $return = "{'success':true, 'id': $last_id}";    echo $return; } else {    // return errors:    $return = "{'success':false, 'errors': [{'db-error': '".$sql." ".$mysqli->error."'}]}";    echo $return; }  $mysqli->close($conn); ?> 

note should not pass values $_post directly did now, escape them, validate them, etc...


Popular posts from this blog

php - How should I create my API for mobile applications (Needs Authentication) -

5 Reasons to Blog Anonymously (and 5 Reasons Not To)

Google AdWords and AdSense - A Dynamic Small Business Marketing Duo