dongyuan4790 2018-01-27 19:06
浏览 19

用于编辑和更改图像的php代码

How can I replace an image file when editing a database record that contains an image?

This is my code that displays the image to be edited:

</tr>
          <tr valign="baseline">
            <td align="right" nowrap="nowrap" class="style4">&nbsp;</td>
            <td><img src="<?php echo $row_Member_info['image1']; ?>" width="100%" /><p align="center"><strong>Image 1</strong></td>
          </tr>

This is what I am trying to add to replace the image file with another image but does not work:

<tr valign="baseline">
            <td align="right" nowrap="nowrap" class="style4">Change Image1:</td>
             <td colspan="2" class="style1">Choose a file to upload: <input name="image1" type="file" /><br></td>
            <td class="style1">&nbsp;</td>
       </tr>

This is my code for updating the record which works except for replacing the image. The new image is not writing to the database and not saving to the 'uploads/' folder. Also, does not generate any error code:

// This displayes the selected row from the previous page
$colname_Member_info = "-1";
if (isset($_GET['recordID'])) {
$colname_Member_info = $_GET['recordID'];
}
mysql_select_db(strato11_members);
$query_Member_info = sprintf("SELECT * FROM Member_info WHERE id = %s", GetSQLValueString($colname_Member_info, "text"));
$Member_info = mysql_query($query_Member_info) or die(mysql_error());
$row_Member_info = mysql_fetch_assoc($Member_info);
$totalRows_Member_info = mysql_num_rows($Member_info);

// Form1 starts here to update the selected record
$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

// added by Jim to set image formats

     function GetImageExtension($imagetype) {
        if(empty($imagetype)) return false;
        switch($imagetype) {
            case 'image/bmp': return '.bmp';
            case 'image/gif': return '.gif';
            case 'image/jpeg': return '.jpg';
            case 'image/png': return '.png';
            default: return false;
        }
    } 

// added by JK to process image1
$target = "uploads/"; 
    if (isset($_FILES["image1"]["tmp_name"]) && !empty($_FILES["image1"]["tmp_name"])) {
        $temp_name=$_FILES["image1"]["tmp_name"];
        $imgtype=$_FILES["image1"]["type"];
        $ext= GetImageExtension($imgtype);

//This is the directory where images will be saved 
        if (!$ext) die("Upload file format is not allowed");
        $target1 = $target . basename( $_FILES['image1']['name']);
        if(move_uploaded_file($temp_name, $target1)) {
        } else {
            exit("Error While uploading image on the server"); 
        }
    }

// JK code ends

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
$updateSQL = sprintf("UPDATE `Member_info` SET name=%s, rank=%s, departed=%s, service=%, lodd=%s, veteran=%s, branch=%s, decorated=%s, comments=%s, image1=%, cemetery=%s WHERE id=%s",                
                   GetSQLValueString($_POST['name'], "text"),
                   GetSQLValueString($_POST['rank'], "text"),
                   GetSQLValueString($_POST['departed'], "text"),
                   GetSQLValueString($_POST['service'], "text"),
                   GetSQLValueString($_POST['lodd'], "text"),
                   GetSQLValueString($_POST['veteran'], "text"),
                   GetSQLValueString($_POST['branch'], "text"),
                   GetSQLValueString($_POST['decorated'], "text"),
                   GetSQLValueString($_POST['comments'], "text"),
                   GetSQLValueString($_POST['cemetery'], "text"),
                   GetSQLValueString($target1,"text"),
                   GetSQLValueString($_POST['id'], "text"));



// code for entering it into a database.
    mysql_select_db(strato11_members);
    $Result1 = mysql_query($updateSQL) or die(mysql_error());

$updateGoTo = "view_members.php";
if (isset($_SERVER['QUERY_STRING'])) {
$updateGoTo .= (strpos($updateGoTo, '?')) ? "&" : "?";
$updateGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $updateGoTo));
}
  • 写回答

1条回答 默认 最新

  • dsvbtgo639708 2018-01-27 20:24
    关注

    In your form tag did you use enctype="multipart/form-data". Because this is the most thing that programmer forgot to use this when we use input type='file'. So First of all check enctype in form tag that you used or not if not then please use this and hope it will upload your file.

    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么