0

I usually don't post detailed versions of my code; however, in this case it may be necessary to figure out the problem. I have a class method that I cannot stop from executing twice. Is there any particular information I am missing on MySQLi prepared statements? I have read similar questions that asks the same to no avail.

I previously asked a question about using eval for dynamic queries in prepared statements as far as it's convention and best practices. I was told to use call_user_func_array() and it worked perfectly; however, I failed to notice that the statement executed twice each time, even with the old eval() code. So I put together a snippet of my ACTUAL code which should pretty much explain itself through my comments

    function insert($table, $query)
    {
        /**
         *
         * This code assumes you have a MySQLi connection stored in variable $db
         * USAGE: insert(table, array('field' => 'value');
         * 
        **/

        // Sets the beginning of the strings for the prepared statements

        $fields = $values = "(";
        $types = "";
        $params = array();

        foreach($query as $key => $val)
        {               
            // array keys = fields, and array values = values;
            $fields.= $key;

            // concatenate the question marks for statement
            $values.= "?";

            // concatenate the type chars
            $types.= is_string($val) ? "s" : (is_int($val) ? "i" : (is_double($val) ? "d" : "b"));

            // pass variables to array params by reference for call_user_func_array();
            $params[] = &$query[$key];

            if($val == end($query))
            {
                $fields .= ")";
                $values .= ")";
                array_unshift($params, $types);
            }
            else
            {
                $fields .= ", ";
                $values .= ", ";
            }
        }
        $str = "INSERT INTO {$table} {$fields} VALUES {$values}";
        if($stmt = $db->prepare($str))
        {
            call_user_func_array(array($stmt, 'bind_param'), $params);

            /**
             *
             * This is where I am pulling my hair out of my head and being 3
             * nothces away from banging my own head into the screen and
             * being without a computer at all.
             *
             * I have tried everything I can think of. I gotta be missing
             * something
             *
             * IT JUST KEEPS SENDING 2 ROWS DANG IT!
             *
             **/


            /////////////////////
            $stmt->execute();//// <---Help is needed here
            /////////////////////

            //-- Close connection;
            $stmt->close();
        }
        else
        {
            //-- Send a nice readable error msg
            die("<center><h3>FAULTY QUERY STRING</h3><h4>Please check query string</h4><p>{$str}</p>");
        }
    }

Changed code format from OOP to regular function for testing without having to create a class.

scottyrox
  • 11
  • 5
  • This question looks similar to [this one](http://stackoverflow.com/questions/20031963/php-mysqli-prepared-statement-executes-twice), which actually never got answered. – Tim Biegeleisen Jan 15 '16 at 05:04
  • 1
    Are you sure you're not calling `insert()` twice? This code works correctly for me. – jbafford Jan 15 '16 at 05:04
  • check SQL->prepare it might be called there may be? – PoX Jan 15 '16 at 05:08
  • I'm not sure. I might have to just strip it down into functions and see if it does it on it's own. It's probably in my __construct() – scottyrox Jan 15 '16 at 05:09
  • @TimBiegeleisen actually it is. I made a new page testing $db as MySQLi and using the class method as the above function and it's the same results. Is it a possible configuration error on my server? – scottyrox Jan 15 '16 at 05:28
  • To ask the obvious, is there any chance that your application logic for insertion is somehow being called twice? I've never heard of MySQL queries being executed twice, not without a trigger or proc having been setup to do so. – Tim Biegeleisen Jan 15 '16 at 05:37
  • I'm not sure. I'm gonna debug and check tomorrow. I really appreciate you guy's input. I'm sure we'll figure it out. – scottyrox Jan 15 '16 at 06:48
  • It actually worked as is once I threw it into an http request. I guess it was just the testing environment. Thanks for the concerns – scottyrox Jan 19 '16 at 23:56

1 Answers1

-1

old question, but people might still stumble upon it, I did.

I think the mysqli_query function is mainly for retrieving data, I had the same problem, and fixed it by using mysqli_real_query on insert and update queries.

dudah84
  • 19
  • 2