PHP, mysqli insert_id not working
<p>I have been going mad trying to work out why insert_id is returning 0 within my database. I upload an image, then upload the which category it is under and a brief description with the image_id (Which should be taken from the insert_id) I cannot figure out why it is not working. </p>

<p>The upload image function is :</p>

<pre><code>function postImageShare($image_name)
{
    global $BEAR;

    if ($stmt = $BEAR-&gt;Database-&gt;prepare("INSERT INTO images SET account_name = '".$_SESSION['account_name']."', image_name = ?"))
    {
        $stmt-&gt;bind_param('s', $image_name);
        $stmt-&gt;execute();
        //$stmt-&gt;close();   
    }       

    $lastItemID  = $BEAR-&gt;Database-&gt;insert_id;
    return $lastItemID; 
}
</code></pre>

<p>Then the function which Inserts the description/image_id and category (called hoard) is the following:</p>

<pre><code>function postImageShareInformation($description, $hoard_id)
{
    global $BEAR;

    if ($stmt = $BEAR-&gt;Database-&gt;prepare("INSERT INTO imagesInformation SET account_name = '".$_SESSION['account_name']."', image_id = '$lastItemID', description = ?, hoard_id = ? "))
    {
        $stmt-&gt;bind_param('si', $description, $hoard_id);
        $stmt-&gt;execute();
        $stmt-&gt;close(); 
    }   

}
</code></pre>

<p>When I run the above the database is populated effectively except for the image_id is 0 everytime. When I have run the above i also get a notice in regards to $lastIteID. I tried placing the </p>

<pre><code>        $lastItemID  = $BEAR-&gt;Database-&gt;insert_id;
</code></pre>

<p>in the second 'postImageShareInformation' function and the inserted result is still 0. Both tables hi id fields that are auto-incremementing, and $BEAR is my connection to the database. Any help would be appreciated as this issue is driving me mad.</p>

<p>UPDATED : </p>

<p>The Html :</p>

<pre><code>&lt;form action=" " id="share_form" method="post" enctype="multipart/form-data"&gt;
       &lt;input type="file" id="share_image" name="share_image" style="display:none"/&gt;
       &lt;div id="upload-image-button" class="uibutton-upload"&gt;Choose Image&lt;/div&gt;        
&lt;/form&gt;
</code></pre>

<p>The above is the form to upload the image, which is done via j'query and PHP below:</p>

<pre><code> $(document).ready(function() { 
        $('#share_image').live('change', function(){ 
            $("#preview_share").html('');
            $("#preview_share").html('&lt;div class="loading_bar"&gt;&lt;div id="image_bar" class="bar"&gt;&lt;span&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;');

            $("#share_form").ajaxForm({
                target: '#preview_share'
            }).submit();
    });
}); 
</code></pre>

<p>Then to upload the image description and hoard is via this form:</p>

<pre><code>&lt;form autocomplete="off" enctype="multipart/form-data" method="post" name="form"&gt;
    &lt;input type="text" name="description"/&gt;&lt;br/&gt;
    &lt;input type="text" name="hoard_id"/&gt;&lt;br/&gt;
    &lt;input type="submit" value="submit"&gt; 
 &lt;/form&gt;
</code></pre>

<p>And this is the php which uses the functions:</p>

<pre><code> if(isset($_POST['description']))
{   // Set and Clean Variables
    $BEAR-&gt;Template-&gt;setData('description', $_POST['description'], TRUE);
    $BEAR-&gt;Template-&gt;setData('hoard_id', $_POST['hoard_id'], TRUE);
    $BEAR-&gt;Webprofile-&gt;postImageShareInformation($BEAR-&gt;Template-&gt;getData('description'), $BEAR-&gt;Template-&gt;getData('hoard_id'));
}

else if(isset($_FILES['share_image']) )
{
    $valid_formats = array("jpg", "jpeg", "png");

    $name = $_FILES['share_image']['name'];
    $size = $_FILES['share_image']['size'];

    if ($size &gt; 2097152)
    {
        echo '&lt;div class="image_error"&gt;Photo Too Large&lt;/div&gt;';
    }
    else if(strlen($name))
    {

        list($txt, $ext) = explode(".", $name);

        if(in_array($ext,$valid_formats))
        {

            // Set Image Upload Data
            $BEAR-&gt;Template-&gt;setData('actual_image_name', $_SESSION['account_name']."-".rand().time().substr(str_replace(" ", "_", $txt), 5).".".$ext);
            $tmp = $_FILES['share_image']['tmp_name'];

            // Move Location
            $location_original = "uploads/test/".$_SESSION['account_name']."/";
            $location_large = "uploads/test/".$_SESSION['account_name']."/large/";
            $location_small = "uploads/test/".$_SESSION['account_name']."/small/";

            if (!file_exists("uploads/test/" . $_SESSION['account_name']))
            {

                mkdir($location_original, 0744);
                mkdir($location_small, 0744);
                mkdir($location_large, 0744);

                move_uploaded_file($tmp, $location_original.$BEAR-&gt;Template-&gt;getData('actual_image_name'));
                $BEAR-&gt;Assets-&gt;create_thumb(125, 125, $location_original, $BEAR-&gt;Template-&gt;getData('actual_image_name'), $location_small);
                $BEAR-&gt;Assets-&gt;create_thumb(500, 500, $location_original, $BEAR-&gt;Template-&gt;getData('actual_image_name'), $location_large);

                echo "&lt;img src='uploads/test/".$_SESSION['account_name']."/large/".$BEAR-&gt;Template-&gt;getData('actual_image_name')."'class='preview'&gt;".$BEAR-&gt;Template-&gt;getData('actual_image_name');
            }
            else
            {
                // Move Image
                move_uploaded_file($tmp, $location_original.$BEAR-&gt;Template-&gt;getData('actual_image_name'));
                $BEAR-&gt;Assets-&gt;create_thumb(125, 125, $location_original, $BEAR-&gt;Template-&gt;getData('actual_image_name'), $location_small);
                $BEAR-&gt;Assets-&gt;create_thumb(500, 500, $location_original, $BEAR-&gt;Template-&gt;getData('actual_image_name'), $location_large);  

                $BEAR-&gt;Webprofile-&gt;postImageShare($BEAR-&gt;Template-&gt;getData('actual_image_name'));
                echo "&lt;img src='uploads/test/".$_SESSION['account_name']."/small/".$BEAR-&gt;Template-&gt;getData('actual_image_name')."' class='preview'&gt;";
            } 
        }
        else 
        {
            echo '&lt;div class="image_error"&gt;Invalid File Type&lt;/div&gt;';
        }                       
    }                        
    else
    {
        echo '&lt;div class="image_error"&gt;Failed&lt;/div&gt;';
    }
}
</code></pre>

<p>All the above allows the user to preview the uploaded image before submitting the description and Hoard ID.Hope this helps.</p>
