【问题标题】:how to send dynamic value in input type file?如何在输入类型文件中发送动态值?
【发布时间】:2015-06-25 10:31:00
【问题描述】:

我创建了一个编辑表单,我正在编辑我的房间号和房间描述以及房间图像等。基于类别 ID。

第一个编辑表单将从数据库中获取数据。 示例代码:

if (isset($_GET['id'])) 
                { 
                    $current_id = $_GET['id'];
                    $query=mysql_query("select * from room where room_id='$current_id'");
                        $i = 0; 
                        while($row=mysql_fetch_array($query))
                        {
                            $i++; 
                            $room_id=$row['room_id'];
                            $cat_id=$row['cat_id'];
                            $room_number=$row['room_no'];
                            $room_desc=$row['room_desc'];
                            $room_image=$row['room_image'];

                    ?>
                    <table class="table table-bordered table-hover table-striped">
                            <thead>
                                <tr>
                                    <th>Room Id</th>
                                    <th>Category Type</th>
                                    <th>Room Number</th>
                                    <th>Room Description</th>
                                    <th>Room Image</th>

                                </tr>
                            </thead>
                            <tbody>
                             <tr class="active">
                                    <td><input type="text" value="<?php echo $room_id; ?>" readonly="readonly" name="room_id"></td>
                                    <td>

                                    <?php           
                                                // Slecting cat_id for displaying cat_type on Edit form
                                    $query_room=mysql_query("SELECT * FROM `category` WHERE cat_id in (". $cat_id .")");

                                    while($row=mysql_fetch_array($query_room))
                                    {
                                        $category_name=$row['cat_type'];
                                        $category_id=$row['cat_id'];
                                        echo '<input type="text" value='.$category_name.' readonly="readonly" name="cat_name">';

                                        echo '<input type="hidden" value='.$category_id.' readonly="readonly" name="cat_id">';  // hidden value for cat_id to fetch data from category
                                         $_SESSION['imagesession'] = "data:image/png;base64, base64_encode($room_image)";
                                    }
                                    ?>
                                    </td>
                                    <td><input type="text" value="<?php echo $room_number ;?>" name="room_number" required></td>
                                    <td><input type="text" value="<?php echo $room_desc ;?>" name="room_desc" required></td>
                                    <td><input type="image" src="data:image/png;base64,<?php echo base64_encode($room_image);?>" name="myimage" alt="No Photo" title="Click to View in Detail" height="50px" width="50px"/>

                                    <label>Upload Image</label>
                                        <input type="file" id="uploadImage" value="<?php echo base64_encode($room_image);?>" name="image" onChange="PreviewImage();" />
                                        <img id="uploadPreview" style="width: 200px; height: 300px; border-style:none" />

                                    </td>
                                    </tr>
                                </tbody>
         </table>
                         </div>
                        </div>
                        <button type="submit" class="btn btn-success" name="update_rooms">Update Rooms</button>
                        <button type="reset" class="btn btn-danger">Reset</button>
                         </form>

在数据库中获取数据并更新

            if(isset($_POST['update_rooms']))
             {
            $current_id = $_POST['room_id'];
            $room_desc = $_POST['room_desc'];
            $room_number = $_POST['room_number'];             
             $cat_id = $_POST['cat_id'];


            // $imageName = mysql_real_escape_string($_FILES["image"]["name"]);
            $imageData = mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"]));
            $imageType = mysql_real_escape_string($_FILES["image"]["type"]); 
            if(empty($imageData))
            {
                if($imageData === $existing_category_image)
                {
                echo '<span style="color:#E02F2F;text-align:center;font-size:30px;padding-left:190px;">Reselect the Image</span>';                  
                return;
                }
            }

            $sql = "UPDATE room SET cat_id='$cat_id', room_no='$room_number', room_desc='$room_desc', room_image='$imageData' WHERE room_id='$current_id'";
             //echo "Res".$sql;
            $result=mysql_query($sql);
            if($result)
            {
            echo '<span style="color:#E02F2F;text-align:center;font-size:30px;padding-left:190px;">Record Updated Successfully!</span>';
            //header("Location: success.php");
            } else
            {
             echo '<span style="color:#E02F2F;text-align:center;font-size:30px;padding-left:250px;">Error Found!.mysql_error()</span>';
            }
}

问题:

1. 编辑时所有字段都已成功更新,但图像无法正常工作我的意思是,如果用户添加新图像,则其在数据库中更新,但如果用户正在编辑现有图像,则空值将存储在数据库中。

如何解决这个问题。如何在输入类型文件中发送动态值。有什么帮助吗?

【问题讨论】:

  • $existing_category_image 是否保存了该行当前在数据库中的图像?如果是,则设置 $imageData = $existing_category_image 并照常保存。
  • 我无法将 $existing_category_image 值传递给数据库。
  • 是的,但我无法将 $existing_category_image 值从表单传递到 php 以及数据库

标签: php file post input-type-file


【解决方案1】:

你也可以用这个

从数据库中获取$existing_category_image,无需从表单中发布。

if($imageData == '' || $imageData == null){
$sql = "UPDATE room SET cat_id='$cat_id', room_no='$room_number', room_desc='$room_desc' WHERE room_id='$current_id'";
}else{
$sql = "UPDATE room SET cat_id='$cat_id', room_no='$room_number', room_desc='$room_desc', room_image='$imageData' WHERE room_id='$current_id'";
}

这样写:

    if($imageData == '' || $imageData == null){ 
            $imageData = $existing_category_image;
     }

之前

$sql = "UPDATE room SET cat_id='$cat_id', room_no='$room_number', room_desc='$room_desc', room_image='$imageData' WHERE room_id='$current_id'";

【讨论】:

  • 感谢您的帮助。它在两种情况下工作 1.“如果用户添加了新图像”,2.“如果用户编辑了现有图像”,但是如果图像已经在数据库中并且用户没有更改任何内容,我的意思是他不想编辑图像然后他将提交编辑室然后我收到一个错误错误是“”.:file_get_contents(): 文件名不能为空”
猜你喜欢
  • 2013-05-18
  • 2014-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多