[php] sample registration and validation mysql

Viewer

copydownloadembedprintName: sample registration and validation mysql
  1. <?php
  2. // Database connection details
  3. $servername = "localhost";
  4. $username = "your_mysql_username";
  5. $password = "your_mysql_password";
  6. $dbname = "your_database_name";
  7.  
  8. // Create connection
  9. $conn = new mysqli($servername, $username, $password, $dbname);
  10.  
  11. // Check connection
  12. if ($conn->connect_error) {
  13.     die("Connection failed: " . $conn->connect_error);
  14. }
  15.  
  16. // SQL query to create the table
  17. $sql = "CREATE TABLE IF NOT EXISTS users (
  18.     id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
  19.     username VARCHAR(30) NOT NULL UNIQUE,
  20.     name VARCHAR(50) NOT NULL,
  21.     phone VARCHAR(20) NOT NULL,
  22.     age INT(3) NOT NULL,
  23.     email VARCHAR(50) NOT NULL UNIQUE,
  24.     password VARCHAR(255) NOT NULL
  25. )";
  26.  
  27. if ($conn->query($sql) === TRUE) {
  28.     echo "Table 'users' created or already exists.<br>";
  29. } else {
  30.     echo "Error creating table: " . $conn->error . "<br>";
  31. }
  32.  
  33. // Registration form submission handling
  34. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  35.     $username = $_POST["username"];
  36.     $name = $_POST["name"];
  37.     $phone = $_POST["phone"];
  38.     $age = $_POST["age"];
  39.     $email = $_POST["email"];
  40.     $password = $_POST["password"];
  41.     $confirmPassword = $_POST["confirmPassword"];
  42.  
  43.     // Validate input data
  44.     $errors = array();
  45.  
  46.     if (empty($username)) {
  47.         $errors[] = "Username is required.";
  48.     }
  49.  
  50.     if (empty($name)) {
  51.         $errors[] = "Name is required.";
  52.     }
  53.  
  54.     if (empty($phone)) {
  55.         $errors[] = "Phone number is required.";
  56.     }
  57.  
  58.     if (empty($age) || !is_numeric($age) || $age < 1 || $age > 120) {
  59.         $errors[] = "Invalid age.";
  60.     }
  61.  
  62.     if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
  63.         $errors[] = "Invalid email address.";
  64.     }
  65.  
  66.     if (empty($password)) {
  67.         $errors[] = "Password is required.";
  68.     }
  69.  
  70.     if ($password != $confirmPassword) {
  71.         $errors[] = "Passwords do not match.";
  72.     }
  73.  
  74.     // If no errors, insert data into the database
  75.     if (empty($errors)) {
  76.         $hashedPassword = password_hash($password, PASSWORD_DEFAULT);
  77.  
  78.         $sql = "INSERT INTO users (username, name, phone, age, email, password)
  79.                 VALUES ('$username', '$name', '$phone', $age, '$email', '$hashedPassword')";
  80.  
  81.         if ($conn->query($sql) === TRUE) {
  82.             echo "Registration successful!";
  83.         } else {
  84.             echo "Error: " . $sql . "<br>" . $conn->error;
  85.         }
  86.     } else {
  87.         // Display validation errors
  88.         echo "Registration failed:<br>";
  89.         foreach ($errors as $error) {
  90.             echo "- " . $error . "<br>";
  91.         }
  92.     }
  93. }
  94.  
  95. $conn->close();
  96. ?>
  97.  
  98. <!DOCTYPE html>
  99. <html>
  100. <head>
  101.     <title>Registration Form</title>
  102. </head>
  103. <body>
  104.     <h2>Registration Form</h2>
  105.     <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  106.         Username: <input type="text" name="username" required><br><br>
  107.         Name: <input type="text" name="name" required><br><br>
  108.         Phone: <input type="text" name="phone" required><br><br>
  109.         Age: <input type="number" name="age" required><br><br>
  110.         Email: <input type="email" name="email" required><br><br>
  111.         Password: <input type="password" name="password" required><br><br>
  112.         Confirm Password: <input type="password" name="confirmPassword" required><br><br>
  113.         <input type="submit" name="submit" value="Register">
  114.     </form>
  115. </body>
  116. </html>

Editor

You can edit this paste and save as new:


File Description
  • sample registration and validation mysql
  • Paste Code
  • 26 Apr-2024
  • 3.4 Kb
You can Share it: