Register Page in PHP With MySQL

  • Post author:
  • Post category:PHP
  • Post comments:0 Comments

In this blog, we are going to create a user registration page in PHP with MySQL

<?php
//connect to the database
$mysqli = new mysqli("localhost", "my_user", "my_password", "my_db");

//check connection
if ($mysqli -> connect_errno) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
  exit();
}

//process the form
if(isset($_POST['submit'])) {
  //prepare statement
  if (!($stmt = $mysqli->prepare("INSERT INTO register (username, password, email) VALUES (?,?,?)"))) {
    echo "Prepare failed: (" . $mysqli->errno . ") " . $mysqli->error;
  }

  //bind parameters
  if (!$stmt->bind_param("sss", $_POST['username'], $_POST['password'], $_POST['email'])) {
    echo "Binding parameters failed: (" . $stmt->errno . ") " . $stmt->error;
  }
 
  //execute the statement
  if (!$stmt->execute()) {
    echo "Execute failed: (" . $stmt->errno . ") " . $stmt->error;
  }
}

//close the connection
$mysqli->close();
?>

<form action="register.php" method="post">
Username: <input type="text" name="username" /><br />
Password: <input type="password" name="password" /><br />
Email: <input type="text" name="email" /><br />
<input type="submit" name="submit" value="Register" />
</form>

<?php
//if the form is submitted
if(isset($_POST['submit'])) {
  echo "You have successfully registered!";
}
?>

Leave a Reply