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>
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
GET vs. POST
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!
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.
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>
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>
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”
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";
?>
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";�?>
�
Close the Connection:
Example (MySQLi Object-Oriented)
$conn->close();
�
Example (MySQLi Procedural)
mysqli_close($conn);
�
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":
<?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);
?>
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);�?>
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);�?>
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 INSERT INTO statement is used to add new records to a MySQL table:
INSERT INTO table_name (column1, column2, column3,...)�VALUES (value1, value2, value3,...)
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.
<?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);�?>
��$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);�?>
Insert Multiple Records Into MySQL Using MySQLi and PDO
Multiple SQL statements must be executed with the mysqli_multi_query() function.
<?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');";�$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);�?>
����Select Data With MySQLi��
<?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 = "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);�?>
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
<?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 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);�?>
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
�
<?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);�?>