Home Database Mysql Tutorial mysql insert into 结合php的使用方法

mysql insert into 结合php的使用方法

Jun 01, 2016 am 09:58 AM
insert insert

ySQL INSERT INTO语句在实际应用中是经常使用到的语句,所以对其相关的内容还是多多掌握为好。

向数据库表插入数据
INSERT INTO 语句用于向数据库表添加新记录。

语法

<code class="sql">INSERT INTO table_name
VALUES (value1, value2,....)</code>
Copy after login

您还可以规定希望在其中插入数据的列:

<code class="sql">INSERT INTO table_name (column1, column2,...)
VALUES (value1, value2,....)</code>
Copy after login

注释:SQL 语句对大小写不敏感。INSERT INTO 与 insert into 相同。

1.insert语句一次可以插入多组值,每组值用一对圆括号括起来,用逗号分隔,如下:

<code class="sql">insert into `news`(title,body,time) values('title 1','body 1',now()),('title 2','body 2',now());</code>
Copy after login

2.Insert Select 语句,将查询的结果插入到新的表,顾名思义,它由一条insert语句和一条select语句组成

<code class="sql">insert into news_one(id,title,body,time) select id,title,body,time from news_two;</code>
Copy after login

要注意的是,这两个表的结构要完全相同,列名可以不同。

在php中使用方法

下面是 "insert.php" 页面的代码:

<code class="php"><?php $con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("my_db", $con);
$sql="INSERT INTO Persons (FirstName, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";
mysql_close($con)
?></code>
Copy after login

 

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)

Using the MINUS operator in SQL Using the MINUS operator in SQL Feb 18, 2024 pm 04:53 PM

Usage of MINUS in SQL and specific code examples In SQL, MINUS is an operator used to perform a difference operation between two result sets. It is used to delete the same rows from the first result set as in the second result set. The result set returned by the MINUS operator will contain rows that exist only in the first result set. The following uses specific code examples to demonstrate the usage of MINUS: Assume there are two tables - "table1" and "table2", their structures are as follows: Table name: table1 field

Can we insert null value in Java list? Can we insert null value in Java list? Aug 20, 2023 pm 07:01 PM

SolutionYes,Wecaninsertnullvaluestoalisteasilyusingitsadd()method.IncaseofListimplementationdoesnotsupportnullthenitwillthrowNullPointerException.Syntaxbooleanadd(Ee) Appends the specified element to the end of this list. Type parameter E − The runtime type of the element. Parameter e − element to be appended to this list

How to insert graphics and text of China map into wps document How to insert graphics and text of China map into wps document Mar 27, 2024 pm 02:01 PM

1. Open the wps software and enter the wps text operation interface. 2. Find the insert option in this interface. 3. Click the Insert option and find the Shape option in its editing area. 4. Click the shape option and find the recommended option in its sub-menu. 5. Find the China map option in the recommended options. 6. Click on the China map option and drag it with the left mouse button in the editing input area to get the China map we need.

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

Use java's StringBuilder.insert() function to insert a string at the specified position Use java's StringBuilder.insert() function to insert a string at the specified position Jul 24, 2023 pm 09:37 PM

Use java's StringBuilder.insert() function to insert a string at a specified position. StringBuilder is a class in Java used to handle variable strings. It provides a variety of methods to operate strings. The insert() function is used to insert strings at specified positions. One of the common methods of positionally inserting strings. In this article, we will introduce how to use the insert() function to insert a string at a specified position and give corresponding code examples. insert()

How to add, edit and delete table rows in jQuery? How to add, edit and delete table rows in jQuery? Sep 05, 2023 pm 09:49 PM

In today's era of web development, effective and efficient table management has become very important, especially when dealing with data-heavy web applications. The ability to dynamically add, edit, and delete rows from a table can significantly enhance the user experience and make applications more interactive. An effective way to achieve this is to leverage the power of jQuery. jQuery provides many features to help developers perform operations. Table rows A table row is a collection of interrelated data, represented by elements in HTML. It is used to group together cells (represented by elements) in a table. Each element is used to define a row in the table, and for multi-attribute tables, it usually contains one or more elements. Syntax$(selector).append(co

How to implement a statement to insert multiple rows of data in MySQL? How to implement a statement to insert multiple rows of data in MySQL? Nov 08, 2023 pm 09:54 PM

How to implement a statement to insert multiple rows of data in MySQL? In MySQL, sometimes we need to insert multiple rows of data into the table at one time. In this case, we can use the INSERTINTO statement to achieve this. The following will introduce how to use the INSERTINTO statement to insert multiple rows of data, and give specific code examples. Suppose we have a table named students, which contains id, name, and age fields. Now we want to insert multiple pieces of student information at once. We can follow the following steps to achieve this:

Use the PHP function 'array_unshift' to insert elements to the beginning of the array Use the PHP function 'array_unshift' to insert elements to the beginning of the array Jul 25, 2023 pm 03:39 PM

Insert an element to the beginning of an array using the PHP function "array_unshift" PHP is a widely used server-side scripting language for creating dynamic web pages. In PHP, an array is a very important data structure used to store and manipulate a set of data. Sometimes we need to insert an element at the beginning of the array, then we can use the PHP built-in function "array_unshift". What the "array_unshift" function does

See all articles