1 of 32

PHP  Form Handling:

PHP - A Simple HTML Form

Example: client code in html

<html>

<body>

​

<form action="welcome.php" method="post">

​

Name: <input type="text" name="name"><br>

E-mail: <input type="text" name="email"><br>

<input type="submit">

​

</form>

</body>

</html>

​

​

2 of 32

  • When the user fills out the form above and clicks the submit button,

​

  • the form data is sent for processing to a PHP file named "welcome.php".

​

  • The form data is sent with the HTTP POST method/ GET method.

3 of 32

PHP file to receive the data from form

<html>

<body>

<$php

$name=$_POST[“name”];

$email=$_POST[“email”];

Echo “WELCOME: $name” ,”<br>”;

Echo “Your email address is:$email”;

?>

</body>

</html>

​

Output:

Welcome raj�Your email address is: kumar@gmail.com

​

​

​

4 of 32

GET vs. POST

​

  • 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.

​

  • Both GET and POST are treated as $_GET and $_POST.

​

  • These are super global, which means that they are always accessible, regardless of scope - and you can access them from any function, class or file without having to do anything special.�

5 of 32

  • $_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.

​

6 of 32

​

When to use GET?

​

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.

​

GET may be used for sending non-sensitive data.

​

Note: GET should NEVER be used for sending passwords or other sensitive information!

​

7 of 32

When to use POST?

​

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.

​

8 of 32

Hadling Text Areas?(html file)

<html>

<body>

<h2> Entering the data from text area</h2>

<form action=“textarea.php" method="post">

Enter the pizza toppings you want:<br>

<textarea name=“data“ cols=“50” rows=“5”>

1.

2.

3.

</textarea><br>

<input type=“submit” value=“Send”>

</form>

</body>

</html>

​

​

9 of 32

PHP file to read data from Textarea:

​

<!DOCTYPE html>�<html>�<body>�<h2> Reading the data from Text Area</h2>

You ordered a pizza with:<br> �<?php

$text=$_REQUEST[“data”];

Echo”str_replace(“\n”,”<br>”,$text);

?>��</body>�</html>

​

10 of 32

PHP Connect to MySQL:

​

PHP and later can work with a MySQL database using:

​

MySQLi extension (the "i" stands for improved)

​

​

“MySQLi will only work with MySQL databases”

​

​

​

11 of 32

Open a Connection to MySQL:

Example (MySQLi Object-Oriented)

<?php

$servername = "localhost";

$username = “root";

$password = “ ";

// Create connection

$conn = new mysqli($servername, $username, $password);

// Check connection

if ($conn->connect_error)

{

die("Connection failed: " . $conn->connect_error);

}

echo "Connected successfully";

?>

​

​

12 of 32

Example (MySQLi Procedural)

<?php�$servername = "localhost";�$username = ”root";�$password = “ ";�// Create connection�$conn = mysqli_connect($servername, $username, $password);�// Check connection�if (!$conn) 

{�    die("Connection failed: " . mysqli_connect_error());�}�echo "Connected successfully";�?>

�

13 of 32

Close the Connection:

​

Example (MySQLi Object-Oriented)

$conn->close();

�

Example (MySQLi Procedural)

mysqli_close($conn);

�

​

​

14 of 32

Create a MySQL Database Using MySQLi

​

The CREATE DATABASE statement is used to create a database in MySQL.

​

The following examples create a database named "myDB":

​

​

​

​

15 of 32

<?php

$servername = "localhost";

$username = "root";

$password = "";

​

// Create connection

$conn = mysqli_connect($servername, $username, $password);

// Check connection

if (!$conn)

{

die("Connection failed: " . mysqli_connect_error());

}

​

// Create database

$sql = "CREATE DATABASE myDB";

if (mysqli_query($conn, $sql))

{

echo "Database created successfully";

}

else

{

echo "Error creating database: " . mysqli_error($conn);

}

​

mysqli_close($conn);

?>

​

​

16 of 32

PHP Database Table creation

<?php�$servername = "localhost";�$username = "username";�$password = "password";�$dbname = "myDB";��// Create connection�$conn = mysqli_connect($servername, $username, $password, $dbname);�// Check connection�if (!$conn) {

die("Connection failed: " . mysqli_connect_error());�}��

// sql to create table�$sql = "CREATE TABLE MyGuests (�id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, �firstname VARCHAR(30) NOT NULL,�lastname VARCHAR(30) NOT NULL,�email VARCHAR(50),�reg_date TIMESTAMP�)";�if (mysqli_query($conn, $sql))

{�    echo "Table MyGuests created successfully";�} 

else 

{�    echo "Error creating table: " . mysqli_error($conn);�}��mysqli_close($conn);�?>

17 of 32

PHP Database Table creation(cont..)

// sql to create table�$sql = "CREATE TABLE MyGuests (�id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, �firstname VARCHAR(30) NOT NULL,�lastname VARCHAR(30) NOT NULL,�email VARCHAR(50),�reg_date TIMESTAMP�)";�if (mysqli_query($conn, $sql))

{�    echo "Table MyGuests created successfully";�} 

else 

