0% found this document useful (0 votes)
4 views

Php Form With Db Connection

This document provides PHP code for a simple registration form that connects to a database and includes an SQL query to create a 'users' table. The form collects user information such as first name, last name, email, and password, and handles the registration process with error handling. The document also includes styling for the form's appearance.

Uploaded by

nalinksbm
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 views

Php Form With Db Connection

This document provides PHP code for a simple registration form that connects to a database and includes an SQL query to create a 'users' table. The form collects user information such as first name, last name, email, and password, and handles the registration process with error handling. The document also includes styling for the form's appearance.

Uploaded by

nalinksbm
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

Simple PHP Form with Database

This document includes the PHP code for a simple registration form, its database connection, and

the SQL query to create the required table.

PHP and HTML Code:


1: <!DOCTYPE html>
2: <html lang="en">
3: <head>
4: <meta charset="UTF-8">
5: <meta name="viewport" content="width=device-width, initial-scale=1.0">
6: <title>Registration Form</title>
7: <style>
8: body {
9: display: flex;
10: justify-content: center;
11: align-items: center;
12: height: 100vh;
13: background-color: #f2f2f2;
14: font-family: Arial, sans-serif;
15: }
16: .form-container {
17: background: #fff;
18: padding: 30px;
19: border-radius: 10px;
20: box-shadow: 0 4px 8px rgba(0,0,0,0.1);
21: width: 300px;
22: }
23: .form-container h2 {
24: text-align: center;
25: margin-bottom: 20px;
26: }
27: .form-container input, .form-container button {
28: width: 100%;
29: padding: 10px;
30: margin: 10px 0;
31: border-radius: 5px;
32: }
33: .form-container button {
34: background-color: #4CAF50;
35: color: white;
36: border: none;
37: }
38: .form-container button:hover {
39: background-color: #45a049;
40: }
41: </style>
42: </head>
43: <body>
44: <?php
45: $servername = "localhost";
46: $username = "root";
47: $password = "";
48: $database = "your_database_name";
49: $conn = new mysqli($servername, $username, $password, $database);
50: if ($conn->connect_error) {
51: die("Connection failed: " . $conn->connect_error);
52: }
53: if ($_SERVER["REQUEST_METHOD"] == "POST") {
54: $first_name = $_POST['first_name'];
55: $last_name = $_POST['last_name'];
56: $email = $_POST['email'];
57: $password = password_hash($_POST['password'], PASSWORD_DEFAULT);
58: $sql = "INSERT INTO users (first_name, last_name, email, password) VALUES (?, ?, ?, ?)";
59: $stmt = $conn->prepare($sql);
60: $stmt->bind_param("ssss", $first_name, $last_name, $email, $password);
61: if ($stmt->execute()) {
62: echo "<p style='text-align:center; color:green;'>Registration successful!</p>";
63: } else {
64: echo "<p style='text-align:center; color:red;'>Error: " . $stmt->error . "</p>";
65: }
66: $stmt->close();
67: }
68: $conn->close();
69: ?>
70: <div class="form-container">
71: <h2>Register</h2>
72: <form action="" method="POST">
73: <input type="text" name="first_name" placeholder="First Name" required>
74: <input type="text" name="last_name" placeholder="Last Name" required>
75: <input type="email" name="email" placeholder="Email" required>
76: <input type="password" name="password" placeholder="Password" required>
77: <button type="submit">Submit</button>
78: </form>
79: </div>
80: </body>
81: </html>
82:

SQL Query:

CREATE TABLE users (


id INT AUTO_INCREMENT PRIMARY KEY,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL
);

You might also like