how to make sure the mysqli prepare statement is correct?












-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;
}









share|improve this question























  • Is it returning a non-zero value?

    – Nick
    Nov 19 '18 at 3:29






  • 1





    Please see How to get MySQLi error information in different environments

    – Phil
    Nov 19 '18 at 3: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 3:30











  • @Nick it is not returning anything.

    – Ms_Lucky13
    Nov 19 '18 at 3:30






  • 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 4:47
















-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;
}









share|improve this question























  • Is it returning a non-zero value?

    – Nick
    Nov 19 '18 at 3:29






  • 1





    Please see How to get MySQLi error information in different environments

    – Phil
    Nov 19 '18 at 3: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 3:30











  • @Nick it is not returning anything.

    – Ms_Lucky13
    Nov 19 '18 at 3:30






  • 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 4:47














-1












-1








-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;
}









share|improve this question














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;
}






php mysqli prepared-statement php-5.6






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 at 3:26









Ms_Lucky13Ms_Lucky13

4,84741628




4,84741628













  • Is it returning a non-zero value?

    – Nick
    Nov 19 '18 at 3:29






  • 1





    Please see How to get MySQLi error information in different environments

    – Phil
    Nov 19 '18 at 3: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 3:30











  • @Nick it is not returning anything.

    – Ms_Lucky13
    Nov 19 '18 at 3:30






  • 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 4:47



















  • Is it returning a non-zero value?

    – Nick
    Nov 19 '18 at 3:29






  • 1





    Please see How to get MySQLi error information in different environments

    – Phil
    Nov 19 '18 at 3: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 3:30











  • @Nick it is not returning anything.

    – Ms_Lucky13
    Nov 19 '18 at 3:30






  • 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 4:47

















Is it returning a non-zero value?

– Nick
Nov 19 '18 at 3:29





Is it returning a non-zero value?

– Nick
Nov 19 '18 at 3:29




1




1





Please see How to get MySQLi error information in different environments

– Phil
Nov 19 '18 at 3:29





Please see How to get MySQLi error information in different environments

– Phil
Nov 19 '18 at 3:29




3




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 3:30





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 3:30













@Nick it is not returning anything.

– Ms_Lucky13
Nov 19 '18 at 3:30





@Nick it is not returning anything.

– Ms_Lucky13
Nov 19 '18 at 3:30




1




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 4:47





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 4:47












1 Answer
1






active

oldest

votes


















0














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






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53367925%2fhow-to-make-sure-the-mysqli-prepare-statement-is-correct%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














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






    share|improve this answer




























      0














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






      share|improve this answer


























        0












        0








        0







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






        share|improve this answer













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







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 21 '18 at 6:52









        Ms_Lucky13Ms_Lucky13

        4,84741628




        4,84741628
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53367925%2fhow-to-make-sure-the-mysqli-prepare-statement-is-correct%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to change which sound is reproduced for terminal bell?

            Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

            Can I use Tabulator js library in my java Spring + Thymeleaf project?