Home  >  Article  >  Backend Development  >  How to add a data code in php

How to add a data code in php

藏色散人
藏色散人Original
2021-07-13 10:17:053108browse

How to add a data code in php: 1. Use the "INSERT INTO" statement to add new records to the MySQL table; 2. Use the PDO method to insert data into MySQL.

How to add a data code in php

The operating environment of this article: Windows7 system, PHP7.1 version, DELL G3 computer

How to add a piece of data in php?

Insert data into MySQL using MySQLi and PDO

After creating the database and table, we can add data to the table.

The following are some syntax rules:

PHP 中 SQL 查询语句必须使用引号
在 SQL 查询语句中的字符串值必须加引号
数值的值不需要引号
NULL 值不需要引号

The INSERT INTO statement is usually used to add new records to the MySQL table:

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

We have created it in the previous chapters The table "MyGuests" is created. The table fields are: "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 value.

The following example adds a new record to the "MyGuests" table:

Instance (MySQLi - Object Oriented)

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
 
// 创建连接
$conn = new mysqli($servername, $username, $password, $dbname);
// 检测连接
if ($conn->connect_error) {
    die("连接失败: " . $conn->connect_error);
} 
 
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES (&#39;John&#39;, &#39;Doe&#39;, &#39;john@example.com&#39;)";
 
if ($conn->query($sql) === TRUE) {
    echo "新记录插入成功";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}
 
$conn->close();
?>

Instance (MySQLi - Procedure Oriented)

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
 
// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);
// 检测连接
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
 
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES (&#39;John&#39;, &#39;Doe&#39;, &#39;john@example.com&#39;)";
 
if (mysqli_query($conn, $sql)) {
    echo "新记录插入成功";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
 
mysqli_close($conn);
?>

Example (PDO)

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";
 
try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // 设置 PDO 错误模式,用于抛出异常
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "INSERT INTO MyGuests (firstname, lastname, email)
    VALUES (&#39;John&#39;, &#39;Doe&#39;, &#39;john@example.com&#39;)";
    // 使用 exec() ,没有结果返回 
    $conn->exec($sql);
    echo "新记录插入成功";
}
catch(PDOException $e)
{
    echo $sql . "<br>" . $e->getMessage();
}
 
$conn = null;
?>

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to add a data code in php. For more information, please follow other related articles on the PHP Chinese website!

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