2

我一直在疯狂地试图弄清楚为什么 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 之前预览上传的图像。希望这会有所帮助。

4

5 回答 5

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>';
    }
}

这应该工作...

希望能帮助到你。

于 2013-01-12T09:08:39.567 回答
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);

现在应该就好了!

于 2013-01-12T13:16:28.803 回答
-1

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

如果你有

$mysqli = new MySQLi($host, $user, $password, $db_name);

然后在你运行查询之后

$mysqli->query("INSERT INTO test VALUES ('test')");

你可以通过

$mysqli->insert_id
于 2013-01-11T22:49:05.920 回答
-1

MySQL 在函数中提供了这个last_insert_id()功能。

于 2013-01-11T22:53:05.333 回答
-1

我会说这是因为您的表的 id 列没有 AUTO_INCREMENT 属性

于 2013-01-12T10:08:17.583 回答