skip to Main Content

Add User

* required field

 <label for="firstname"> First Name</label>
  <input type="text" name="firstname">
  <span class="error">* <?php echo $nameErr;?></span>
  <br><br>
  <label for="lastname">Last Name</label>
   <input type="text" name="lastname">
  <span class="error">* <?php echo $nameErr;?></span>
  <br><br>
  <label for="email">Email</label>
  <input type="text" name="email">
  <span class="error">* <?php echo $nameErr;?></span>
  <br><br>
  <label for="Password">Password</label>
  <input type="text" name="Password">
  <span class="error">* <?php echo $nameErr;?></span>
  <br><br>
  <label for="usertype">User Type</label>
  <span class="error">* <?php echo $genderErr;?></span>
  <div>
    <label for="Admin" class="radio-inline">
  <input type="radio" name="user" id="Admin" value="Admin">Admin</label>
  <label for="Member" class="radio-inline">
  <input type="radio" name="user"id="Member" value="Member">Member</label>
  <label for="Guest" class="radio-inline">
  <input type="radio" name="user" id="Guest" value="Guest">Guest</label>
  </div>
  <br><br>
  <label for="Status">Status</label>
  <span class="error">* <?php echo $genderErr;?></span>
  <div>
    <label for="Active" class="radio-inline">
  <input type="radio" name="Status" id="Active" value="Active">Active</label>
  <label for="Inactive" class="radio-inline">
  <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

3

Answers


  1. 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

    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
  3. I suggest you to go through these PHP documentations and get yourself familiar with how the database connection, queries etc work:

    Looking at your code 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search