当前位置:网站首页>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; ?>
边栏推荐
- Market demand survey and marketing strategy analysis report of global and Chinese pet milk substitutes 2022-2028
- Leetcode 108 converts an ordered array into a binary search tree -- recursive method
- Play with fancy special effects. This AE super kit is for you
- ArrayList分析3 : 删除元素
- Managing multiple selections with MVVM - managing multiple selections with MVVM
- Type conversion, variable
- Brief introduction to the core functions of automatic penetration testing tool
- c# .net 工具生态
- IntelliJ 2021.3 short command line when running applications
- 基于人脸识别的课堂考勤系统 tkinter+openpyxl+face_recognition
猜你喜欢
Talk about the design and implementation logic of payment process
鸿蒙第三次培训
Hongmeng third training
QT学习日记9——对话框
MySQL grouping query
Leetcode 669 pruning binary search tree -- recursive method and iterative method
Introduction to SolidWorks gear design software tool geartrax
Deops入门
Baiwen.com 7 days Internet of things smart home learning experience punch in the next day
面试官:值为 nil 为什么不等于 nil ?
随机推荐
Comparison of kotlin collaboration + retro build network request schemes
ArrayList analysis 3: delete elements
Keepalived 设置不抢占资源
How to deploy applications on kubernetes cluster
Fedora 21 安装 LAMP 主机服务器
Embedded-c language-7
AcWing 271. 杨老师的照相排列【多维DP】
Analyse ArrayList 3: suppression d'éléments
面试官:值为 nil 为什么不等于 nil ?
c# .net 工具生态
QT learning diary 9 - dialog box
List的stream中Long对象与long判等问题记录
PS screen printing brush 131, many illustrators have followed suit
Research Report on investment trends and development planning of China's thermal insulation material industry, 2022-2028
Kotlin的协程:上下文
自动渗透测试工具核心功能简述
聊聊支付流程的设计与实现逻辑
Distributed task distribution framework gearman
国内如何购买Google Colab会员
Baiwen.com 7 days Internet of things smart home learning experience punch in the next day