how to send dynamic value in input type file?

1.5k views Asked by At

i have created one edit form that i am editing my room number and room description and room image etc . based on the category id.

first edit form will fetch the data from database . example code :

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>

Getting data and updating in database

            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>';
            }
}

PROBLEM:

1.while editing all fields are updating successfully but come to the image its not working i mean if user is adding new image then its updating in database but if user is editing existing image then null value is storing in database .

how to solve this problem . how to send dynamic value in input type file . any help ?

1

There are 1 answers

1
PHP Worm... On BEST ANSWER

you may also use this

Fetch this $existing_category_image from database no need to post it from form.

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'";
}

OR

Write this:

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

before

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