如何在输入类型文件中发送动态值?

时间:2015-06-25 10:31:01

标签: php file post input-type-file

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

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

onchange="javascript:setTimeout('__doPostBack(\'ddlHolidayType\',\'\')', 0)"

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

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>

问题:

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

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

1 个答案:

答案 0 :(得分: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'";