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

次に、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;

2 番目の '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 を送信する前に、アップロードされた画像をプレビューできます。

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

今、私はここで何が起こっているのかを理解しました。したがって、次の2つの機能があります。

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を呼び出すときに、返されたIDを変数に割り当てる必要があります。

$lastItemID = postImageShare("myImageName");

2番目の関数を呼び出すと、この変数を最後のパラメーターに割り当てることができます。

postImageShareInformation("My description", 13, $lastItemID);

今は大丈夫です!

于 2013-01-12T13:16:28.803 に答える
-1

これは、テーブルの id 列に AUTO_INCREMENT プロパティがないためだと思います

于 2013-01-12T10:08:17.583 に答える
-1

$Bear オブジェクトは直接の mysqli 接続ではないと思います.それはあなたが作成したクラスですか?

あなたが持っている場合

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

次に、クエリを実行した後

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

最後に挿入されたIDを取得できます

$mysqli->insert_id
于 2013-01-11T22:49:05.920 に答える
-1

MySQL は、関数でこの機能を提供しlast_insert_id()ます。

于 2013-01-11T22:53:05.333 に答える