The following code is not inserting the input password in my database.It is inserted as 0 in the password field

<!DOCTYPE html>
<?php require_once("configure2.php"); ?>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Login and Register form</title>
    <link rel="stylesheet" href="bootstrap-4.0.0-dist/css/bootstrap.min.css">
    <link rel="stylesheet" href="style.css">
    <script src="https://kit.fontawesome.com/f63bb9f33d.js" crossorigin="anonymous"></script>
    <script src="https://code.jquery.com/jquery-3.7.0.js" integrity="sha256-JlqSTELeR4TLqP0OG9dxM7yDPqX1ox/HfgiSLBj8+kM=" crossorigin="anonymous"></script>
    <script>"scripts/jquery-3.7.1.slim.min"</script>
    <script>"bootstrap-4.0.0-dist/js/bootstrap.bundle.min.js"</script>
</head>
<body>
    <div class="container">
      <div class="row">
        <?php
        if(isset($_POST['signup']))
        {
            $username=$_POST['username'];
            $gmail=$_POST['gmail'];
            $password=$_POST['password'];
            if(strlen($username)<3)
            {
                $error[]='Please enter username using 3 characters atleast.';
            }
            if(!preg_match("/^^[^0-9][a-z0-9]+([_-]?[a-z0-9])*$/",$username))
            {
                $error[]='Invalid entry.Enter lowercase letters without any space and no number at the start';
            }
            if(strlen($password)<6)
            {
                $error[]='Password is not 6 characters long.';
            }
            $sql="select * from signup where (User_name='$username' or Gmail='$gmail');";
            $res=mysqli_query($con,$sql);
            if(mysqli_num_rows($res)>0)
            {
                $row=mysqli_fetch_assoc($res);
                if($username==$row['User_name'])
                {
                    $error[]='Username already exists.';
                }
                if($gmail==$row['Gmail'])
                {
                    $error[]='Gmail already exists';
                }
            }
            if(!isset($error))
            {
                $hashedPassword = password_hash($password, PASSWORD_DEFAULT);
                $stmt=$con->prepare("insert into signup(User_name,Gmail,Psswd) values(?,?,?)");
                if(!$stmt)
                   die('Query failed:('.$con->errno.')'.$con->error);
                $res=$stmt->bind_param('sss',$username,$gmail,$hashedpassword);
                if(!$res)
                   die('Binding failed:('.$stmt->errno.')'.$stmt->error);
                $exec=$stmt->execute();
                if(!$exec)
                   die('Query execution failed:('.$stmt->errno.')'.$stmt->error);
                if($stmt->affected_rows > 0)
                {
                    $done=3;
                }
                else 
                {
                    $error[]='Failed:Something went wrong';
                }
                $stmt->close();
            }
        }
        ?>
     </div>
     <div class="col-sm-3">  
        <?php
        if(isset($error))
        {
            foreach($error as $error)
            {
                echo '<div class="alert alert-danger p-1 d-flex justify-content-center">';
                echo '<p class="errmsg">&#x26A0;'.$error.'</p>';
                echo'</div>';
            }
        }
        ?>
     </div>
        <?php if(isset($done))
        {?>
        <div class="successmsg"><span style="font-size:100px;">&#9989;</span><br>You have registered successfully.<br><a href="signin.php" style="color:#fff;">Login here...</a></div>
        <?php } else {?>
        <div class="form-box">
          <h1 id="title">Sign Up</h1>
          <form  method="POST">
            <div class="input-group">
                <div class="input-field">
                   <i class="fa-solid fa-user"></i>
                   <input type="text" placeholder="User Name" id="uname" name="username" value="<?php if(isset($error)){ echo $_POST['username'];}?>" required>
                </div>
                <div class="input-field">
                    <i class="fa-solid fa-envelope"></i>
                    <input type="email" placeholder="Gmail" id="umail" name="gmail" value="<?php if(isset($error)){ echo $_POST['gmail'];}?>" required>
                   </div>
                   <div class="input-field">
                     <i class="fa-solid fa-lock"></i>
                     <input type="password" placeholder="Password" id="upsswd" name="password" value="<?php if(isset($error)){ echo $_POST['password'];}?>">
                    </div>
                <div class="btn-field">
                    <a href="signin.php">Already a user?</a>
                    <button type="submit" id="signupbtn" name="signup">Sign Up</button>
                </div> 
            </div> 
            </form>
        <?php }?>  
        </div>
    </div>
</body>
</html>

TIL that putting more than 1 caret at the start of a regex is harmless, but consumes all of them.

Well, lets do the basic error checking…

  1. Is $hashedpassword a string containing “0” when you look at it?
  2. Is your database Psswd field set to be something that can hold a string, and not a numeric field?

Unrelated 3) Why is your insert a prepared statement but the select isnt?

Psswd is set to varchar,is there any problem

Does insert statement (first one) need to be prepared?

What did you mean in the first question?

You have to change your column to type of varbinary

Password != password

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.