2011-10-21 38 views
0

我有下面的代碼,顯示使用從MySQL表中的PHP回聲ID給定的圖像。 PHP的是:編輯現有的圖像php mysql

<?php include 'dbc.php'; page_protect(); 


$id=$_GET['id']; 


if(!checkAdmin()) {header("Location: login.php"); 
exit(); 
} 

$host = $_SERVER['HTTP_HOST']; 
$host_upper = strtoupper($host); 
$login_path = @ereg_replace('admin','',dirname($_SERVER['PHP_SELF'])); 
$path = rtrim($login_path, '/\\'); 

foreach($_GET as $key => $value) { 
    $get[$key] = filter($value); 
} 

foreach($_POST as $key => $value) { 
    $post[$key] = filter($value); 
} 
?> 


<?php 
if($_FILES['photo']) 
{ 
    $target = "images/furnishings/"; 
    $target = $target . basename($_FILES['photo']['name']); 

    $title = mysql_real_escape_string($_POST['title']); 
    $pic = "images/furnishings/" .(mysql_real_escape_string($_FILES['photo']['name'])); 
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
{ 

    mysql_query("update `furnishings` set `photo`='$pic' WHERE id='$id'") ;  

    echo "Image updated"; 
} 
else 
{ 
    echo "Please select a new image to upload"; 

} 
} 
?> 

的HTML是:

<form enctype="multipart/form-data" action="editfurnimage.php" method="POST"> 
    <table width="450" border="2" cellpadding="5"class="myaccount"> 
    <tr> 
     <td width="35%" class="myaccount">Current Image: </td> 
     <td width="65%"><img src='<?php 

mysql_select_db("dbname", $con); 
mysql_set_charset('utf8'); 

$result = mysql_query("SELECT * FROM furnishings WHERE id='$id'"); 

while($row = mysql_fetch_array($result)) 
{ 
    echo '' . $row['photo'] . ''; 

} 
mysql_close($con); 
?>' style="width:300px; height:300px;"></td> 
    </tr> 
    <tr> 
     <td class="myaccount">New Image: </td> 
     <td><input type="file" name="photo" /></td> 
    </tr> 
    <tr> 
     <td colspan="2"><input type="submit" class="CMSbutton" value="Add" /></td> 
    </tr> 
    </table> 
</form> 

雖然編碼添加到服務器的新形象,mysql表不似乎與新的圖像進行更新 - 沒有事實變化正在做 - 當我調整行:

mysql_query("update `furnishings` set `photo`='$pic' WHERE id='$id'") ; 

到:

mysql_query("update `furnishings` set `photo`='$pic' WHERE id='8'") ; 

它的工作原理雖然如此,假設問題是躺在這部分的代碼,但不知道如何糾正代碼以正確拉動$標識到PHP。

最後,當腳本運行時,我試圖讓頁面「editfurnimage.php?id = $ id」在用戶點擊添加按鈕後重新加載 - 此刻返回的頁面是「editfurnimage.php 「這顯然沒有顯示錶中的任何數據。

任何幫助非常感謝 - 並一如既往隨時撕碎我的代碼 - 仍然學習!

感謝 JD

回答

0

嘗試刪除周圍的$ id的單引號。 如果你的數據庫中的id字段爲int,那麼引號不應該用在它周圍。

編輯:錯過了這一個 - $ _GET ['id']是從哪裏發出的,因爲你的表單肯定沒有在$ _GET範圍內發送任何標識符?嘗試添加名爲「id」的輸入並將其值輸入到表單中。另外,在你的php文件中使用$ _POST,而不是$ _GET。

在你的PHP,替換:

$id=$_GET['id']; 

隨着

if(isset($_POST['id'])){ 
$id=$_POST['id']; 
}else{ 
$id=$_GET['id']; 
} 

然後在你的HTML添加:

<input type="hidden" name="id" value="<?php echo $id; ?>"/> 
+0

我在表中顯示的所有項目網頁 - 點擊這些項目中的一個,出現一個新頁面(editfurnimage.php?id =),根據所選項目的ID。然後它在HTML中生成圖像 - SELECT * FROM furniture WHERE id ='$ id'。 – JD2011

+0

您仍然需要在表單中創建一個name ='id'和value ='the-id-value'的輸入,以便在頁面刷新時可以使用$ _POST ['id']。除非您向已刷新頁面發送值,否則您的$ id將爲空。我已經更新了我的代碼,可能有所幫助。 –

+0

感謝vm richard! – JD2011