PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Copy Clear
Copy Format Clear
<?php // Create users table if it doesn't exist $pdo->exec("CREATE TABLE IF NOT EXISTS users ( id INT AUTO_INCREMENT PRIMARY KEY, username VARCHAR(50) UNIQUE NOT NULL, email VARCHAR(100) UNIQUE NOT NULL, password VARCHAR(255) NOT NULL, full_name VARCHAR(100), phone VARCHAR(20), address TEXT )"); // User registration if ($_SERVER['REQUEST_METHOD'] === 'POST') { $username = $_POST['username'] ?? ''; $email = $_POST['email'] ?? ''; $password = $_POST['password'] ?? ''; $full_name = $_POST['full_name'] ?? ''; $phone = $_POST['phone'] ?? ''; $address = $_POST['address'] ?? ''; if (empty($username) || empty($email) || empty($password)) { echo "Username, email, and password are required fields."; } else { $hashed_password = password_hash($password, PASSWORD_DEFAULT); $stmt = $pdo->prepare("INSERT INTO users (username, email, password, full_name, phone, address) VALUES (?, ?, ?, ?, ?, ?)"); $stmt->execute([$username, $email, $hashed_password, $full_name, $phone, $address]); echo "User registered successfully!"; } } // User search functionality if (isset($_GET['q'])) { $query = '%' . $_GET['q'] . '%'; $stmt = $pdo->prepare("SELECT username FROM users WHERE username LIKE ?"); $stmt->execute([$query]); $users = $stmt->fetchAll(PDO::FETCH_ASSOC); echo json_encode($users); exit; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>User Registration and Search</title> </head> <body> <h2>User Registration</h2> <form method="post"> <input type="text" name="username" placeholder="Username" required><br> <input type="email" name="email" placeholder="Email" required><br> <input type="password" name="password" placeholder="Password" required><br> <input type="text" name="full_name" placeholder="Full Name"><br> <input type="tel" name="phone" placeholder="Phone"><br> <textarea name="address" placeholder="Address"></textarea><br> <button type="submit">Register</button> </form> <h2>User Search</h2> <input type="text" id="search" placeholder="Search users" oninput="searchUsers(this.value)"> <div id="results"></div> <script> function searchUsers(query) { if (query.length < 2) return; fetch(`?q=${encodeURIComponent(query)}`) .then(response => response.json()) .then(data => { const results = document.getElementById('results'); results.innerHTML = data.map(user => `<div>${user.username}</div>`).join(''); }); } </script> </body> </html>
Show:  
Copy Clear