0

The Code is:

class anything_i
{

/*Every variable is defined and hidden fro privacy.*/

public function connect(){
        return mysqli_connect(self::HOST,self::USERNAME,self::PASSWORD,self::DATBASE);
}



public function insertData($postData){
        $sqli = 'INSERT INTO `payments`(`item_name`,`price`,`email`,`date`,`time`,`detail`,`volume`,`ip`,`payment_status`) VALUES ("'.$productData['name'].'","'.$productData['price'].'","'.$postData['email'].'","'.$date.'","'.$time.'","'.$postData['list'].'","'.$productData['volume'].'","'.$ip.'","On Hold")';


       if(mysqli_query($this->connect(),$sqli)){
           print_r(mysqli_insert_id($this->connect()));
      }else{
                print_r(mysqli_error($this->connect()));
      }
   }
}

It is inserting the data and working fine but does not return anything other than a 0 (zero) not getting any error.

Let me clear that my table contains auto_increment and my connection is fine because it is entering data just fine. Please don't disregard the question because you found similar once answered. I also found similar questions but most of them have connection problems, some of them the no AUTO_INCREMENT column and there may be some which included another query in between. So, please read it before mentioning another answer.

Here is the proof of AUTO_INCREMENT: enter image description here

2 Answers2

6

I suspect this code has been migrated from the legacy mysql extension where mysql_connect():

Opens or reuses a connection to a MySQL server.

This is no longer the case with mysqli_connect():

Opens a connection to the MySQL Server.

You really need to store the connection in a variable and reuse that same connection. Currently you're starting many different connections within the same script run.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
1

When you call mysqli_connect() it will create a new connection each time. It does not reuse the previous connection. When a new connection is created all the properties of the old one are lost.

You are calling mysqli_connect() every time you call $this->connect() so you will never get the errors or the auto-generated ID.

mysqli connection should be global to your application. It does not make sense to keep recreating the same connection because it will lead to terrible performance issues and it will cause you problems like this one. If you are using dependency injection it should be easy to create the mysqli at the start of your application and then pass it as an argument to your class' constructor.

Connecting to the database using mysqli is always the same 3 lines of code. Only the details you pass as arguments are different.

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'user', 'pass', 'db_name');
$mysqli->set_charset('utf8mb4'); // always set the charset

You can then pass this to your constructor whenever you create an object and store it in a private property.

Your fixed code would like this:

<?php

class anything_i {
    private mysqli $db = null;

    public function connect(mysqli $db) {
        $this->db = $db;
    }

    public function insertData($postData) {
        /*
        ...
        */
        $stmt = $this->db->prepare('INSERT INTO `payments`(`item_name`,`price`,`email`,`date`,`time`,`detail`,`volume`,`ip`,`payment_status`) VALUES (?,?,?,?,?,?,?,"On Hold")');
        $stmt->bind_param('ssssssss', $productData['name'], $productData['price'], $postData['email'], $date, $time, $postData['list'], $productData['volume'], $ip);
        $stmt->execute();
    }
}

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'inet', '5432', 'test');
$mysqli->set_charset('utf8mb4'); // always set the charset

$obj = new anything_i($mysqli);
Dharman
  • 30,962
  • 25
  • 85
  • 135