我想编辑我上传的图片和产品名称

时间:2012-02-16 06:34:00

标签: php file-upload

这是我的index.php文件

<?php
require_once('../admin/constant.php');
$host="localhost"; // Host name
$username="***"; // Mysql userna
$password="*****"; // Mysql password
$db_name="***"; // Database name
$tbl_name="users"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$result=mysql_query("SELECT * FROM users ORDER BY id DESC");
?>

<html>
<head>
<title>Homepage</title>

</head>


<body>
<a href="add.php">Add New Data</a><br/><br/>
<?php
echo "<table width='80%' border=0 align='center'>";
echo "<tr bgcolor='#CCCCCC'>";
echo "<td>Product Name</td>";
echo "<td>Normal Image</td>";
echo "<td>Selected Image</td>";
echo "<td>Update</td>";
echo "</tr>";
while($res=mysql_fetch_array($result)){
    echo "<tr>";
    echo "<td>".$res['name']."</td>";
        ?>
    <td>
<?php     
$handle = opendir(dirname(realpath(__FILE__)).'/images/');
echo '<img style="height:50px; width="20px;"" src="upload/product/'.$res['id'].'/'.$res['image'].'" border="0" />'; 
?></td>
<td>
<?php     
$handle = opendir(dirname(realpath(__FILE__)).'/images/');
echo '<img style="height:50px; width="20px;"" src="upload/product/'.$res['id'].'/'.$res['image1'].'" border="0" />'; 
?></td>

<?php 

echo "<td><a href=\"edit.php?id=$res[id]\">Edit</a> | <a href=\"delete.php?id=$res[id]\">Delete</a></td>";

}
echo "</table>";
?>

这显示了我的产品名称及其图像列表  问题是,当我点击编辑链接然后编辑不工作意味着名称,图像不更新PLZ指导我。 下面是我的edit.php

<?php
require_once('../admin/constant.php');  
error_reporting(0);//including the database connection file
$host="localhost"; // Host name
$username="***"; // Mysql username
$password="***"; // Mysql password
$db_name="***"; // Database name
$tbl_name="users"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

if(isset($_POST['update']))
{
    $ProdImg        = false;
    $ProdIcon       = false;
    $id             = $_POST['id'];
    $pic_name_hid   = $_POST['hidimage'];//product icon
    $pic_name_hid1  = $_POST['hidimage1'];//seelct image
    $name           = $_POST['product_name'];
    $sql            = "update users set name='$name',image ='$pic_name_hid',image1='$pic_name_hid1' where id=$id";
    echo $sql;
    $res            = mysql_query($sql);
    header("Location: index.php");

    if($_FILES['product_icon']['name'] == "")
        {
            $ProdImg                    = true;
            $picture_filename           = $_POST['hidimage'];
            $data_all["image"]          = $_POST["hidimage"];

            print_r($data_all);
        }
    if($ProdImg == false) { 
            $image          = $_FILES['product_icon']['name'];

            $extension      = substr(strrchr($image, '.'), 1);
            $port_tmp_name  = $_FILES['product_icon']['tmp_name'];
            $result         =mysql_query("SELECT * FROM users WHERE id=$id");
            $obj            =mysql_fetch_object($result);

            print_r($obj);
            $icon_nm            = $obj->id.".".$extension;

            $sql="UPDATE users SET name='$name',image='$icon_nm',image1='$pic_name_hid1' WHERE id=$id";
            $result=mysql_query($sql);
            if(is_dir(SITE_UPLOAD_PRODUCT_PATH."/".$obj->id)) 
            {
                    mkdir(SITE_UPLOAD_PRODUCT_PATH."/".$obj->id);
                    move_uploaded_file($port_tmp_name, SITE_UPLOAD_PRODUCT_PATH.$obj->id."/".$icon_nm);
                    unlink(SITE_UPLOAD_PRODUCT_PATH."/".$obj->id);
                    move_uploaded_file($selected_tmp_nm, SITE_UPLOAD_PRODUCT_PATH.$obj->id."/".$icon_nm);
                    unlink(SITE_UPLOAD_PRODUCT_PATH."/".$obj->id);
                    header("Location: index.php");exit;
            }
        }


    $image          = $_FILES['product_icon']['name'];
    $extension      = substr(strrchr($image, '.'), 1);
    $port_tmp_name  = $_FILES['product_icon']['tmp_name'];

    $image1             =$_FILES['select_image']['name'];   
    $extension_img      = substr(strrchr($image1, '.'), 1);
    $selected_tmp_nm    = $_FILES['select_image']['tmp_name'];

    else
    {   
        //updating the table
        $result=mysql_query("SELECT * FROM users WHERE id=$id");
        $obj    =mysql_fetch_object($result);

        $icon_nm            = $obj->id.".".$extension;
        $selected_nm        = $obj->id."_selected.".$extension_img;

        $sql="UPDATE users SET name='$name',image='$icon_nm',image1='$selected_nm' WHERE id=$id";
        $result=mysql_query($sql);
        if(is_dir(SITE_UPLOAD_PRODUCT_PATH."/".$obj->id)) 
        {
                mkdir(SITE_UPLOAD_PRODUCT_PATH."/".$obj->id);
                move_uploaded_file($port_tmp_name, SITE_UPLOAD_PRODUCT_PATH.$obj->id."/".$icon_nm);
                unlink(SITE_UPLOAD_PRODUCT_PATH."/".$obj->id);
                move_uploaded_file($selected_tmp_nm, SITE_UPLOAD_PRODUCT_PATH.$obj->id."/".$selected_nm);
                unlink(SITE_UPLOAD_PRODUCT_PATH."/".$obj->id);
}
        //redirectig to the display page. In our case, it is index.php
        header("Location: index.php");
    }
}
?>
<?php
//for displaying data of this particular data

