Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
<?php
   $driver = new mysqli_driver();
   $driver -> report_mode = MYSQLI_REPORT_OFF;
   
   if(isset($_SESSION['log']))
   {
      header('Location: welcome.php');
      exit();
   }
   else
   {
      if($_SERVER["REQUEST_METHOD"] == "POST")
      {
         function validData($x)
         {
            $x = trim($x);
            $x = stripslashes($x);
            $x = htmlspecialchars($x);
            return $x;
         }
         
         $server = "localhost";
         $user = "root";
         $pass = "";
         $db = "codescracker";
      
         $conn = @new mysqli($server, $user, $pass, $db);
      
         if($conn->connect_errno)
         {
            echo "Database connection failed!<BR>";
            echo "Reason: ", $conn->connect_error;
            exit();
         }
      
         $fname = $lname = $uname = $email = $pass = "";
         $unameE = $emailE = $passE = "";
         
         $fname = validData($_POST["firstname"]);
         $lname = validData($_POST["lastname"]);
         $uname = validData($_POST["username"]);
         $email = validData($_POST["email"]);
         $pass = validData($_POST["password"]);
         
         if(empty($uname))
            $unameE = "Username field was empty!<BR>";
         if(empty($email))
            $emailE = "Email Id field was empty!<BR>";
         if(empty($pass))
            $passE = "Password field was empty!<BR>";
         if(strlen($uname)<6)
            $unameE .= "Username must be of 6 or more characters!<BR>";
         if(strlen($pass)<6)
            $passE .= "Password must be of 6 or more characters!<BR>";
         if(!filter_var($email, FILTER_VALIDATE_EMAIL))
            $emailE .= "Enter a valid Email ID!<BR>";
      
         if(!empty($unameE) || !empty($emailE) || !empty($passE))
            $err = "Try again";
         else
         {
            $sql = "INSERT INTO `users`(`FirstName`, `LastName`, `Username`, `Email`, `Password`)
               VALUES (?, ?, ?, ?, ?)";
            
            $stmt = $conn->prepare($sql);
            $stmt->bind_param("sssss", $fname, $lname, $uname, $email, $pass);
      
            if($stmt->execute())
            {
               $_SESSION['log'] = $uname;
               header('Location: welcome.php');
               exit();
            }
            else
               $execE = "Something went wrong<BR>Please try again!";
         }
         $conn->close();
      }
   }
?>
<HTML>
<HEAD>
<STYLE>
   .form{width: 400px; margin: auto; padding: 12px; border-left: 2px solid #ccc; border-radius: 18px;}
   h2{color: purple; text-align: center;}
   input{padding: 12px; width: 100%; margin-bottom: 12px; border: 0px; border-radius: 6px;
      background-color: #ccc;}
   button{margin: 20px 0px; width: 100%; background-color: #008080; color: white; padding: 12px;
      font-size: 1rem; border-radius: 6px;}
   p{text-align: center;}
   button:hover{cursor: pointer;}
   .red{color: red;}
</STYLE>
</HEAD>
<BODY>

<DIV class="form">
   <H2>User Registration Form</H2>
   <FORM name="register" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
      <LABEL>First Name</LABEL><BR>
      <INPUT type="text" name="firstname" placeholder="First Name"><BR>
      <LABEL>Last Name</LABEL><BR>
      <input type="text" name="lastname" placeholder="Last Name"><BR>
      <LABEL>Username
      <?php
         if(!empty($unameE))
            echo "<SPAN class=\"red\">*</SPAN>";
         else
            echo "*";
      ?>
      </LABEL><BR>
      <INPUT type="text" name="username" placeholder="Create Username" required><BR>
      <LABEL>Email
      <?php
         if(!empty($emailE))
            echo "<SPAN class=\"red\">*</SPAN>";
         else
            echo "*";
      ?>
      </LABEL><BR>
      <INPUT type="text" name="email" placeholder="Email ID" required><BR>
      <LABEL>Password
      <?php
         if(!empty($passE))
            echo "<SPAN class=\"red\">*</SPAN>";
         else
            echo "*";
      ?>
      </LABEL><BR>
      <INPUT type="text" name="password" placeholder="Create Password" required><BR>
      <BUTTON type="submit">Register</BUTTON>
   </FORM>
   <?php
   if(isset($err))
   {
      echo "<DIV class=\"red\">";
      if(!empty($unameE))
         echo $unameE;
      if(!empty($emailE))
         echo $emailE;
      if(!empty($passE))
         echo $passE;
      echo "</DIV>";
   }
   elseif(isset($execE))
      echo $execE;
   else
   {
      echo "<P>Direction:  Username and Password must be of 6 or more characters.<BR>";
      echo "Star (*) Fields must not be empty.<BR>";
      echo "Special characters are not allowed.</P>";
   }
   ?>
   <P>Already registered ? <a href="login.php">Login</a></P>
</DIV>

</BODY>
</HTML>
The output produced by above PHP MySQLi user registration form, is shown in the snapshot given below:


What I have tried:

nothing was tried by me as per now
Posted
Updated 8-Jun-23 2:30am

Look at the error message, it gives you a lot of information. It tells you the file it occurs in: login.php, it tells you the line it happened on:66, and it tells you what it found was wrong: call to a member function bind_param() on bool

So go to line 66:
PHP
$stmt->bind_param("sssss", $fname, $lname, $uname, $email, $pass);
And it calls bind_param on $stmt HOw can that be wrong? Look at the lines above and see where $stmt was loaded with a value:
PHP
$stmt = $conn->prepare($sql);
$stmt->bind_param("sssss", $fname, $lname, $uname, $email, $pass);

if($stmt->execute())
Then look at the documentation for the prepare method and look at its return code: PHP: mysqli::prepare - Manual[^]
Quote:
mysqli_prepare() returns a statement object or false if an error occurred.
Ah! The error said a bool was involved, so prepare is returning false.

Which means that it failed for some reason.
The SQL looks pretty reasonable:
SQL
INSERT INTO `users`(`FirstName`, `LastName`, `Username`, `Email`, `Password`) VALUES (?, ?, ?, ?, ?)
So it's possible it's the connection, which we can't test as we have no access to your system.

So start there and look for what is wrong!

You should expect to get errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get an error!

And ... you need to fix something much deeper than that: Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]

And remember: if this is web based and you have any European Union users then GDPR applies and that means you need to handle passwords as sensitive data and store them in a safe and secure manner. Text is neither of those and the fines can be .... um ... outstanding. In December 2018 a German company received a relatively low fine of €20,000 for just that.
 
Share this answer
 
prepare('INSERT INTO "users" (username, email, firstname, lastname, password) VALUES (:username, :email, :firstname, :lastname, :password)');
$stmt->bindParam(':username', $username);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':firstname', $firstname);
$stmt->bindParam(':lastname', $lastname);
$stmt->bindParam(':password', $hashedPassword);

if ($stmt->execute()) {
echo 'Signup successful!';
} else {
echo 'Error: Unable to signup.';
}
}
}
?>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900