dtcuv8044 2012-03-04 03:38
浏览 88
已采纳

如何自动检查mysql数据库中的复选框

I have a problem to solve which im not sure if it can be solved in the way that i have in mind. It's about a form with checkboxes. The purpose of this form is to allow users to add or remove their favorite brands from the database by checking/unchecking the form checkboxes. The challege that i face now is that there may already be brands selected in the past from the user. And i would like the page when it loads to check in the database and automatically check the brands in the form which where found in the database. I checked to see if it was possible with jquery , but i got stuck. Here is my code:

/* == SQL CODE == */

CREATE TABLE IF NOT EXISTS `favoriteBrands` (
`pkFavoriteBrand` int(5) NOT NULL AUTO_INCREMENT,
`fkCompanyID` int(5) NOT NULL,
`Brands` varchar(30) NOT NULL,
PRIMARY KEY (`pkFavorietemerken`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=81 ;


INSERT INTO `favoriteBrands` (`pkFavoriteBrand`, `fkCompanyID`, `Brands`) VALUES
(80, 7, 'Replay'),
(79, 7, 'Pepe Jeans'),
(71, 12, 'Nike'),
(70, 12, 'Le Coq Sportif'),

 /* == SQL CODE == */

 /* == PHP CODE == */
 <?php

 $find_fav_brands = " SELECT Brands FROM favoriteBrands WHERE fkCompanyID=$company_id";

 $get_brand        = mysql_query($find_fav_brands) or die ("No brand found");

 while($row = mysql_fetch_assoc($get_brand )){

$show_brand = $row["Brands"];

    echo"<p>$show_brand</p>";     
 }?>

 <form name="form" method="post" action="ready.php" >                       

<ul>

<li><input type="checkbox" name="merk[]" value="Adidas"/>Adidas</li>
<li><input type="checkbox" name="merk[]" value="Airforce"/>Airforce</li>
<li><input type="checkbox" name="merk[]" value="Armani"/>Armani</li>
<li><input type="checkbox" name="merk[]" value="Asics"/>Asics</li>
 </ul>

</form>
/* == PHP CODE == */
  • 写回答

2条回答 默认 最新

  • ds342222222 2012-03-04 03:45
    关注
    <?php
    
    $find_fav_brands = " SELECT Brands FROM favoriteBrands WHERE fkCompanyID=$company_id";
    
    $get_brand        = mysql_query($find_fav_brands) or die ("No brand found");
    $brands = array();
    while($row = mysql_fetch_assoc($get_brand )){
        $brands[]=$row["Brands"];
        $show_brand = $row["Brands"];
        echo"<p>$show_brand</p>";     
    }
    ?>
    <form name="form" method="post" action="ready.php">     
    
    <ul>
    <li><input type="checkbox" name="merk[]" value="Adidas"<?php echo in_array("Adidas",$brands)?" checked="checked"":""; ?> />Adidas</li>
    <li><input type="checkbox" name="merk[]" value="Airforce"<?php echo in_array("Airforce",$brands)?" checked="checked"":""; ?> />Airforce</li>
    <li><input type="checkbox" name="merk[]" value="Armani"<?php echo in_array("Armani",$brands)?" checked="checked"":""; ?> />Armani</li>
    <li><input type="checkbox" name="merk[]" value="Asics"<?php echo in_array("Asics",$brands)?" checked="checked"":""; ?> />Asics</li>
    </ul>
    
    </form>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