如果数据库中的图像行为空,我想显示视频,反之亦然。例如,在我的数据库中,我有tbl_library,其中包含图像,视频等列,主键是 library_id 。
我使用while循环查看表信息并提供删除和编辑按钮。删除工作正常,我在编辑时遇到了一些问题。如果列图像为空或空,我想使用<video>*video here*</video>
显示视频,反之亦然如果视频为空,当我单击编辑按钮时将显示图像。
<?php
$define_attachment=mysql_query("SELECT video FROM tbl_library_ads WHERE library_id='$get_id'");
$define_image = mysql_num_rows($define_attachment);
if ($define_image > 0){
echo'
<center>
<div class="form-group">
<video width="500" height="350" style="margin-top:-80px; margin-left:20px;" controls>
<source src="../'.$row['video'].'" type="video/mp4">
</video>
</div>
<div class="form-group">
<label>Update Advertisement Video</label>
<input type="file" name="video" value="" accept="video/*">
</div>
</center>';
} else {
echo'
<center>
<div class="form-group">
<img src="../'.$row['image'].'" width="230" height="220"/>
</div>
<div class="form-group">
<label>Update Advertisement Picture</label>
<input type="file" name="image" value="" accept="image/*">
</div>
</center>';
} ?>
答案 0 :(得分:0)
要检查列是否为空,您只需使用=== NULL
来测试列是否为空。如果=== NULL
的计算结果为true,则该列为空,您可以轻松使用if
语句显示相应的HTML标记和编辑表单。
<?php
// Sanitize (you really should be using PDO/prepared stmnts so you don't have to do this)
$get_id = mysql_real_escape_string($get_id);
// Select statement
$define_attachment = mysql_query("SELECT video, image FROM tbl_library_ads WHERE library_id='$get_id'");
while ($row = mysql_fetch_assoc($define_attachment)) {
if ($row['video'] !== NULL) { // If the video column is NOT empty, display the video and the corresponding form
echo '
<center>
<div class="form-group">
<video width="500" height="350" style="margin-top:-80px; margin-left:20px;" controls>
<source src="../'.$row['video'].'" type="video/mp4">
</video>
</div>
<div class="form-group">
<label>Update Advertisement Video</label>
<input type="file" name="video" value="" accept="video/*">
</div>
</center>';
} elseif ($row['image'] !== NULL) { // If the image column is NOT empty, display the image and the corresponding form
echo '
<center>
<div class="form-group">
<img src="../'.$row['image'].'" width="230" height="220"/>
</div>
<div class="form-group">
<label>Update Advertisement Picture</label>
<input type="file" name="image" value="" accept="image/*">
</div>
</center>';
}
}
?>