{�    echo "Error creating table: " . mysqli_error($conn);�}��mysqli_close($conn);�?>

18 of 32

​

​

​

​

​

PHP Insert Data Into MySQL

After a database and a table have been created, we can start adding data in them.

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:

​

INSERT INTO table_name (column1, column2, column3,...)�VALUES (value1, value2, value3,...)

19 of 32

​

​

​

Note:  If a column is AUTO_INCREMENT (like the "id" column) or TIMESTAMP (like the "reg_date" column),

​

it is no need to be specified in the SQL query;

​

MySQL will automatically add the value.

20 of 32

​

​

​

<?php�$servername = "localhost";�$username = “root";�$password = “ ";�$dbname = "myDB";��// Create connection�$conn = mysqli_connect($servername, $username, $password, $dbname);�

// Check connection�if (!$conn) 

{�    die("Connection failed: " . mysqli_connect_error());�}��

��$sql = "INSERT INTO MyGuests (firstname, lastname, email)�VALUES ('John', 'Doe', 'john@example.com')";��if (mysqli_query($conn, $sql))

{�    echo "New record created successfully";�} else {�    echo "Error: " . $sql . "<br>" . mysqli_error($conn);�}��mysqli_close($conn);�?>

21 of 32

​

​

​

��$sql = "INSERT INTO MyGuests (firstname, lastname, email)�VALUES ('John', 'Doe', 'john@example.com')";��if (mysqli_query($conn, $sql))

{�    echo "New record created successfully";�} else {�    echo "Error: " . $sql . "<br>" . mysqli_error($conn);�}��mysqli_close($conn);�?>

22 of 32

​

​

​

​

Insert Multiple Records Into MySQL Using MySQLi and PDO

​

Multiple SQL statements must be executed with the mysqli_multi_query() function.

​

23 of 32

<?php�$servername = "localhost";�$username = “root";�$password = ” ";�$dbname = "myDB";�// Create connection�$conn = mysqli_connect($servername, $username, $password, $dbname);�// Check connection�if (!$conn) {�    die("Connection failed: " . mysqli_connect_error());�}

24 of 32

$sql = "INSERT INTO MyGuests (firstname, lastname, email)�VALUES ('John', 'Doe', 'john@example.com');";�$sql .= "INSERT INTO MyGuests (firstname, lastname, email)�VALUES ('Mary', 'Moe', 'mary@example.com');";�$sql .= "INSERT INTO MyGuests (firstname, lastname, email)�VALUES ('Julie', 'Dooley', 'julie@example.com')";�if (mysqli_multi_query($conn, $sql)) {�    echo "New records created successfully";�} else {�    echo "Error: " . $sql . "<br>" . mysqli_error($conn);�}�mysqli_close($conn);�?>

25 of 32

����Select Data With MySQLi��

  • The following example selects the id, firstname and lastname columns from the MyGuests table and displays it on the page:

26 of 32

<?php�$servername = "localhost";�$username = “root";�$password = ” ";�$dbname = "myDB";��// Create connection�$conn = mysqli_connect($servername, $username, $password, $dbname);�// Check connection�if (!$conn) {�    die("Connection failed: " . mysqli_connect_error());�}��

​

27 of 32

$sql = "SELECT id, firstname, lastname FROM MyGuests";�$result = mysqli_query($conn, $sql);�if (mysqli_num_rows($result) > 0)

{�    // output data of each row�    while($row = mysqli_fetch_assoc($result))

{�        echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";�    }�} 

else 

{�    echo "0 results";�}�mysqli_close($conn);�?>

28 of 32

Delete Data From a MySQL Table Using MySQLi and PDO

​

The DELETE statement is used to delete records from a table:

​

SQL Syntax:

​

DELETE FROM table_name�WHERE some_column = some_value

​

​

​

29 of 32

<?php�$servername = "localhost";�$username = “root";�$password = ” ";�$dbname = "myDB";�// Create connection�$conn = mysqli_connect($servername, $username, $password, $dbname);�// Check connection�if (!$conn) 

{�die("Connection failed: " . mysqli_connect_error());�}�

30 of 32

�// sql to delete a record�$sql = "DELETE FROM MyGuests WHERE id=3";��if (mysqli_query($conn, $sql))

{�    echo "Record deleted successfully";�} 

else 

{�    echo "Error deleting record: " . mysqli_error($conn);�}�mysqli_close($conn);�?>

31 of 32

Update Data In a MySQL Table Using MySQLi

​

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

​

SQL Syntax:

UPDATE table_name�SET column1=value, column2=value2,...�WHERE some_column=some_value 

​

�

​

​

​

​

32 of 32

<?php�$servername = "localhost";�$username = "username";�$password = "password";�$dbname = "myDB";��// Create connection�$conn = mysqli_connect($servername, $username, $password, $dbname);�// Check connection�if (!$conn) {�    die("Connection failed: " . mysqli_connect_error());�}� // Update data�$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";��if (mysqli_query($conn, $sql)) {�    echo "Record updated successfully";�} else {�    echo "Error updating record: " . mysqli_error($conn);�}��mysqli_close($conn);�?>