1Computer Sciences Department Princess Nourah bint Abdulrahman University.

29
1 Computer Sciences Department Princess Nourah bint Abdulrahman University

Transcript of 1Computer Sciences Department Princess Nourah bint Abdulrahman University.

Computer Sciences Department 1

Princess Nourah bint Abdulrahman University

PHPPart 3

Computer Sciences Department 4

Creating a new MySQL Database using………

Create & Check connection with Database that has been created.

Creating a new table Form Handling GET vs. POST methods Insert, select, delete, update data

Objectives

Computer Sciences Department 5

Creating a new MySQL Database

Computer Sciences Department 6

After WAMP server has started, you will see the WAMP icon as shown below in the notification area at the bottom-right side of the desktop.

If the icon is green as shown above, all services like Apache, MySQL etc. are running. If the icon is not green, left-click on it. You will see the WAMP administrator panel as shown below. Click on “Start All Services”.

Step 1

Computer Sciences Department 7

Click on “phpMyAdmin” under the “Tools” section. Log in with Username and Password for MySQL. Generally, the Username is "root" with no Password required for the Super user of MySQL

Step 2

Computer Sciences Department 8

Step 3 “create a new Database”

Note: Don't use a dot( “.”) in the database name

Computer Sciences Department 9

Step 4 “create a new Table”

1

2 3 4

Computer Sciences Department 10

Step 5 “Create new field”

Computer Sciences Department 11

Insert, delete, or modify ……etc. date

Step 6

Computer Sciences Department 12

Create a form “example”

Computer Sciences Department 13

When the user fills out the form …….and clicks the submit button, the form data is sent for processing to a PHP file named “?????????.php".

The form data is sent with the HTTP POST method.

PHP 5 Form Handling

Computer Sciences Department 14

<form action="insert.php" method="post"> ID: <input type="text" name="id"> Name: <input type="text" name="name"> Date: <input type="text" name="date"> <input type="submit"> </form>

Step 7 “create a form”

Computer Sciences Department 15

Both GET and POST create an array (e.g. array( key => value, key2 => value2, key3 => value3, ...)). This array holds key/value pairs, where keys are the names of the form controls and values are the input data from the user.

$_GET is an array of variables passed to the current script via the URL parameters.

$_POST is an array of variables passed to the current script via the HTTP POST method

GET vs. POST

Computer Sciences Department 16

Information sent from a form with the GET method is visible to everyone (all variable names and values are displayed in the URL). GET also has limits on the amount of information to send. The limitation is about 2000 characters. However, because the variables are displayed in the URL, it is possible to bookmark the page. This can be useful in some cases.

GET may be used for sending non-sensitive data. Note: GET should NEVER be used for sending

passwords or other sensitive information!

When to use GET?

Computer Sciences Department 17

$user = 'root'; $pass = ''; $db = 'db_test'; $hostname = 'localhost';

// Create connection $con=mysqli_connect($hostname,$user, $pass, $db);

// Check connection if (mysqli_connect_errno()) { echo "Failed to connect to DataBase: " . mysqli_connect_error(); }

Step 8 “Create & Check connection with Database”

Computer Sciences Department 18

Information sent from a form with the POST method is invisible to others (all names/values are embedded within the body of the HTTP request) and has no limits on the amount of information to send.

Moreover POST supports advanced functionality such as support for multi-part binary input while uploading files to server.

However, because the variables are not displayed in the URL, it is not possible to bookmark the page.

When to use POST?

Computer Sciences Department 19

Explanation

PHP 5 Form Validation

Computer Sciences Department 20

When some fields cannot be empty and must be filled out in the HTML form.

if (empty($_POST["name"])) {    $nameErr = "Name is required";  } else {    $name = test_input($_POST["name"]);  }

Example

PHP 5 Forms - Required Fields

Computer Sciences Department 21

Insert, select, delete, update data

Computer Sciences Department 22

Computer Sciences Department 23

Computer Sciences Department 24

The SELECT statement is used to select data from one or more tables

SELECT column_name(s) FROM table_name or we can use the * character to select ALL

columns from a table: SELECT * FROM table_name

PHP Select Data From MySQL

Computer Sciences Department 25

Computer Sciences Department 26

The DELETE statement is used to delete records from a table

DELETE FROM table_nameWHERE some_column = some_value

PHP Delete Data From MySQL

Computer Sciences Department 27

Computer Sciences Department 28

The UPDATE statement is used to update existing records in a table

UPDATE table_nameSET column1=value, column2=value2,...WHERE some_column=some_value

PHP Update Data in MySQL

Computer Sciences Department 29