Home > Blockchain >  PHP form validation for user registration
PHP form validation for user registration

Time:02-03

Add User

* required field

 <label for="firstname"> First Name</label>
  <input type="text" name="firstname">
  <span >* <?php echo $nameErr;?></span>
  <br><br>
  <label for="lastname">Last Name</label>
   <input type="text" name="lastname">
  <span >* <?php echo $nameErr;?></span>
  <br><br>
  <label for="email">Email</label>
  <input type="text" name="email">
  <span >* <?php echo $nameErr;?></span>
  <br><br>
  <label for="Password">Password</label>
  <input type="text" name="Password">
  <span >* <?php echo $nameErr;?></span>
  <br><br>
  <label for="usertype">User Type</label>
  <span >* <?php echo $genderErr;?></span>
  <div>
    <label for="Admin" >
  <input type="radio" name="user" id="Admin" value="Admin">Admin</label>
  <label for="Member" >
  <input type="radio" name="user"id="Member" value="Member">Member</label>
  <label for="Guest" >
  <input type="radio" name="user" id="Guest" value="Guest">Guest</label>
  </div>
  <br><br>
  <label for="Status">Status</label>
  <span >* <?php echo $genderErr;?></span>
  <div>
    <label for="Active" >
  <input type="radio" name="Status" id="Active" value="Active">Active</label>
  <label for="Inactive" >
  <input type="radio" name="Status" id="Inactive" value="Inactive">Inactive</label></div>
  <br><br>
  <input type="submit" name="submit" value="Submit">  
  <input type="reset" name="reset" value="Reset">  
</form>
  </div>
</div>
</div>
      </section>
    </body>
<?php
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
$Password=$_POST['Password'];
$user=$_POST['user'];
$Status=$_POST['Status'];


//database connect
$conn = mysqli_connect($localhost, $root,$"", $usert);
if($conn->connect_error)
{
    die('Connection Failed:'.$conn>'connect-error');
}
else{
    $stmt=$conn->prepare("insert into user(firstname,lastname,email,Password,user,Status)Values(?,?,?,?,?,?)");
    $stmt->bind_param('sssssii',$firstname,$lastname,$email,$Password,$user,$Status);
    $stmt->execute();
    echo"Success!";
    $stmt->close();
    $conn->close();}
?>

hi, trying to make a simple form with PHP validation for user registration. When I am giving the input value is not working. I can't understand the error .please help! I am new to PHP. Please let me know the correction.enter image description here

CodePudding user response:

I suggest you to go through these PHP documentations and get yourself familiar with how the database connection, queries etc work:

Looking at your quickly, here are few places to start with:

a) The things like database credentials - you should define these, those should not come through PHP forms

b) Make sure on the variable names etc. For example you've used $user at the top and later used $username.

Start here:

a) Just write this much of code in a file and make sure database is connected properly:

$mysqli = new mysqli("localhost","my_user","my_password","my_db"); // write actual database credentials and database name

if ($mysqli -> connect_errno) {
  echo "Failed to connect to MySQL: " . $mysqli -> connect_error;
  exit();
} else {
  echo "Database connected properly";
} 

b) Next, on form submission page just see first if you are getting the values from form submission:

var_dump($_POST)

Hope it is helpful.

CodePudding user response:

You haven't declared your DB credentials, it is just using random variables.

Replace the variables in your mysqli_connect() function with actual values/correct variables

CodePudding user response:

you should use PDO for connect your php code to databes.read this document for help: https://www.w3schools.com/php/php_mysql_connect.asp

  • Related