我正在构建一个简单的应用程序,其基本任务是批准从 MySQL 表中列出的内容。列表项很长,例如 15 到 20 项。我想在列表项的右侧放置一个复选框,并允许用户选中多个项目并单击提交按钮以批准所选项目。
我在表格上制作了一个基本标志列,用于设置 0 或 1 表示批准或不批准。在我现有的系统中,用户必须单击链接并转到特定页面并单击批准才能批准该项目。我想通过允许用户选中复选框并选择多个条目来一次批准这些项目来缩短流程时间。
这是我的表格,该项目是 YouTube 链接,因此管理员可以查看视频,如果效果好,他/她可以通过选中复选框来批准它。
这里是当前页面特定的 php 代码,用于批准该项目
<?php
$msg = "";
$idForm = $_GET['id'];
if( isset( $_POST['submit'] ) ){
$msg= approveVideo($idForm);
}
?>
approveVideo 函数:
function approveVideo($id) {
$message ="";
$query= "UPDATE locus_videos SET set_flag='1' WHERE id='{$id}'";
if(mysql_query($query)) {
$message ="Video Approved ";
}else {
die("failed: " . mysql_error());
}
return $message;
}
php 文件的表格格式:
<table width="100%" height="487" border="0" cellpadding="5" cellspacing="5">
"method="POST">
<table width="100%" border="0" cellspacing="5" cellpadding="5">
上传者姓名 视频链接
"> "target="_blank">
</form>
</td>
在我当前的版本中,当用户点击视频链接时,这会重定向到特定页面,并且只有他可以从那里批准视频(YouTube 链接)
最佳答案
有两种方法,具体取决于您是只想“批准”行,还是“批准”和“不批准”行。
首先:
<table>
<thead>
<tr>
<td> </td>
<td>Url</td>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" name="Approve[]" value="1" /></td>
<td>http://www.youtube.com/...</td>
</tr>
<tr>
<td><input type="checkbox" name="Approve[]" value="2" /></td>
<td>http://www.youtube.com/...</td>
</tr>
<tr>
<td><input type="checkbox" name="Approve[]" value="3" /></td>
<td>http://www.youtube.com/...</td>
</tr>
</tbody>
</table>
如您所见,有一个名为“Approve[]”的复选框,每行都有一个不同的值。该值是您的表的 ID 列。
在您的 PHP 中,您将使用以下内容:
function approveVideos($ids)
{
$message = '';
$query = 'update locus_videos set set_flag = 1 where id in (' . implode(',', $ids) . ')';
if(mysql_query($query))
{
$message ="Videos Approved ";
}
else
{
die("failed: " . mysql_error());
}
return $message;
}
$ids = $_POST['Approve'];
approveVideos($ids);
第二个:
<table>
<thead>
<tr>
<td> </td>
<td>Url</td>
</tr>
</thead>
<tbody>
<tr>
<td><input type="hidden" name="Approve[1]" value="0" /><input type="checkbox" name="Approve[1]" value="1" /></td>
<td>http://www.youtube.com/...</td>
</tr>
<tr>
<td><input type="hidden" name="Approve[2]" value="0" /><input type="checkbox" name="Approve[2]" value="1" /></td>
<td>http://www.youtube.com/...</td>
</tr>
<tr>
<td><input type="hidden" name="Approve[3]" value="0" /><input type="checkbox" name="Approve[3]" value="1" /></td>
<td>http://www.youtube.com/...</td>
</tr>
</tbody>
</table>
在 PHP 中,您将执行以下操作:
function setVideoFlag($id, $flag = 1)
{
$message = '';
$query = 'update locus_videos set set_flag = ' . $flag . ' where id = ' . $id;
if(mysql_query($query))
{
$message ="Video " . ($flag == 1 ? 'Approved' : 'Disapproved');
}
else
{
die("failed: " . mysql_error());
}
return $message;
}
foreach($_POST['Approve'] as $id => $flag)
{
setVideoFlag($id, $flag);
}
希望对您有所帮助?
加文
关于php - 如何使用复选框在 PHP 中进行多个查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10634186/