简体   繁体   English

PHP记录未插入数据库

[英]PHP record not inserting to database

Hi i'm a new PHP developer and its a language I'm just starting to pick up. 嗨,我是一名新的PHP开发人员,其语言我才刚开始使用。 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 $ mysqli是连接的名称
  • tested with string inputs, still wouldn't execute 经过字符串输入测试,仍然无法执行

As a php learner what other debugging steps should I take? 作为php学习者,我还应该采取哪些其他调试步骤?

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 . 我想您的问题是您在混合使用OO(面向对象)和使用mysqli过程方法。

If your $mysqli variable is created via new mysqli(/* params here */); 如果您的$mysqli变量是通过new mysqli(/* params here */); then you use OO approach and shouldn't use mysqli_ prefixed functions. 那么您将使用OO方法,并且不应使用mysqli_前缀函数。

In case $mysqli variable is mysqli_connect(/* params here */); 如果$mysqli变量是mysqli_connect(/* params here */); then you use procedural approach and you don't have $mysqli object to use -> on it. 那么您将使用过程方法,并且没有$mysqli对象可用于->

Update. 更新。

Ok, as you said - you create a $mysqli object via new . 好的,正如您所说-通过new创建一个$mysqli对象。 Then, as I said mysqli_ prefixed functions can't be used. 然后,正如我所说,不能使用mysqli_前缀函数。 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. 我假设$ txn_id是一个整数,并且您将其绑定为字符串。 That will fail. 那会失败。 What about $payment_status ? 那么$ payment_status呢? Is that a string? 那是绳子吗? Remember that boolean type in mysql is seen as small INT too. 请记住,MySQL中的布尔类型也被视为小型INT。

$statement->bind_param('ssi',$txn_id, $payment_status, $id); $ statement-> bind_param('ssi',$ txn_id,$ payment_status,$ id); Change it to: 'isi' or bind it to the appropriate type. 将其更改为:“ isi”或将其绑定到适当的类型。

Also as @u_mulder pointed out, you are mixing procedural and object oriented style. 就像@u_mulder指出的那样,您正在混合过程和面向对象的样式。

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 '. 问题在于单引号而不是“您必须拥有”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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