gpt4 book ai didi

php - 使用 PHP 和 MySQL 获取多个选中的复选框

转载 作者:行者123 更新时间:2023-11-29 10:41:23 27 4
gpt4 key购买 nike

我需要从数据库中获取[a_users_has_interest]表中我拥有的所有兴趣的特定用户,并将其显示在Checkbox中,但我也想同时显示所有兴趣,以及用户已选择

类似这样的事情:

Ej

注意:我有下表,我附上了 SQL 和代码示例

a_interest:所有兴趣

a_users:所有用户

a_users_has_interest:所有有兴趣的用户

-- ----------------------------
-- Table structure for a_interest
-- ----------------------------
DROP TABLE IF EXISTS `a_interest`;
CREATE TABLE `a_interest` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of a_interest
-- ----------------------------
INSERT INTO `a_interest` VALUES ('1', 'Deportes');
INSERT INTO `a_interest` VALUES ('2', 'Salud');
INSERT INTO `a_interest` VALUES ('3', 'Belleza');
INSERT INTO `a_interest` VALUES ('4', 'Amor');
INSERT INTO `a_interest` VALUES ('5', 'Internet');

-- ----------------------------
-- Table structure for a_users
-- ----------------------------
DROP TABLE IF EXISTS `a_users`;
CREATE TABLE `a_users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of a_users
-- ----------------------------
INSERT INTO `a_users` VALUES ('1', 'User 1');
INSERT INTO `a_users` VALUES ('2', 'User 2');

-- ----------------------------
-- Table structure for a_users_has_interest
-- ----------------------------
DROP TABLE IF EXISTS `a_users_has_interest`;
CREATE TABLE `a_users_has_interest` (
`user_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`interest_id` int(10) unsigned NOT NULL,
PRIMARY KEY (`user_id`,`interest_id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of a_users_has_interest
-- ----------------------------
INSERT INTO `a_users_has_interest` VALUES ('1', '1');
INSERT INTO `a_users_has_interest` VALUES ('1', '2');
INSERT INTO `a_users_has_interest` VALUES ('1', '3');
INSERT INTO `a_users_has_interest` VALUES ('2', '1');
INSERT INTO `a_users_has_interest` VALUES ('2', '2');

我获取数据的示例代码:

SELECT *
FROM a_users_has_interest UHI
LEFT JOIN a_interest I ON I.id = UHI.interest_id
WHERE UHI.user_id = '2'

这向我显示了具有选项的用户,但我想显示所有的兴趣以及用户已经出现的兴趣及其兴趣 ID 和没有兴趣的兴趣,然后它们是空的。

最佳答案

您好,请使用以下代码

db_connect.php

$host       =   'localhost';
$user = 'root';
$password = 'root';
$database = 'skerp';


$connection_error = 'Sorry!!! We are experiencing problems with the database settings';

$link = mysqli_connect($host, $user, $password, $database) or DIE($connection_error);

我使用它的代码如下

<?php
require_once('db_connect.php');

$getAllInterests = mysqli_query($link, "SELECT * FROM a_interests");

$userInterests = mysqli_query($link, "SELECT * FROM a_user_has_interests WHERE user_id = 1";

?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
while($getAllInterest = mysqli_fetch_assoc($getAllInterests)){
while($userInterest = mysqli_fetch_assoc($userInterests)){
?>
<input
<?php echo ($userInterest['id'] == $getAllInterest['id']) ? 'checked="checked"' : '' ?>
type="checkbox" name="interests[]" value="<?php echo $getAllInterest['id'] ?>"> <?php echo $getAllInterest['name'] ?>
<?php
}
}
?>
</body>
</html>

第二种方法:

您可以使用 in_array 来检查以下代码,而不是使用循环中的循环

$getAllInterests = mysqli_query($link, "SELECT * FROM a_interests");

$userInterests = mysqli_query($link, "SELECT interest_id FROM a_user_has_interests WHERE user_id = 1";

<?php
while($getAllInterest = mysqli_fetch_assoc($getAllInterests)){
?>
<input
<?php echo (in_array($getAllInterest['id'], $userInterests)) ? 'checked="checked"' : '' ?>
type="checkbox" name="interests[]" value="<?php echo $getAllInterest['id'] ?>"> <?php echo $getAllInterest['name'] ?>
<?php
}
?>

关于php - 使用 PHP 和 MySQL 获取多个选中的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45423957/

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