PHP,mysqli insert_id不工作

PHP,mysqli insert_id不工作,php,Php,我一直在疯狂地试图弄明白为什么insert_id在我的数据库中返回0。我上传了一张图片,然后上传了它所属的类别以及带有图片id的简要描述(应该取自插入id),我无法理解为什么它不起作用 上载图像功能是: function postImageShare($image_name) { global $BEAR; if ($stmt = $BEAR->Database->prepare("INSERT INTO images SET account_name = '".$

我一直在疯狂地试图弄明白为什么insert_id在我的数据库中返回0。我上传了一张图片,然后上传了它所属的类别以及带有图片id的简要描述(应该取自插入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和类别(称为囤积)的函数如下所示:

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(); 
    }   

}
当我运行上面的命令时,数据库被有效地填充,除了每次映像的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();
    });
}); 
$(文档).ready(函数(){
$('share_image').live('change',function(){
$(“#预览共享”).html(“”);
$(“#预览共享”).html(“”);
$(“#股份形式”).ajaxForm({
目标:“#预览#U共享”
}).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>';
    }
}
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; 
}
if(isset($\u POST['description']))
{//Set和Clean变量
$BEAR->Template->setData('description',$\u POST['description',TRUE);
$BEAR->Template->setData('囤积id',$U POST['囤积id',TRUE);
$BEAR->Webprofile->postImageShareInformation($BEAR->Template->getData('description'),$BEAR->Template->getData('囤积id');
}
else if(isset($\u文件['share\u image']))
{
$valid_formats=数组(“jpg”、“jpeg”、“png”);
$name=$\u文件['share\u image']['name'];
$size=$\u文件['share\u image']['size'];
如果($size>2097152)
{
回声“照片太大”;
}
else if(斯特伦($name))
{
列表($txt,$ext)=分解(“.”,$name);
if(在数组中($ext,$valid_格式))
{
//设置图像上载数据
$BEAR->Template->setData('actual_image_name',$_SESSION['account_name'].-“.rand().time().substr(str_replace(“,”,“,$txt),5)。”$ext);
$tmp=$\u文件['share\u image']['tmp\u name'];
//移动位置
$location_original=“uploads/test/”$\会话['account\u name']。“/”;
$location\u large=“uploads/test/”$\u SESSION['account\u name']。“/large/”;
$location_small=“uploads/test/”$\u SESSION['account\u name']。“/small/”;
如果(!file_存在(“上载/test/”$_会话['account_name']))
{
mkdir(原件,0744);
mkdir($0744);
mkdir($0744);
移动上传的文件($tmp,$location_original.$BEAR->模板->获取数据('actual_image_name'));
$BEAR->Assets->create_thumb(125125,$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“Template->getData('actual_image_name')。“'class='preview'>”$BEAR->Template->getData('actual_image_name');
}
其他的
{
//移动图像
移动上传的文件($tmp,$location_original.$BEAR->模板->获取数据('actual_image_name'));
$BEAR->Assets->create_thumb(125125,$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“Template->getData('actual_image_name')。“'class='preview'>”;
} 
}
其他的
{
回显“无效文件类型”;
}                       
}                        
其他的
{
echo“失败”;
}
}

所有这些都允许用户在提交描述和囤积ID之前预览上传的图像。希望这有所帮助。

我假设$Bear对象不是直接的mysqli连接。。这是你创建的一个类

如果你有

$mysqli = new MySQLi($host, $user, $password, $db_name);
然后在运行查询之后

$mysqli->query("INSERT INTO test VALUES ('test')");
您可以通过以下方式获取最后插入的id:

$mysqli->insert_id

我假设$Bear对象不是直接的mysqli连接。。这是你创建的一个类

如果你有

$mysqli = new MySQLi($host, $user, $password, $db_name);
然后在运行查询之后

$mysqli->query("INSERT INTO test VALUES ('test')");
您可以通过以下方式获取最后插入的id:

$mysqli->insert_id

MySQL在
last\u insert\u id()函数中提供此功能

MySQL在
last\u insert\u id()
函数中提供此功能

在我看来,您的函数
postmageshareinformation($description,$囤积\u 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(); 
    }   

}
更新后的问题

在调用
postmageshare()
函数之前,您正在调用
postmageshare()
函数。因此,它永远不会得到
$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>';
    }
}
if(isset($\u文件['share\u image']))
{
$valid_formats=数组(“jpg”、“jpeg”、“png”);
$name=$\u文件['share\u image']['name'];
$size=$\u文件['share\u image']['size'];
如果($size>2097152)
{
回声“照片太大”;
}
else if(斯特伦($name))
{
列表($txt),