如何在输入类型文件中发送动态值?
how to send dynamic value in input type file?
我已经创建了一个编辑表单,我正在编辑我的房间号、房间描述和房间图像等。基于类别 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.while 编辑所有字段都已成功更新但图像无法正常工作我的意思是如果用户正在添加新图像然后在数据库中更新但如果用户正在编辑现有图像然后空值存储在数据库 .
如何解决这个问题。如何在输入类型文件中发送动态值。有帮助吗?
你也可以用这个
从数据库中获取此 $existing_category_image
,无需从表单中 post。
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'";
我已经创建了一个编辑表单,我正在编辑我的房间号、房间描述和房间图像等。基于类别 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.while 编辑所有字段都已成功更新但图像无法正常工作我的意思是如果用户正在添加新图像然后在数据库中更新但如果用户正在编辑现有图像然后空值存储在数据库 .
如何解决这个问题。如何在输入类型文件中发送动态值。有帮助吗?
你也可以用这个
从数据库中获取此 $existing_category_image
,无需从表单中 post。
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'";