* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download HTML Basics - HTML, Text, Images, Tables, Forms
Microsoft Access wikipedia , lookup
Oracle Database wikipedia , lookup
Entity–attribute–value model wikipedia , lookup
Concurrency control wikipedia , lookup
Microsoft SQL Server wikipedia , lookup
Ingres (database) wikipedia , lookup
Functional Database Model wikipedia , lookup
Extensible Storage Engine wikipedia , lookup
Open Database Connectivity wikipedia , lookup
Microsoft Jet Database Engine wikipedia , lookup
Versant Object Database wikipedia , lookup
ContactPoint wikipedia , lookup
Clusterpoint wikipedia , lookup
Internet Application Development
Lecture # 08
DB Connection and Data Insertion
Instructor: Rida Noor
Department of Software Engineering
Three Layered Architecture
Three Layered Architecture
3
Create Database and Table in WAMP or
XAMPP
Create a Database
 Start Apache and MySQL services on XAMPP.
 Press Admin as pointed in image to go to phpMyAdmin.
5
Create a Database
6
Create a Database
 Enter Database name and press create button.
7
Create a Database
 Enter Database name ‘testdb’ and press create button.
8
Create a Table
 Enter table name ‘tbl_registration’ , number of fields ‘6’ and
press GO button to create table .
9
Create a Table
 Enter following fields and their types. Enter length of each field
except for fld_date. Select CURRENT_TIMESTAMP value for
fld_date in Default column.
10
Create a Table
 Check the checkbox of A_I that is auto-increament for fld_reg_id
press SAVE button.
11
Create a Table
 Here you can see the table you have created.
12
PHP Connect to MySQL
Why to establish a connection?
 You should establish a connection to the MySQL database.
 This is an extremely important step because if your script
cannot connect to its database, your queries to the database will
fail.
14
How to establish a connection?
 Create a new php file in Dreamweaver and save it as
“connection.php”.
 Add following code in connection.php file.
15
Connection Elements
 $servername ="your_ servername";
 $username="your_username";
 $password="your_password";
 $database="your_database_name";
 You
should replace "your_servername“,
"your_username",
"your_password" and "your_database_name" with the your MySQL
servername, username, password and database that will be used by your
script.
 At this point you may be wondering if it is a security risk to keep your
password in the file.
 You don't need to worry because the PHP source code is processed by
the server before being sent to the browser. So the visitor will not see the
script's code in the page source.
16
Create Connection
 Next you should connect your PHP script to the database.
This can be done with the new mysqli PHP function:
 // Create connection
new mysqli($servername, $username, $password, $database);
 This line tells PHP to connect to the MySQL database server
at 'localhost' (localhost is the MySQL server which usually
runs on the same physical server as your script).
17
Check Connection
 After the connection is established you should check whether the connection has
been established or not.
 This can be completed through the following command:
 // Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
else
{
echo "Connected successfully";
}
 This line checks if connection has not been established it will show an error
otherwise it will show success message.
 ‘die’ provides debugging functionality.
18
Why to Close Connection ???
 The connection will be closed automatically when the script
ends.To close the connection before, use the following:
 // Close connection
mysqli_close($conn);
19
PHP Insert Static Data Into MySQL
PHP Insert Data Into MySQL
 After a database and a table have been created, and connection has
been established, we can start adding data in table.
 Here are some syntax rules to follow:
The SQL query must be quoted in PHP
String values inside the SQL query must be quoted
Numeric values must not be quoted
The word NULL must not be quoted
 The INSERT INTO statement is used to add new records to a
MySQL table:
 Syntax:
 INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)
21
PHP Insert Data Into MySQL
 In the start of this lecture we created a empty table named
"tbl_registration" with six columns: “fld_reg_id”, “fld_email”,
“fld_password”, “fld_firstname”, “fld_lastname” and
“fld_date”. Now, let us fill the table with data.
 Note: If a column is AUTO_INCREMENT (like the "
fld_reg_id " column) or TIMESTAMP (like the "fld_date"
column), it is no need to be specified in the SQL query;
MySQL will automatically add the value.
22
PHP Insert Data Into MySQL
 Create a new PHP page in Dreamweaver and save its as
“signUp.php”.
 Write following code in signUp.php page.
23
Include Connection File
 You can write connection code in each php page to connect
PHP to MySQL. But this is not a better approach.
 A better approach is to make one connection file and include
that connection file through a single statement as written
below in each page instead of writing entire code.
 // Include connection
include 'connection.php';
24
Insert Statement
 Following SQL statement inserts data or record into table.
 // Insert Query
$sql = "INSERT INTO tbl_registration (fld_email, fld_password, fld_firstname,
fld_lastname)
VALUES ('[email protected]', 'abc123', 'John', 'Doe')";
25
Check Insertion
 Following statement checks whether data has been added
successfully or not.
 //Check Insertion
if (mysqli_query($conn, $sql))
{
echo "New record created successfully";
}
else
{
echo "Error: " . mysqli_error($conn);
}
26
Close Connection
 The connection will be closed automatically when the script
ends.To close the connection before, use the following:
 // Close connection
mysqli_close($conn);
27
How to Insert Data Through HTML Form?
Html Design View of Form
 Create an html page in Dreamweaver and save it as
“signUp.html”. Design following layout:
29
Html Code View of Form
 Go to Code View of form and change names of textfileds,
action and method attributes as follow:
30
Insert Data Using Form Into MySQL
 Create a new PHP page in Dreamweaver and save its as
“signUp.php”.
 Write following code in signUp.php page.
31
					 
									 
									 
									 
									 
									 
									 
									 
									 
									 
									 
									 
									 
									 
									 
									 
									 
									 
									 
									 
									 
									 
									 
									 
									 
									 
									 
									 
									 
									 
									 
									 
                                             
                                             
                                             
                                             
                                             
                                             
                                             
                                             
                                             
                                             
                                            