Reputation: 39
How to check insert success or not When i using this code ?
i want to check insert success or not
if success echo "success";
else echo "error";
$link = mysql_connect($host, $user, $pass);
$sql = mysql_query("INSERT INTO `transaction` (txnid, pro_name, price, status)
VALUES (
'".$data['txn_id']."' ,
'".$data['pro_name']."' ,
'".$data['price']."' ,
'$status'
)", $link);
return mysql_insert_id($link);
Upvotes: 0
Views: 84
Reputation: 7447
Simply:
if (!$sql) echo 'error';
else echo 'success';
From the docs on mysql_query:
For ... INSERT ... mysql_query() returns TRUE on success or FALSE on error.
More importantly though, read this: Why shouldn't I use mysql_* functions in PHP?
Upvotes: 1
Reputation: 1163
According to mysql_insert_id
. it returns new generated id on successful insert.
Check here :
$link = mysql_connect($host, $user, $pass);
$sql = mysql_query("INSERT INTO `transaction` (txnid, pro_name, price, status)
VALUES (
'".$data['txn_id']."' ,
'".$data['pro_name']."' ,
'".$data['price']."' ,
'$status'
)", $link);
$lastInsertedId = mysql_insert_id($link);
if (empty($lastInsertedId))
$return = 'error';
else
$return = $lastInsertedId;
return $return;
Upvotes: 0
Reputation: 1
$link = mysql_connect($host, $user, $pass);
$sql = mysql_query("INSERT INTO `transaction` (txnid, pro_name, price, status)
VALUES (
'".$data['txn_id']."' ,
'".$data['pro_name']."' ,
'".$data['price']."' ,
'$status'
)", $link);
//------------Changes----------------
$inserted_id = mysql_insert_id($link);
if(mysql_errno() != 0){
echo "error";
else
echo "success";
return $inserted_id;
Upvotes: 0