Checking whether insertion of new row in mySql database was successful using php, and returning the newly inserted row -
i have php code registers new users android app. have declared username field unique, if duplicate username entered generate error @ insertion. how check whether insertion executed succesfully , how proceed returning newly added row app. here code, should write in if() statement?
$con = mysqli_connect("", "", "", ""); $username = $_post["username"]; $password = $_post["password"]; $imagelink = $_post["imagelink"]; $name = $_post["name"]; $dob = $_post["dob"]; $mobile = $_post["mobile"]; $email = $_post["email"]; $currency = $_post["currency"]; $location = $_post["location"]; $statement = mysqli_prepare($con, "insert a8768135_testdb.uzer (username, password, imagelink, name, dob, mobile, email, currency, location) values (?, ?, ?, ?, ?, ?, ?, ?, ?)"); mysqli_stmt_bind_param($statement, "sssssssss", $username, $password, $imagelink, $name, $dob, $mobile, $email, $currency, $location); mysqli_stmt_execute($statement); if() { $statemente = mysqli_prepare($con, "select * a8768135_testdb.uzer username = ? , password = ?"); mysqli_stmt_bind_param($statemente, "ss", $username, $password); mysqli_stmt_execute($statemente); mysqli_stmt_store_result($statemente); mysqli_stmt_bind_result($statemente, $userid, $username, $password, $imagelink, $name, $dob, $mobile, $email, $currency, $location); $user = array(); while (mysqli_stmt_fetch($statemente)) { $user["username"] = $username; $user["password"] = $password; $user["imagelink"] = $imagelink; $user["name"] = $name; $user["dob"] = $dob; $user["mobile"] = $mobile; $user["email"] = $email; $user["currency"] = $currency; $user["location"] = $location; } echo json_encode($user); } mysqli_close($con);
use mysqli_insert_id:
mysqli_insert_id($con); the result last id (auto increment value) record created, return of value 0 means no new record added/inserted
your code should this:
$statement = mysqli_prepare($con, "insert a8768135_testdb.uzer (username, password, imagelink, name, dob, mobile, email, currency, location) values (?, ?, ?, ?, ?, ?, ?, ?, ?)"); mysqli_stmt_bind_param($statement, "sssssssss", $username, $password, $imagelink, $name, $dob, $mobile, $email, $currency, $location); mysqli_stmt_execute($statement); if(mysqli_insert_id($con) > 0) { ...... }
Comments
Post a Comment