当前位置:网站首页>PHP MySQL inserts data
PHP MySQL inserts data
2022-07-03 17:51:00 【Crooning ~ shallow singing】
Use MySQLi and PDO towards MySQL insert data
After creating the database and tables , We can add data to the table .
Here are some grammar rules :
- PHP in SQL Query statements must use quotation marks
- stay SQL String values in query statements must be quoted
- The value of a numeric value does not need quotation marks
- NULL Values don't need quotes
INSERT INTO Statements are usually used to direct MySQL Table add new record :
INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)
Learn more about SQL knowledge , Please check our SQL course .
We have created tables in the previous chapters "MyGuests", Table fields have : "id", "firstname", "lastname", "email" and "reg_date". Now? , Let's start filling the table with data .
Be careful : If the column is set AUTO_INCREMENT ( Such as "id" Column ) or TIMESTAMP ( Such as "reg_date" Column ),, We don't need to be in SQL Specify a value in the query statement ; MySQL Automatically adds a value to the column . |
The following example shows "MyGuests" A new record has been added to the table :
example (MySQLi - object-oriented )
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Detection connection if ($conn->connect_error) { die(" The connection fails : " . $conn->connect_error); } $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', '[email protected]ample.com')"; if ($conn->query($sql) === TRUE) { echo " New record inserted successfully "; } else { echo "Error: " . $sql . "<br>" . $conn->error; } $conn->close(); ?>
example (MySQLi - Process oriented )
<?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; // Create connection $conn = mysqli_connect($servername, $username, $password, $dbname); // Detection connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', '[email protected]')"; if (mysqli_query($conn, $sql)) { echo " New record inserted successfully "; } 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); // Set up PDO Wrong mode , Used to throw an exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('John', 'Doe', '[email protected]')"; // Use exec() , No results returned $conn->exec($sql); echo " New record inserted successfully "; } catch(PDOException $e) { echo $sql . "<br>" . $e->getMessage(); } $conn = null; ?>
边栏推荐
- c# . Net tool ecosystem
- Notes on problems -- watching videos on edge will make the screen green
- STM32 realizes 74HC595 control
- 互联网医院HIS管理平台源码,在线问诊,预约挂号 智慧医院小程序源码
- Kotlin's collaboration: Context
- First day of rhcsa study
- Leetcode540: a single element in an ordered array
- AcWing 3438. 数制转换
- 企业级自定义表单引擎解决方案(十一)--表单规则引擎1
- Is AI too slow to design pictures and draw illustrations? 3 sets of practical brushes to save you
猜你喜欢
Collection of the most beautiful graduation photos in the graduation season, collection of excellent graduation photos
Getting started with deops
聊聊支付流程的設計與實現邏輯
Global and Chinese pediatric palliative care drug market development research and investment planning recommendations report 2022-2028
Select 3 fcpx plug-ins. Come and see if you like them
Internet hospital his management platform source code, online consultation, appointment registration smart hospital applet source code
Research Report on market demand and investment planning for the development of China's office chair industry, 2022-2028
Research Report on investment trends and development planning of China's thermal insulation material industry, 2022-2028
Is AI too slow to design pictures and draw illustrations? 3 sets of practical brushes to save you
面试官:值为 nil 为什么不等于 nil ?
随机推荐
[combinatorics] recursive equation (summary of the solution process of recursive equation | homogeneous | double root | non-homogeneous | characteristic root is 1 | exponential form | the bottom is th
Managing multiple selections with MVVM - managing multiple selections with MVVM
UE4 official charging resources, with a total price of several thousand
Ssl/bio of OpenSSL_ get_ fd
1146_ SiCp learning notes_ exponentiation
QT learning diary 9 - dialog box
supervisor监控Gearman任务
AcWing 4489. Longest subsequence
Investigation on the operation prospect of the global and Chinese Anti enkephalinase market and analysis report on the investment strategy of the 14th five year plan 2022-2028
Is AI too slow to design pictures and draw illustrations? 3 sets of practical brushes to save you
How to deploy applications on kubernetes cluster
Distributed task distribution framework gearman
OpenSSL的SSL/BIO_get_fd
Assembly for unloading Loadfrom() loaded assembly - unloading the assembly loaded with assembly LoadFrom()
PS screen printing brush 131, many illustrators have followed suit
1164 Good in C
Analyse ArrayList 3: suppression d'éléments
[combinatorics] generating function (linear property | product property)
Classroom attendance system based on face recognition tkinter+openpyxl+face_ recognition
Where is the database account used when running SQL tasks in data warehouse tasks configured