MySQLi Insert prepared statement via PHP
        Posted  
        
            by 
                Jimmy
            
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Jimmy
        
        
        
        Published on 2014-06-01T02:16:54Z
        Indexed on 
            2014/06/01
            3:26 UTC
        
        
        Read the original article
        Hit count: 217
        
Howdie do,
This is my first time dealing with MySQLi inserts. I had always used mysql and directly ran the queries. Apparently, not as secure as MySQLi.
Anywho, I'm attempting to pass two variables into the database. For some reason my prepared statement keeps erroring out.
I'm not sure if it's a syntax error, but the insert just won't work.
I've updated the code to make the variables easier to read
Also, the error is specifically, Error preparing statement.
I've updated the code, but it's not a pHP error. It's a MySQL error as the script runs but fails at the execution of:
if($stmt = $mysqli -> prepare("INSERT INTO subnets (id, subnet, mask, sectionId, description, vrfId, masterSubnetId, allowRequests, vlanId, showName, permissions, pingSubnet, isFolder, editDate) VALUES ('', ?, ?, '1', '', '0', '0', '0', '0', '0', '{"3":"1","2":"2"}', '0', '0', 'NULL')")) {
I'm going to enable MySQL error checking. I honestly didn't know about that.
<?php
error_reporting(E_ALL);
function InsertIPs($decimnal,$cidr)
{
    error_reporting(E_ALL);
    $mysqli = new mysqli("localhost","jeremysd_ips","","jeremysd_ips");
       if(mysqli_connect_errno()) 
       {
          echo "Connection Failed: " . mysqli_connect_errno();
          exit();
       }
if($stmt = $mysqli -> prepare("INSERT INTO subnets (id, subnet, mask,sectionId,description,vrfld,masterSubnetId,allowRequests,vlanId,showName,permissions,pingSubnet,isFolder,editDate) VALUES ('',?,?,'1','','0','0','0','0','0', '{'3':'1','2':'2'}', '0', '0', NULL)"))
            {
                    $stmt-> bind_param('ii',$decimnal,$cidr);
                    if($stmt-> execute())
                    {
                            echo "Row added successfully\n";
                    }
                    else
                    {
                            $stmt->error;
                    }
                    $stmt -> close;
            }
            }
            else
            {
                    echo 'Error preparing statement';
            }
    $mysqli -> close();
}
$SUBNETS = array ("2915483648 | 18");
foreach($SUBNETS as $Ip)
{
                list($TempIP,$TempMask) = explode(' | ',$Ip);
                echo InsertIPs($Tempip,$Tempmask);
}
?>
This function isn't supposed to return anything. It's just supposed to perform the Insert.
Any help would be GREATLY appreciated. I'm just not sure what I'm missing here
© Stack Overflow or respective owner