Home Backend Development PHP Tutorial 自选值插入数据库解决办法

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

Jun 13, 2016 pm 01:42 PM
class insert post quot

自选值插入数据库
科目:有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>";
      }
}
?>

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)

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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use classes and methods in Python How to use classes and methods in Python Apr 21, 2023 pm 02:28 PM

Concepts and instances of classes and methods Class (Class): used to describe a collection of objects with the same properties and methods. It defines the properties and methods common to every object in the collection. Objects are instances of classes. Method: Function defined in the class. Class construction method __init__(): The class has a special method (construction method) named init(), which is automatically called when the class is instantiated. Instance variables: In the declaration of a class, attributes are represented by variables. Such variables are called instance variables. An instance variable is a variable modified with self. Instantiation: Create an instance of a class, a specific object of the class. Inheritance: that is, a derived class (derivedclass) inherits the base class (baseclass)

How to use python requests post How to use python requests post Apr 29, 2023 pm 04:52 PM

Python simulates the browser sending post requests importrequests format request.postrequest.post(url,data,json,kwargs)#post request format request.get(url,params,kwargs)#Compared with get request, sending post request parameters are divided into forms ( x-www-form-urlencoded) json (application/json) data parameter supports dictionary format and string format. The dictionary format uses the json.dumps() method to convert the data into a legal json format string. This method requires

What does class mean in python? What does class mean in python? May 21, 2019 pm 05:10 PM

Class is a keyword in Python, used to define a class. The method of defining a class: add a space after class and then add the class name; class name rules: capitalize the first letter. If there are multiple words, use camel case naming, such as [class Dog()].

What is the difference between insert ignore, insert and replace in mysql What is the difference between insert ignore, insert and replace in mysql May 29, 2023 pm 04:40 PM

The difference between insertignore, insert and replace instructions already exist or not. Example of insert error. Insertintonames(name,age)values("Xiao Ming", 23); insertignore ignores insertignoreintonames(name, age)values("Xiao Ming", 24); replace Replace and insert replaceintonames(name,age)values("Xiao Ming", 25); table requirements: PrimaryKey, or unique index result: the table id will be automatically incremented. Test code creates table

How does java initiate an http request and call the post and get interfaces? How does java initiate an http request and call the post and get interfaces? May 16, 2023 pm 07:53 PM

1. Java calls post interface 1. Use URLConnection or HttpURLConnection that comes with java. There is no need to download other jar packages. Call URLConnection. If the interface response code is modified by the server, the return message cannot be received. It can only be received when the response code is correct. to return publicstaticStringsendPost(Stringurl,Stringparam){OutputStreamWriterout=null;BufferedReaderin=null;StringBuilderresult=newSt

Replace the class name of an element using jQuery Replace the class name of an element using jQuery Feb 24, 2024 pm 11:03 PM

jQuery is a classic JavaScript library that is widely used in web development. It simplifies operations such as handling events, manipulating DOM elements, and performing animations on web pages. When using jQuery, you often encounter situations where you need to replace the class name of an element. This article will introduce some practical methods and specific code examples. 1. Use the removeClass() and addClass() methods jQuery provides the removeClass() method for deletion

A brief analysis of the POST method in PHP with parameters to jump to the page A brief analysis of the POST method in PHP with parameters to jump to the page Mar 23, 2023 am 09:15 AM

For PHP developers, using POST to jump to pages with parameters is a basic skill. POST is a method of sending data in HTTP. It can submit data to the server through HTTP requests. The jump page processes and jumps the page on the server side. In actual development, we often need to use POST with parameters to jump to pages to achieve certain functional purposes.

Detailed explanation of PHP Class usage: Make your code clearer and easier to read Detailed explanation of PHP Class usage: Make your code clearer and easier to read Mar 10, 2024 pm 12:03 PM

When writing PHP code, using classes is a very common practice. By using classes, we can encapsulate related functions and data in a single unit, making the code clearer, easier to read, and easier to maintain. This article will introduce the usage of PHPClass in detail and provide specific code examples to help readers better understand how to apply classes to optimize code in actual projects. 1. Create and use classes In PHP, you can use the keyword class to define a class and define properties and methods in the class.

See all articles