我一直在疯狂地试图弄清楚为什么 insert_id 在我的数据库中返回 0。我上传了一张图片,然后上传了它所属的类别以及带有 image_id 的简短描述(应该从 insert_id 中获取)我无法弄清楚它为什么不起作用。
上传图片功能是:
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;
}
然后插入描述/图像ID和类别(称为hoard)的函数如下:
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();
}
}
当我运行上面的数据库时,除了 image_id 每次都是 0 之外,数据库被有效地填充。当我运行上述内容时,我还会收到关于 $lastIteID 的通知。我试着把
$lastItemID = $BEAR->Database->insert_id;
在第二个“postImageShareInformation”函数中,插入的结果仍然是 0。两个表都具有自动递增的 id 字段,$BEAR 是我与数据库的连接。任何帮助将不胜感激,因为这个问题让我发疯。
更新 :
的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>
上面是上传图片的表格,下面是通过j'query和PHP完成的:
$(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();
});
});
然后通过此表单上传图像描述和囤积:
<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>
这是使用以下功能的 php:
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>';
}
}
以上所有内容都允许用户在提交描述和囤积 ID 之前预览上传的图像。希望这会有所帮助。