gpt4 book ai didi

php - 如何从 MySQL 数组中检索选中的复选框?

转载 作者:行者123 更新时间:2023-11-29 08:47:52 26 4
gpt4 key购买 nike

我的mysql是这样设置的

MySQL

表格:图库

+-------------+------------+
| galleryname | coverphoto |
+-------------+------------+

表格:多画廊

+-------------+-----------+
| galleryname | galleries |
+-------------+-----------+

我使用 php 中的复选框数组将画廊放入 multigalleries.galleries 中,例如 gallery1,gallery2,gallery3。所有这些工作正常,我唯一不明白的是,当我拉出 gallery.galleryname 列表并使用 multigalleries.galleries 引用它时,我如何返回如果它存在于 multigalleries.galleries 中,则为选中状态?

任何帮助将不胜感激。

编辑//IM 使用的实际代码(如果有帮助的话)

$con = mysql_connect($host,$dbusername,$dbpassword);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($databasename, $con);
$query = "SELECT galleries.gallery, galleries.coverphoto, multigalleries.multigallery, multigalleries.galleries FROM galleries, multigalleries";
$result = mysql_query($query);

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo '<div class="gallery" style="background-image:none">';
echo '<input type="checkbox" name="galleriesm[]" value="'.$line[gallery].'">'. $line[gallery].'';
echo '</div>';

最佳答案

试试这个:

$a = mysql_query("
SELECT g.galleryname FROM gallery AS g, multigalleries AS m
WHERE g.galleryname = 'gallery1' AND FIND_IN_SET(g.galleryname, m.galleries)
");

$b = mysql_fetch_assoc($a);

echo '<input type="checkbox" name="whatever"';

if($b['galleryname'])
{
echo ' checked="checked"';
}

echo '>';

编辑:

$result = mysql_query("
SELECT g.galleryname, m.galleryname AS name FROM gallery AS g
LEFT JOIN (SELECT galleryname, galleries FROM multigalleries) AS m ON
FIND_IN_SET(g.galleryname, m.galleries)
");

while($row = mysql_fetch_assoc($result))
{
$checked = '';

if($row['name'])
{
$checked = ' checked="checked"';
}

echo '<input type="checkbox" name="galleriesm[]" id="'.$row['galleryname'].'" value="'.$row['galleryname'].'"'.$checked.'>
<label for="'.$row['galleryname'].'">'.$row['galleryname'].'</label>';
}

关于php - 如何从 MySQL 数组中检索选中的复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12098965/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com