自选值插入数据库解决办法

WBOY
Release: 2016-06-13 10:10:03
Original
615 people have browsed it

自选值插入数据库
科目:有20个选项,自选5个。
班级:只能选一个。

然后自选的这个5个科目和班级会被写进数据库。就是要实现这么一个简单的功能,我虽然做出来了,但是觉得我做的不太合适。

我现在把科目和班级分成了两个表。相等于有两次insert,两次insert的id是AUTO_INCREMENT。所以理论上他们所对应的id也应该是能对上号的。但是显然这样很不合理。

其次,这样写的话实际上insert科目表的时候是把这20个都写进去了,只不过已选中的就把值写进去了,没选择的写进去的是空。然后在输出的都是也是把这20个全部输出,只不过有的是空所以最后只会看到有5个值输出,但是实际上却输出了20个,只不过另外15个是空罢了。

PHP code
alert('提交成功');location.href='index.php';";      }else{          echo "";      }}?>
Copy after login


------解决方案--------------------
不太明白你的需求 要是我 我会这样子做 建一个科目表
选择的话是保存科目表的id 以字符串的形式 如 1,2,3 选择的班级也保存同一个表
显示选择科目时 select * from 科目表 where id in(1,2,3)

------解决方案--------------------
三个表: 
科目表:
id
name

班级:
id
name

选修表:
id 
科目ID (存科目id eg:1,2,3)
班级ID (存班级id)

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!