0% found this document useful (0 votes)
4 views2 pages

php

This document provides a guide for submitting a form using PHP and MySQLi, including steps for creating an HTML form, setting up a MySQL database and table, and writing PHP code to insert and display data. It outlines the necessary HTML input fields, database structure, and PHP scripts for handling form submissions and displaying user records. The guide ensures that user data is securely stored and can be retrieved for viewing.

Uploaded by

scot0718757621
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)
4 views2 pages

php

This document provides a guide for submitting a form using PHP and MySQLi, including steps for creating an HTML form, setting up a MySQL database and table, and writing PHP code to insert and display data. It outlines the necessary HTML input fields, database structure, and PHP scripts for handling form submissions and displaying user records. The guide ensures that user data is securely stored and can be retrieved for viewing.

Uploaded by

scot0718757621
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
You are on page 1/ 2

PHP + MySQLi Form Submission Guide

Step 1: HTML Form


<form action="insert.php" method="POST">
<input type="text" name="name" placeholder="Enter your name" required><br>
<input type="email" name="email" placeholder="Enter your email" required><br>
<input type="number" name="age" placeholder="Enter your age" required><br>
<input type="password" name="password" placeholder="Enter your password" required><br>
<label>
<input type="checkbox" name="agree" required> I agree to the terms
</label><br>
<button type="submit" name="submit">Submit</button>
</form>

Step 2: MySQL Table Setup


CREATE DATABASE test_db;
USE test_db;

CREATE TABLE users (


id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100),
age INT,
password VARCHAR(255),
agree TINYINT(1)
);

Step 3: PHP Code to Insert Data (insert.php)


<?php
$conn = new mysqli("localhost", "root", "", "test_db");
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }

if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$age = $_POST['age'];
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
$agree = isset($_POST['agree']) ? 1 : 0;

$stmt = $conn->prepare("INSERT INTO users (name, email, age, password, agree) VALUES
(?, ?, ?, ?, ?)");
$stmt->bind_param("ssisi", $name, $email, $age, $password, $agree);

if ($stmt->execute()) {
echo "Data saved successfully!";
} else {
echo "Error: " . $stmt->error;
}
PHP + MySQLi Form Submission Guide

$stmt->close();
}
$conn->close();
?>

Step 4: PHP Code to Display Data (view.php)


<?php
$conn = new mysqli("localhost", "root", "", "test_db");
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }

$sql = "SELECT * FROM users";


$result = $conn->query($sql);

echo "<h2>User Records</h2>";


echo "<table border='1' cellpadding='5'>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
<th>Age</th>
<th>Agreed?</th>
</tr>";

if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
$agreed = $row['agree'] ? "Yes" : "No";
echo "<tr>
<td>{$row['id']}</td>
<td>{$row['name']}</td>
<td>{$row['email']}</td>
<td>{$row['age']}</td>
<td>$agreed</td>
</tr>";
}
} else {
echo "<tr><td colspan='5'>No records found.</td></tr>";
}
echo "</table>";
$conn->close();
?>

You might also like