search
HomeBackend DevelopmentPHP Tutorial 自选值插入数据库解决办法

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

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

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

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

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
<?php if(isset($_POST['submit'])){
    $subjects_sql = "insert into subjects (`subjects_01`,`subjects_02`,`subjects_03`,`subjects_04`,`subjects_05`,`subjects_06`,`subjects_07`,`subjects_08`,`subjects_09`,`subjects_10`,`subjects_11`,`subjects_12`,`subjects_13`,`subjects_14`,`subjects_15`,`subjects_16`,`subjects_17`,`subjects_18`,`subjects_19`,`subjects_20`) values('$_POST[subjects_01]','$_POST[subjects_02]','$_POST[subjects_03]','$_POST[subjects_04]','$_POST[subjects_05]','$_POST[subjects_06]','$_POST[subjects_07]','$_POST[subjects_08]','$_POST[subjects_09]','$_POST[subjects_10]','$_POST[subjects_11]','$_POST[subjects_12]','$_POST[subjects_13]','$_POST[subjects_14]','$_POST[subjects_15]','$_POST[subjects_16]','$_POST[subjects_17]','$_POST[subjects_18]','$_POST[subjects_19]','$_POST[subjects_20]')";
    $class_sql = "insert into class (`class_01`,`class_02`,`class_03`,`class_04`,`class_05`,`class_06`,`class_07`,`class_08`,`class_09`,`class_10`,`class_11`,`class_12`,`class_13`,`class_14`,`class_15`,`class_16`) values('$_POST[class_01]','$_POST[class_02]','$_POST[class_03]','$_POST[class_04]','$_POST[class_05]','$_POST[class_06]','$_POST[class_07]','$_POST[class_08]','$_POST[class_09]','$_POST[class_10]','$_POST[class_11]','$_POST[class_12]','$_POST[class_13]','$_POST[class_14]','$_POST[class_15]','$_POST[class_16]')";
    if(mysql_query($subjects_sql) and mysql_query($class_sql)){
          echo "<script>alert('提交成功');location.href='index.php';";
      }else{
          echo "<script>alert('提交失败');location.href='index.php';</script>";
      }
}
?>



------解决方案--------------------
不太明白你的需求 要是我 我会这样子做 建一个科目表
选择的话是保存科目表的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)

Statement
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment