简体   繁体   中英

PHP record not inserting to database

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.

  • tested for syntax errors
  • $mysqli is the name of the connection
  • tested with string inputs, still wouldn't execute

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();



        }

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) {

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.

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=='fortunefilly@hotmail.com'){

                //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 '.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM