-1

I've following insert function to insert values in to database using mysqli prepare. But this is not giving me any error and not inserting values in to database. Can somebody help me to troubleshoot?

I can print all these arrays $fields, $placeholders, $values, $format and it's all displaying correct values too.

    public function insert($table, $data, $format) {
            global $dbcon;
// Check for $table or $data not set
            if (empty($table) || empty($data)) {
                return false;
            }

// Cast $data and $format to arrays
            $data = (array) $data;
            $format = (array) $format;

// Build format string
            $format = implode('', $format);
            $format = str_replace('%', '', $format);

            list( $fields, $placeholders, $values ) = $this->prep_query($data);

// Prepend $format onto $values
// array_unshift($values, $format);

// Prepary our query for binding
            $stmt = $dbcon->prepare("INSERT INTO {$table} ({$fields}) VALUES ({$placeholders})");

            if (false === $stmt) {
                die('prepare() failed: ' . htmlspecialchars($mysqli->error));
            }

// Dynamically bind values
            $bp = call_user_func_array(array($stmt, "bind_param"), array_merge($format, $values));
//            $bp = call_user_func_array(array($stmt, 'bind_param'), $this->ref_values($values));
            if (false === $bp) {
                die('bind_param() failed: ' . htmlspecialchars($stmt->error));
            }
// Execute the query
            $result = $stmt->execute();
            if (false === $result) {
                die('execute() failed: ' . htmlspecialchars($stmt->error));
            }
// Check for successful insertion
            $Insertid = $stmt->insert_id;
            return $Insertid;

//            if ($stmt->affected_rows) {
//                return true;
//            } 
//
//            return false;
        }
Lucky13
  • 11,393
  • 7
  • 25
  • 36
  • Is it returning a non-zero value? – Nick Nov 19 '18 at 03:29
  • 1
    Please see [How to get MySQLi error information in different environments](https://stackoverflow.com/questions/22662488/how-to-get-mysqli-error-information-in-different-environments) – Phil Nov 19 '18 at 03:29
  • 3
    I suggest you move to PDO if you want to do dynamic query building things like this. Having to use `call_user_func_array()` and getting the variable references all correct is a pain. With PDO, you can bind individual parameters and / or call `execute()` with an array of values – Phil Nov 19 '18 at 03:30
  • @Nick it is not returning anything. – Lucky13 Nov 19 '18 at 03:30
  • @Ms_Lucky13 PDO and MySQLi were introduced in the same PHP version ~ 5.0 – Phil Nov 19 '18 at 03:43
  • 1
    Did you trace the code line by line? Did you check mysqli's `$error` property for any existing errors? Have you enabled error reporting and taken a look at PHP logs? I think you would better do these before having us on your side. We could not have any clue unless you give us some. – revo Nov 19 '18 at 04:47

1 Answers1

0

Change $bp = call_user_func_array(array($stmt, "bind_param"), array_merge($format, $values)); statement to $bp = $stmt->bind_param($format, ...$values);

Lucky13
  • 11,393
  • 7
  • 25
  • 36