[英]PHP, mysqli insert_id not working
我一直在瘋狂嘗試找出為什么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之前預覽上傳的圖像。希望這會有所幫助。
在我看來,您的函數postImageShareInformation($description, $hoard_id)
沒有獲得$lastItemID
變量。 因此,嘗試像下面$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>';
}
}
這應該工作...
希望能幫助到你。
我假設$ Bear對象不是直接的mysqli連接。它是由您創建的類?
如果你有
$mysqli = new MySQLi($host, $user, $password, $db_name);
然后在您運行查詢后
$mysqli->query("INSERT INTO test VALUES ('test')");
您可以通過以下方式獲取最后插入的ID
$mysqli->insert_id
MySQL在last_insert_id()
函數中提供了此功能。
我想說這是因為您的表格的ID列沒有AUTO_INCREMENT屬性
現在我知道這里發生了什么。 所以我們有這兩個功能:
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);
現在應該就好了!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.