编辑现有图像 php mysql

发布于 2024-12-10 22:51:04 字数 2538 浏览 0 评论 0原文

我有以下代码,使用 mysql 表中的 php echo id 显示给定图像。 php 是:

    <?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'") ; 

it虽然可以工作,但假设问题出在这部分代码上,但不确定如何更正代码以将 $id 正确拉入 php 中。

最后,当脚本运行时,我试图在用户单击“添加”按钮后重新加载页面“editfurnimage.php?id=$id” - 目前返回的页面是“editfurnimage.php”,这显然不是显示表中的任何数据。

非常感谢任何帮助 - 并且一如既往地随意撕开我的编码 - 仍在学习!

谢谢 京东

I have the following code that displays a given image using php echo id from a mysql table. The php is:

    <?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"; 

}
} 
?>

The HTML is:

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

While the coding is adding the new image to the server, the mysql table doesnt seem to be updating with the new image - in fact no changes are being made - when I adjust the line:

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

to:

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

it works though so assuming the issue is lying with this part of the code but not sure how to correct the code to pull the $id into the php correctly.

Finally, when the script runs I am trying to get the page "editfurnimage.php?id=$id" to reload following the user clicking the Add button - at the moment the page that is returned is "editfurnimage.php" which obviously doesnt show up any data from the table.

Any help much appreciated - and as always feel free to tear my coding apart - still learning!!

Thanks
JD

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你如我软肋 2024-12-17 22:51:05

尝试删除 $id 周围的单引号。
如果数据库中的 id 字段为 int,则不应在其周围使用引号。

编辑:错过了这一点 - $_GET['id'] 是从哪里发送的,因为您的表单肯定没有在 $_GET 范围内发送任何 id?尝试将名称为“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; ?>"/>

try to remove your single quotes around $id.
If your id field in the database in an int, then quotes should not be used around it.

EDIT: Missed this one - Where is $_GET['id'] being sent from, because your form sure isn't sending any id in the $_GET scope? Try adding the input with a name of 'id' and a value for it in to your form. also, use $_POST in your php file, not $_GET.

In your php, replace:

$id=$_GET['id'];

With

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

Then in your html add:

<input type="hidden" name="id" value="<?php echo $id; ?>"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文