Reputation:
Hi i'm a new PHP developer and its a language I'm just starting to pick up. I've tested my code and getting no errors but for some reason it won't add the values to the database.
As a php learner what other debugging steps should I take?
if($payment_status=='Completed'){
$txn_id_check = mysqli_query($mysqli,"SELECT `transaction_id` FROM `payment` WHERE `transaction_id`='$txn_id'");
if(mysqli_num_rows($txn_id_check)!=1){
// add txn_id to db
$query = "INSERT INTO `payment` (`transaction_id`, `payment_status`, `users_id`) VALUES(?, ?, ?)";
$statement = $mysqli->prepare($query);
$statement->bind_param('ssi',$txn_id, $payment_status, $id);
if($statement->execute()){
print 'Success! ID of last inserted record is : ' .$statement->insert_id .'<br />';
}else{
die('Error : ('. $mysqli->errno .') '. $mysqli->error);
}
$statement->close();
}
Upvotes: 0
Views: 520
Reputation: 54831
I suppose your problem is that you're mixing OO (object-oriented) and procedural ways of working with mysqli
.
If your $mysqli
variable is created via new mysqli(/* params here */);
then you use OO approach and shouldn't use mysqli_
prefixed functions.
In case $mysqli
variable is mysqli_connect(/* params here */);
then you use procedural approach and you don't have $mysqli
object to use ->
on it.
Update.
Ok, as you said - you create a $mysqli
object via new
.
Then, as I said mysqli_
prefixed functions can't be used.
And you should change
$txn_id_check = mysqli_query($mysqli,"SELECT `transaction_id` FROM `payment` WHERE `transaction_id`='$txn_id'");
to
$txn_id_check = $mysqli->query('Your query here');
// now $txn_id_check stores mysqli_result
And:
if(mysqli_num_rows($txn_id_check)!=1) {
to
// use your mysqli_result to check number of rows
if($txn_id_check->num_rows != 1) {
Upvotes: 3
Reputation: 61
I assume that $txn_id is an integer and you are binding it as a string. That will fail. What about $payment_status ? Is that a string? Remember that boolean type in mysql is seen as small INT too.
$statement->bind_param('ssi',$txn_id, $payment_status, $id); Change it to: 'isi' or bind it to the appropriate type.
Also as @u_mulder pointed out, you are mixing procedural and object oriented style.
Upvotes: 0
Reputation: 2026
if($payment_status=='Completed'){
$txn_id_check = mysqli_query($mysqli,"SELECT 'transaction_id' FROM 'payment' WHERE 'transaction_id'='$txn_id'");
if(mysqli_num_rows($txn_id_check)!=1){
//if($receiver_email=='[email protected]'){
//if($payment_amount=='10.00' && $payment_currency=='GBP'){
// add txn_id to db
$query = "INSERT INTO 'payment' ('transaction_id', 'payment_status', 'users_id') VALUES(?, ?, ?)";
$statement = $mysqli->prepare($query);
$statement->bind_param('ssi',$txn_id, $payment_status, $id);
if($statement->execute()){
print 'Success! ID of last inserted record is : ' .$statement->insert_id .'<br />';
}else{
die('Error : ('. $mysqli->errno .') '. $mysqli->error);
}
$statement->close();
// update premium to 1
//$update_premium = mysqli_query("UPDATE 'users' SET is_member ='1' WHERE 'id' ='".$id."'");
}
}
This happened to me before, it was quite a hassle to detect the issue. The problem was with the single quotation marks instead of ` you must have '.
Upvotes: -1