我一直在疯狂地试图弄清楚为什么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;
}
然后插入description / image_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>';
}
}
以上所有内容都允许用户在提交说明和Hoard ID之前预览上传的图像。希望这会有所帮助。
答案 0 :(得分:1)
我觉得你的函数postImageShareInformation($description, $hoard_id)
没有得到$lastItemID
变量。因此,请尝试在函数本身中传递$lastItemID
的值,如下所示。
function postImageShareInformation($description, $hoard_id, $lastItemID)
{
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();
}
}
更新问题
在调用postImageShareInformation()
函数之前,您正在调用postImageShare()
函数。因此它永远不会得到$lastItemID
。假设您使用我的新修改函数,我已经修改了您的代码。但是这仍然可以给你一个想法。
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);
// This will return $lastItemID which we will pass now to postImageShareInformation() function in the block below.
$lastItemID = $BEAR->Webprofile->postImageShare($BEAR->Template->getData('actual_image_name'));
if(isset($_POST['description']))
{ // Set and Clean Variables
$BEAR->Template->setData('description', $_POST['description'], TRUE);
$BEAR->Template->setData('hoard_id', $_POST['hoard_id'], TRUE);
//You were calling this function before ...
$BEAR->Webprofile->postImageShareInformation($BEAR->Template->getData('description'), $BEAR->Template->getData('hoard_id'),$lastItemID);
}
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>';
}
}
这应该有用......
希望它有所帮助。
答案 1 :(得分:0)
我假设$ Bear对象不是直接的mysqli连接..这是一个由你创建的类?
如果你有
$mysqli = new MySQLi($host, $user, $password, $db_name);
然后运行查询
$mysqli->query("INSERT INTO test VALUES ('test')");
您可以通过
获取最后插入的ID$mysqli->insert_id
答案 2 :(得分:0)
MySQL在last_insert_id()
函数中提供此功能。
答案 3 :(得分:0)
我想说这是因为你的表的id列没有AUTO_INCREMENT属性
答案 4 :(得分:0)
现在我明白了这里发生了什么。所以我们有这两个功能:
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;
}
和
// Note that I've added a new parameter to this function and also
// I've modified your prepare statement
function postImageShareInformation($description, $hoard_id, $lastItemID)
{
global $BEAR;
if ($stmt = $BEAR->Database->prepare("INSERT INTO imagesInformation SET account_name = '".$_SESSION['account_name']."', image_id = ?, description = ?, hoard_id = ? "))
{
$stmt->bind_param('si', $lastItemID, $description, $hoard_id);
$stmt->execute();
$stmt->close();
}
}
现在,当我们调用第一个时,我们必须将返回的id分配给变量:
$lastItemID = postImageShare("myImageName");
当我们调用第二个函数时,我们可以将此变量赋值给最后一个参数:
postImageShareInformation("My description", 13, $lastItemID);
现在应该没问题!