MySQL insert into inserts a table as an array

Source: Internet
Author: User
Tags mysql insert

When using PHP + MySql in the project, you need to insert data, but the database table field is very long. If you use the regular insert into table (c) values ('dd, it will be a very long SQL statement, and if you are not careful, it will be wrong. Therefore, arrays are usually used for operations. Many frameworks have special functions, such as table ing of thinkphp.

$ Result = $ model-> Add ($ post) // $ post is the array corresponding to the table Field

However, the following is my summary of how native is implemented. Although it is cumbersome, it is absolutely feasible. For example:

$ Link = mysql_connect ('localhost', 'root', 'pwd'); mysql_select_db ('db', $ link ); $ data ['a'] = $ post ['a']; $ data ['B'] = $ post ['B']; $ data ['C'] = $ post ['C']; $ data ['C'] = $ post ['C']; $ data ['D'] = $ post ['D']; $ data ['E'] = $ post ['E']; $ data ['F'] = $ post ['F']; $ data ['G'] = $ post ['G']; // a, B, c, d, E, F, and g are the table fields foreach ($ data as $ k => $ v) {$ K1 [] = '''. $ K. '''; // use a field as an array; $ V1 [] = '"'. $ v. '"'; // use the inserted value as an array;} $ strv. = implode (',', $ V1); $ strk. = implode (",", $ k1); $ SQL = "insert into Tb ($ strk) values ($ strv)"; mysql_query ($ SQL );

At present, there is no better way to do this.

This is also done on the Internet, but I have not tested it yet.

Step 1: Create a table to store the values of the array variables, that is, use the test database that comes with MySQL.

Use test;

Creat table 'Neil '('id' int (20) primary key not null,

'Major' varchar (25) not null );

Step 2: Create a check box page to simulate an instance PHP array index.html

<Body>

<Form method = "Post" Action = "show. php">

ID: <input type = "text" name = "ID"> <br>

<Input type = "checkbox" name = "teach [0]" value = "online"> online <br>

<Input type = "checkbox" name = "teach [1]" value = "video"> video <br>

<Input type = "checkbox" name = "teach [2]" value = "face"> face-to-face <br>

<Input type = "Submit" value = "Submit">

</Form>

Step 3: Describe show. php

Index.html submits the variable to show. php through post for processing. Let's write the show. PHP code first.

<? PHP

$ Db_name = "test ";

$ Table_name = "Neil ";

$ Connection = @ mysql_connect ("localhost", "root", "root") or die (mysql_error ());

$ Db = @ mysql_select_db ($ db_name, $ connection) or die (mysql_error ());

/* Here we will process array variables */

$ Value = ''; // defines a variable. Value Initialization is empty.

Foreach ($ _ post ["Teach"] as $ key)

{$ Value. = $ key. ',';}/* transmits the array value to the intermediate variable key, and the key transfers the value to the variable value in sequence */

/* Insert statement */

$ SQL = "insert into Neil values ('$ _ post [ID]', '$ value ')";

$ Query = @ mysql_query ($ SQL, $ connection) or die (mysql_error ());

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.