Home >Backend Development >PHP Problem >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、插入数据的语句。
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}') ";
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; }
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!