国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

PHP MySQL insert data

We have learned about creating databases and data tables before, so this section will tell you about adding data to the table:

The following are some syntax rules:

· SQL query statements in PHP Quotes must be used

· String values ??in SQL query statements must be quoted

· Numeric values ??do not require quotes

· NULL values ??do not require quotes


INSERT INTO statement is usually used to add new records to a MySQL table

Syntax

INSERT INTO table_name (column1, column2, column3,...)VALUES (value1, value2, value3,...)

table_name : The name of the table we want to insert data into.

column1, column2 : The fields in the table, such as id firstname

value1, value2: are the data we want to insert

Note

1. It is not necessary to specify the field name column..., but the order after the values ??should be consistent with the sorting of the table fields. insert into user(username,sex) values('Liu Qi',1);

2. Fields with default values ??do not need to be written, and they will be default values.

3. If there is a default value or a nullable field and you do not want to pass in a specific value, you can write null.

To learn more about SQL, check out our SQL tutorials.

Let us illustrate it with an example:


Examples

In the previous few In the chapter we have created the table "MyGuests" with the following fields: "id", "firstname", "lastname", "email" and "reg_date". Now, let's start filling the table with data.

Note: If the column is set to AUTO_INCREMENT (such as the "id" column) or TIMESTAMP (such as the "reg_date" column), we do not need to specify the value in the SQL query statement; MySQL will Automatically add a value to the column.

AUTO_INCREMENT means to add data by yourself

<?php
 header("Content-type:text/html;charset=utf-8");    //設(shè)置編碼
 $servername = "localhost";
 $username = "root";
 $password = "root";
 $dbname = "test";
 
 // 創(chuàng)建連接
 $conn = mysqli_connect($servername, $username, $password, $dbname);
 // 檢測(cè)連接
 if (!$conn) {
     die("Connection failed: " . mysqli_connect_error());
 }
 
 $sql = "INSERT INTO MyGuests (firstname, lastname, email)
 VALUES ('John', 'Doe', 'john@example.com')";
 
 if (mysqli_query($conn, $sql)) {
     echo "新記錄插入成功";
 } else {
     echo "Error: " . $sql . "<br>" . mysqli_error($conn);
 }
 
 mysqli_close($conn);
 ?>

Program running result:

New record insertion is successful

Let us open PHPadmin and see if there is any data we added:

111.png

You can see that we have inserted the data into our data table


Continuing Learning
||
<?php header("Content-type:text/html;charset=utf-8"); //設(shè)置編碼 $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "test"; // 創(chuàng)建連接 $conn = mysqli_connect($servername, $username, $password, $dbname); // 檢測(cè)連接 if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', 'john@example.com')"; if (mysqli_query($conn, $sql)) { echo "新記錄插入成功"; } else { echo "Error: " . $sql . "<br>" . mysqli_error($conn); } mysqli_close($conn); ?> 運(yùn)行結(jié)果
submitReset Code