MODULE 4
Mysql Database: connect, create database, table, insert, select, delete, update Forms and
Database using MySql, Cookies, Sessions, Form Validation, Php super global variables.
Mysql Database:
CONNECT:
<?php
$db_server = “localhost”;
$db_user = “root”;
$db_password = “ ”;
$dp_name = “test_db”;
$conn = “ ”;
$conn = mysqli_connect ($db_server, $db_user, $db_password, $db_name)
If ( $conn ){
echo “You are connected”;
}
else {
echo “Could not connect”;
}
?>
CREATE DATABASE:
$conn = mysqli_connect("localhost", "root", "");
$sql = "CREATE DATABASE test_db";
mysqli_query($conn, $sql);
CREATE TABLE:
<?php
$conn = mysqli_connect("localhost", "root", "", "test_db");
$sql = "CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
email VARCHAR(50)
)";
mysqli_query($conn, $sql);
?>
INSERT DATA:
<?php
$conn = mysqli_connect("localhost", "root", "", "test_db");
$sql = "INSERT INTO users (name, email) VALUES ('John', 'john@[Link]')";
mysqli_query($conn, $sql);
?>
Suraksha
SELECT:
<?php
$conn = mysqli_connect("localhost","root","","test_db");
$res = mysqli_query($conn,"SELECT name,email FROM users");
while($u = mysqli_fetch_assoc($res)) echo "{$u['name']} - {$u['email']}<br>";
?>
DELETE
<?php
$conn = mysqli_connect("localhost", "root", "", "test_db");
$sql = "DELETE FROM users WHERE id=1";
mysqli_query($conn, $sql);
?>
UPDATE
<?php
$conn = mysqli_connect("localhost", "root", "", "test_db");
$sql = "UPDATE users SET name='Jane' WHERE id=2";
mysqli_query($conn, $sql);
?>
PHP Superglobal Variables
PHP super global variables are built-in variables that are always available — anywhere in your
script (no need to declare them). They are used to retrieve and manipulate data from forms,
sessions, cookies, server settings, and other sources.
$_GET
<?php
echo “Name: ” . $_GET['username'];
?>
$_POST
<form method = “post”>
Suraksha
Name: <input type = “text” name=“name” >
<input type = “Submit” >
</form>
<?php
echo “Name: ” . $_POST[‘Name'];
?>
$_REQUEST
<?php
echo “Name: ” . $_REQUEST[‘Name’];
?>
$_SERVER
<?php
echo $_SERVER['SERVER_NAME'];
?>
$_FILES
echo $_FILES['my le']['name'];
$_ENV
echo $_ENV['USER'];
$GLOBALS
<?php
$ame = “John”;
function test() {
global $name;
echo $name;
}
test()
?>
Cookies 🍪
Small pieces of data stored on the client's
(user's) browser.
Used to remember information between
visits — like login status, preferences, cart
items.
Sent automatically with every request to
the server.
Example:
$_COOKIE
setcookie(“fav_food”, “pizza”, time() +
(86400 * 2), “/“);
setcookie(“fav_dessert”, “ice
cream”, time() + (86400 * 4), “/“);
if( isset ($_COOKIE [ “fav_food”] ) ) {
echo ”BUY SOME { $_COOKIE [“fav_food”]} !!!”
}
else{
Suraksha
fi
echo”I don’t know your favourite food”;
}
Sessions 📦
Data stored on the server linked to a session ID stored in the user's cookie.
More secure because sensitive data stays on the server, not on the user's device.
Used for temporary user data like login sessions, form progress, etc.
Ends when the user logs out or after a certain period of inactivity.
$_SESSION
<?php
session_start( );
?>
<?php
$_SESSION[“username”] = “BroCode”;
$_SESSION[“password”] = “pizza123”;
echo $_SESSION[“username”] . “<br>” ;
echo $_SESSION[“password”] . “<br>” ;
Form Validation means checking the data entered by the user before processing it.
✅ Make sure the elds are not empty.
✅ Check if the email entered is a real email format.
✅ Check if the age entered is a number, etc.
How it works:
•When you submit the form:
◦If name is empty, it shows "Name is required".
◦If email is empty, it shows "Email is required".
•If both are lled, no error is shown.
Suraksha
fi
fi
Suraksha