//getting id from url
$id = $_GET['id'];

//selecting data associated with this particular id
$result=mysql_query("select * from users where id=$id");

while($res=mysql_fetch_object($result))
{

     $tid=$res->id;
     $name = $res->name;
     $image = $res->image;
    $image1 = $res->image1;
}
?>
<html>
<title>Edit Data</title>
<body>
<a href="index.php">Home</a>
<br/><br/>
<form name="form1" method="post" action="edit.php" enctype="multipart/form-data">
<table border="0" align="center">

  <tr> 
      <td>Product Name</td>
      <td><input type="text" name="product_name" id="product_name" value="<?php echo $name;?>"><span style="color:Red; "></span></td>
      &nbsp;<td ><span id="errproduct_name" style="width:50px;"></span> </td>

    </tr>

  <tr> 
      <td>Normal Images</td>
      <td><input type="file" name="product_icon" id="product_icon" value="<?php echo $image;?>">
      <span style="color:Red;"></span></td>
      <td><?php if($image != '') {  echo "<br>";?>
          <img src="<?php echo SITE_UPLOAD_PRODUCT_URL. $tid."/" .$image; ?>" width="50px" height="60px"  /><br>
          <?php } ?>
      </td>
      <td ><span id="errproduct_icon"></span></td>
   </tr>
<tr> 
      <td>Selector Images</td>
      <td><input type="file" name="select_image" id="select_image" value="<?php echo $image1;?>/><span style="color:Red;"></span></td>
      <td><?php if($image1 != '') {  echo "<br>";?>
          <img src="<?php echo SITE_UPLOAD_PRODUCT_URL. $tid."/" .$image1; ?>" width="50px" height="60px"  /><br>
          <?php } ?>
      </td>
      <td ><span id="errselect_image"></span></td>
    </tr>
<tr>
    <td><input type="hidden" name="id" value=<?php echo $_GET['id'];?>>  
    <input type="hidden" name="hidimage" id="hidimage" value=<?php echo $image?>> 
     <input type="hidden" name="hidimage1" id="hidimage1" value=<?php echo $image1?>>  </td>
    <td><input type="submit" name="update" value="Update"></td>
  </tr>
</table>
</form>

</body>
</html>

PLZ给我编辑EDIT.PHP :(

1 个答案:

答案 0 :(得分:0)

如果我在这么晚的时候正确地阅读你的代码 - 而不是这个:

“更新用户设置名称='$ name',image ='$ pic_name_hid',image1 ='$ pic_name_hid1',其中id = $ id”;

试试这个:

“UPDATE users SET name ='”。 $ name。 “',image ='”。 $ pic_name_hid。 '“,image1 ='”。 $ pic_name_hid1。 '“WHERE id ='”。 $ id。 ““;

我假设你已经检查过你的POST变量确实正确传递(错别字等)