我有下面的代碼,顯示使用從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
我在表中顯示的所有項目網頁 - 點擊這些項目中的一個,出現一個新頁面(editfurnimage.php?id =),根據所選項目的ID。然後它在HTML中生成圖像 - SELECT * FROM furniture WHERE id ='$ id'。 – JD2011
您仍然需要在表單中創建一個name ='id'和value ='the-id-value'的輸入,以便在頁面刷新時可以使用$ _POST ['id']。除非您向已刷新頁面發送值,否則您的$ id將爲空。我已經更新了我的代碼,可能有所幫助。 –
感謝vm richard! – JD2011