product not inserting in mysql using a php webservive -
i have database have insert row first have perform check on pid. unable found solution kindly help. here php code:
<?php /* * following code create new product row * product details read http post request */ // array json response $response = array(); // check required fields if (isset($_post['name']) && isset($_post['price']) && isset($_post['description'])) { //$name = $_post['name']; //$price = $_post['price']; //$description = $_post['description']; $name = "zone"; $price = 123; $description = "what"; // include db connect class require_once __dir__ . '/db_connect.php'; // connecting db $db = new db_connect(); // mysql inserting new row $result = mysql_query("if ((select count(*) products pid = 7)= 0) begin insert products(name, price, description) values('$name', '$price', '$description'); end else begin end"); // check if row inserted or not if ($result) { // inserted database $response["success"] = 1; $response["message"] = "product created."; // echoing json response echo json_encode($response); } else { // failed insert row $response["success"] = 0; $response["message"] = "oops! error occurred."; // echoing json response echo json_encode($response); } } else { // required field missing $response["success"] = 0; $response["message"] = "required field(s) missing"; // echoing json response echo json_encode($response); } ?> here android code
list<namevaluepair> params = new arraylist<namevaluepair>(); params.add(new basicnamevaluepair("name", name)); params.add(new basicnamevaluepair("price", price)); params.add(new basicnamevaluepair("description", description)); jsonobject jsonobject= new org.test.jsonparser().makehttprequest(url, "post", params); try { int success = jsonobject.getint(tag_success); if (success == 1) { // created product log.d("create","created"); // closing screen finish(); } else { // failed create product } } catch (jsonexception e) { e.printstacktrace(); log.d("exc","exc "+e); } return null; } public void onpostexecute(string file_url){ pdialog.dismiss(); product not inserted , message success 0 oops , error occurred $result not 1. please help!
though can't verify it, i'm pretty confident sending mysql entire statement going fail miserably. however, if break more realistic chunks succeed. maybe this:
$result = mysql_query("select `pid` `products` `pid` = 7"); $rows = mysql_num_rows($result); if ($rows == 0) { // means there not record pid = 7... $result = mysql_query("insert products(name, price, description) values('$name', '$price', '$description')"); if ($result) { // error occurred insert'ing data } } else { // means there record pid = 7... } edit
your comment verified assumptions failure since said produced error:
you have error in sql syntax; check manual corresponds mysql server version right syntax use near 'if ((select count(*) products pid = 7)= 0)
security note
please move away mysql_query , @ least leverage mysqli_query , parameterized queries ensure you're protected sql injection.
Comments
Post a Comment