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.
The upload image function is :
function postImageShare($image_name)
{
global $BEAR;
if ($stmt = $BEAR->Database->prepare("INSERT INTO images SET account_name = '".$_SESSION['account_name']."', image_name = ?"))
{
$stmt->bind_param('s', $image_name);
$stmt->execute();
//$stmt->close();
}
$lastItemID = $BEAR->Database->insert_id;
return $lastItemID;
}
Then the function which Inserts the description/image_id and category (called hoard) is the following:
function postImageShareInformation($description, $hoard_id)
{
global $BEAR;
if ($stmt = $BEAR->Database->prepare("INSERT INTO imagesInformation SET account_name = '".$_SESSION['account_name']."', image_id = '$lastItemID', description = ?, hoard_id = ? "))
{
$stmt->bind_param('si', $description, $hoard_id);
$stmt->execute();
$stmt->close();
}
}
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
$lastItemID = $BEAR->Database->insert_id;
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.
UPDATED :
The Html :
<form action=" " id="share_form" method="post" enctype="multipart/form-data">
<input type="file" id="share_image" name="share_image" style="display:none"/>
<div id="upload-image-button" class="uibutton-upload">Choose Image</div>
</form>
The above is the form to upload the image, which is done via j'query and PHP below:
$(document).ready(function() {
$('#share_image').live('change', function(){
$("#preview_share").html('');
$("#preview_share").html('<div class="loading_bar"><div id="image_bar" class="bar"><span></span></div></div>');
$("#share_form").ajaxForm({
target: '#preview_share'
}).submit();
});
});
Then to upload the image description and hoard is via this form:
<form autocomplete="off" enctype="multipart/form-data" method="post" name="form">
<input type="text" name="description"/><br/>
<input type="text" name="hoard_id"/><br/>
<input type="submit" value="submit">
</form>
And this is the php which uses the functions:
if(isset($_POST['description']))
{ // Set and Clean Variables
$BEAR->Template->setData('description', $_POST['description'], TRUE);
$BEAR->Template->setData('hoard_id', $_POST['hoard_id'], TRUE);
$BEAR->Webprofile->postImageShareInformation($BEAR->Template->getData('description'), $BEAR->Template->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 > 2097152)
{
echo '<div class="image_error">Photo Too Large</div>';
}
else if(strlen($name))
{
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats))
{
// Set Image Upload Data
$BEAR->Template->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->Template->getData('actual_image_name'));
$BEAR->Assets->create_thumb(125, 125, $location_original, $BEAR->Template->getData('actual_image_name'), $location_small);
$BEAR->Assets->create_thumb(500, 500, $location_original, $BEAR->Template->getData('actual_image_name'), $location_large);
echo "<img src='uploads/test/".$_SESSION['account_name']."/large/".$BEAR->Template->getData('actual_image_name')."'class='preview'>".$BEAR->Template->getData('actual_image_name');
}
else
{
// Move Image
move_uploaded_file($tmp, $location_original.$BEAR->Template->getData('actual_image_name'));
$BEAR->Assets->create_thumb(125, 125, $location_original, $BEAR->Template->getData('actual_image_name'), $location_small);
$BEAR->Assets->create_thumb(500, 500, $location_original, $BEAR->Template->getData('actual_image_name'), $location_large);
$BEAR->Webprofile->postImageShare($BEAR->Template->getData('actual_image_name'));
echo "<img src='uploads/test/".$_SESSION['account_name']."/small/".$BEAR->Template->getData('actual_image_name')."' class='preview'>";
}
}
else
{
echo '<div class="image_error">Invalid File Type</div>';
}
}
else
{
echo '<div class="image_error">Failed</div>';
}
}
All the above allows the user to preview the uploaded image before submitting the description and Hoard ID.Hope this helps.