需要通过mysql结果检查html复选框

时间:2013-03-06 19:17:56

标签: php javascript html mysql

我需要通过mysql select result ...

检查一个复选框

这是我的mysql表

username | hobbies              
----------------------------
abc      | reading painting     

当我选择用户名'abc'并提交时,它会转到编辑页面..

在那个页面中我需要编辑爱好..

<html>
<body>
<input type="checkbox" name="hobbies[]" value="reading">Reading
<input type="checkbox" name="hobbies[]" value="painting">painting
<input type="checkbox" name="hobbies[]" value="gaming">gaming
</body>
<html>

当我选择爱好阅读和绘画的'abc'时,只有第一个&amp;必须检查第二个复选框...我怎么能用PHP,mysql ???请帮助.....

4 个答案:

答案 0 :(得分:1)

首先将数据库记录拆分为单独的数组条目

<?php
    $hobbies = explode(" ", $mysql_result['hobbies']);
?>

如果数组包含值,则检查每个值,并将复选框设置为选中

<input type="checkbox" name="hobbies[]" value="reading" <?php if(in_array("reading", $hobbies)) echo "checked=\"checked\""; ?>>Reading

答案 1 :(得分:0)

您必须使用以下HTML标记:

<input type="checkbox" name="hobbies[]" value="reading" checked="checked" />

答案 2 :(得分:0)

<input type="checkbox" name="hobbies[]" value="reading" <?php echo status;?>>Reading

然后让你的php根据你的SQL查询确定复选框的状态并将其存储在变量中

status

答案 3 :(得分:0)

考虑更改架构。每个字段都应该是原子的 - 只有一个值。

创建一个爱好表,更改用户表,添加一个表以启用用户和爱好之间的多对多关系。创建一个显示用户兴趣爱好的视图。

这意味着您将能够执行像。

这样的查询
select * from `hobby` ; -- means your hobby check box can now be created dynamically
select * from `username_hobby` where username_id = 1 ; -- means you already have the username hobbies as separate elements.

有关数据规范化的更多细节=&gt; https://www.google.com.au/search?q=atomic+data+normalization

sqlfiddle示例http://sqlfiddle.com/#!2/5cdef/1/0

以下建议架构。

DROP TABLE IF EXISTS `hobby`;

CREATE TABLE `hobby` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `hobby` varchar(31) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `hobby` (`id`, `hobby`)
VALUES
    (1,'reading'),
    (2,'painting'),
    (3,'gaming');

DROP TABLE IF EXISTS `username`;

CREATE TABLE `username` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `username` varchar(31) DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `username` (`id`, `username`)
VALUES
    (1,'abc');


DROP VIEW IF EXISTS `username_hobbies_view`;

CREATE TABLE `username_hobbies_view` (
   `username` VARCHAR(31) DEFAULT '',
   `hobby` VARCHAR(31) DEFAULT NULL,
   `username_id` INT(11) UNSIGNED NOT NULL,
   `hobby_id` INT(11) UNSIGNED NOT NULL
) ENGINE=MyISAM;

DROP TABLE IF EXISTS `username_hobby`;

CREATE TABLE `username_hobby` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `username_id` int(11) unsigned NOT NULL,
  `hobby_id` int(11) unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `username_relationship` (`username_id`),
  KEY `hobby_relationship` (`hobby_id`),
  CONSTRAINT `hobby_relationship` FOREIGN KEY (`hobby_id`) REFERENCES `hobby` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `username_relationship` FOREIGN KEY (`username_id`) REFERENCES `username` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `username_hobby` (`id`, `username_id`, `hobby_id`)
VALUES
    (1,1,1),
    (2,1,2);

DROP TABLE `username_hobbies_view`;

CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `username_hobbies_view`
AS SELECT
   `username`.`username` AS `username`,
   `hobby`.`hobby` AS `hobby`,
   `username_hobby`.`username_id` AS `username_id`,
   `username_hobby`.`hobby_id` AS `hobby_id`
FROM ((`username` join `username_hobby` on((`username`.`id` = `username_hobby`.`username_id`))) join `hobby` on((`hobby`.`id` = `username_hobby`.`hobby_id`)));