Presentation is loading. Please wait.

Presentation is loading. Please wait.

Web Design and Development

Similar presentations


Presentation on theme: "Web Design and Development"— Presentation transcript:

1 Web Design and Development
Lecture # 09 DB Connection and Data Insertion Instructor: Rida Noor Department of Computer Science

2 Three Layered Architecture
* 07/16/96 Three Layered Architecture (c) 2007 National Academy for Software Development - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

3 Three Layered Architecture

4 Create Database and Table in WAMP or XAMPP
* 07/16/96 Create Database and Table in WAMP or XAMPP (c) 2007 National Academy for Software Development - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

5 Create a Database Start Apache and MySQL services on XAMPP.
* 07/16/96 Start Apache and MySQL services on XAMPP. Press Admin as pointed in image to go to phpMyAdmin. (c) 2007 National Academy for Software Development - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

6 Create a Database * 07/16/96 (c) 2007 National Academy for Software Development - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

7 Create a Database Enter Database name and press create button. *
07/16/96 Enter Database name and press create button. (c) 2007 National Academy for Software Development - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

8 Create a Database * 07/16/96 Enter Database name ‘testdb’ and press create button. (c) 2007 National Academy for Software Development - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

9 Create a Table * 07/16/96 Enter table name ‘tbl_registration’ , number of fields ‘6’ and press GO button to create table . (c) 2007 National Academy for Software Development - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

10 Create a Table * 07/16/96 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. (c) 2007 National Academy for Software Development - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

11 Create a Table * 07/16/96 Check the checkbox of A_I that is auto-increament for fld_reg_id press SAVE button. (c) 2007 National Academy for Software Development - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

12 Create a Table Here you can see the table you have created. * 07/16/96
(c) 2007 National Academy for Software Development - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

13 PHP Connect to MySQL * 07/16/96
(c) 2007 National Academy for Software Development - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

14 Why to establish a connection?
* 07/16/96 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. (c) 2007 National Academy for Software Development - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

15 How to establish a connection?
* 07/16/96 Create a new php file in Dreamweaver and save it as “connection.php”. Add following code in connection.php file. (c) 2007 National Academy for Software Development - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

16 Connection Elements $servername ="your_ servername";
* 07/16/96 $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. (c) 2007 National Academy for Software Development - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

17 Create Connection * 07/16/96 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). (c) 2007 National Academy for Software Development - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

18 Check Connection * 07/16/96 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. (c) 2007 National Academy for Software Development - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

19 Why to Close Connection ???
* 07/16/96 The connection will be closed automatically when the script ends. To close the connection before, use the following: // Close connection mysqli_close($conn); (c) 2007 National Academy for Software Development - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

20 PHP Insert Static Data Into MySQL
* 07/16/96 PHP Insert Static Data Into MySQL (c) 2007 National Academy for Software Development - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

21 PHP Insert Data Into MySQL
* 07/16/96 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,...) (c) 2007 National Academy for Software Development - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

22 PHP Insert Data Into MySQL
* 07/16/96 In the start of this lecture we created a empty table named "tbl_registration" with six columns: “fld_reg_id”, “fld_ ”, “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. (c) 2007 National Academy for Software Development - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

23 PHP Insert Data Into MySQL
* 07/16/96 Create a new PHP page in Dreamweaver and save its as “signUp.php”. Write following code in signUp.php page. (c) 2007 National Academy for Software Development - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

24 Include Connection File
* 07/16/96 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'; (c) 2007 National Academy for Software Development - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

25 Insert Statement * 07/16/96 Following SQL statement inserts data or record into table. // Insert Query $sql = "INSERT INTO tbl_registration (fld_ , fld_password, fld_firstname, fld_lastname) VALUES 'abc123', 'John', 'Doe')"; (c) 2007 National Academy for Software Development - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

26 Check Insertion * 07/16/96 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); (c) 2007 National Academy for Software Development - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

27 Close Connection * 07/16/96 The connection will be closed automatically when the script ends. To close the connection before, use the following: // Close connection mysqli_close($conn); (c) 2007 National Academy for Software Development - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

28 How to Insert Data Through HTML Form?
* 07/16/96 How to Insert Data Through HTML Form? (c) 2007 National Academy for Software Development - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

29 Html Design View of Form
* 07/16/96 Create an html page in Dreamweaver and save it as “signUp.html”. Design following layout: (c) 2007 National Academy for Software Development - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

30 Html Code View of Form * 07/16/96 Go to Code View of form and change names of textfileds, action and method attributes as follow: (c) 2007 National Academy for Software Development - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*

31 Insert Data Using Form Into MySQL
* 07/16/96 Create a new PHP page in Dreamweaver and save its as “signUp.php”. Write following code in signUp.php page. (c) 2007 National Academy for Software Development - All rights reserved. Unauthorized copying or re-distribution is strictly prohibited.*


Download ppt "Web Design and Development"

Similar presentations


Ads by Google