0% found this document useful (0 votes)
2 views11 pages

PHP Forms MySQL NewTheme ReadableCode

The document provides a concise guide on handling PHP forms and interacting with a MySQL database. It covers form handling, validation, connecting to MySQL, creating tables, and performing CRUD operations (Create, Read, Update, Delete) using PHP. Each section includes code snippets demonstrating the respective functionalities.

Uploaded by

capriconsoul
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views11 pages

PHP Forms MySQL NewTheme ReadableCode

The document provides a concise guide on handling PHP forms and interacting with a MySQL database. It covers form handling, validation, connecting to MySQL, creating tables, and performing CRUD operations (Create, Read, Update, Delete) using PHP. Each section includes code snippets demonstrating the respective functionalities.

Uploaded by

capriconsoul
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PHP Forms & MySQL Database

Short, Clear & To-the-Point (BS Level)


PHP Form Handling
Collect user input using GET or POST method.

<form method="post">
<input type="text" name="name">
</form>

<?php
echo $_POST["name"];
?>
PHP Form Validation
Check input before processing.

<?php
if (empty($_POST["name"])) {
echo "Name required";
}
?>
PHP Email Validation
Validate email using filter_var().

<?php
$email = $_POST["email"];

if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Invalid Email";
}
?>
MySQL Connect
Connect PHP with MySQL server.

<?php
$conn = new mysqli("localhost", "root", "", "test");

if ($conn->connect_error) {
die("Connection failed");
}
?>
MySQL Create Table
Create table inside database.

<?php
$conn->query(
"CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50)
)"
);
?>
MySQL Insert Data
Insert single record.

<?php
$conn->query(
"INSERT INTO users (name)
VALUES ('Ali')"
);
?>
MySQL Prepared Statement
Secure insert using prepared statement.

<?php
$stmt = $conn->prepare(
"INSERT INTO users (name) VALUES (?)"
);

$stmt->bind_param("s", $name);
$stmt->execute();
?>
MySQL Select Data
Retrieve data from table.

<?php
$result = $conn->query(
"SELECT * FROM users"
);

while ($row = $result->fetch_assoc()) {


echo $row["name"];
}
?>
MySQL Update Data
Update existing record.

<?php
$conn->query(
"UPDATE users
SET name = 'Khan'
WHERE id = 1"
);
?>
MySQL Delete Data
Delete record.

<?php
$conn->query(
"DELETE FROM users
WHERE id = 1"
);
?>

You might also like