How to add data to data table in php

(*-*)浩
Release: 2023-02-23 16:10:01
Original
4442 people have browsed it

How to add data to data table in php

Connect to the database.

The prerequisite for operating the database is to connect to the database first. Before connecting to the database, we need to know the host name, user name, password of the database, and also know which database we will choose to insert data into. . Examples are as follows: (Recommended learning: PHP video tutorial)

$username="root";
$password="";
$servernmae="localhost";
$dbname="ceshi";
$connect=new mysqli($servernmae,$username,$password,$dbname);2、插入数据的语句。
Copy after login

The format of the statement to insert data:

insert into data table name (field Name 1, field name 2, .....) values ​​(value of field 1, value of field 2);

Examples are as follows:

$sql="insert into test1 (username,age,register_time) values ('liming',20,'{$time1}') ";
Copy after login

Note: The field name and field value must be the same One correspondence, no chaos!

Execute statement

The next step is to execute our insert statement.

$query=$connect->query($sql);
if($query){
echo "成功插入数据!";
}
else{
echo $connect->error;
}
Copy after login

Execute the insert statement and give corresponding prompts.

test.

Test locally. Before testing, you must first enable the local apache and mysql services, otherwise you will not be able to access them.

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

Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template