データベース内で 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 を送信する前に、アップロードされた画像をプレビューできます。