将数据输入到php表单中,而不填充MySQL表

时间:2019-03-19 01:28:52

标签: php mysql

具有用于注册系统的PHP表单:

         <div class="col-md-6 login-right">
    <h2> Register Here </h2>
    <form action="registration.php" method="post">
    <div class="form-group">
        <label>Username</label> 
        <input type="text" name="user" class="form-control" required>
        </div>
    <div class="form-group">
        <label>Password</label> 
        <input type="password" name="password" class="form-control" required>
        </div>
        <button type="submit" class="btn btn-primary"> Register</button>
</form>
</div>

然后创建了一个registration.php:

    <?php   

session_start();

$con = mysqli_connect('localhost','root', 'test');

mysqli_select_db($con, 'userregistration');

$name = $_POST['user']; 
$pass = $_POST['password'];

$s = " select * from usertable where name = '$name'";

$result = mysqli_query($con, $s);

$num = mysqli_num_rows($result);

if($num == 1){
    echo " Username Already Taken";
}else{
    $reg = " insert into usertable(name , password) values ('$name' , $pass')";
    mysqli_query($con, $reg);
    echo" Registration Successful";

}
?>

还具有使用Database:userregistration»Table:usertable创建的MySQL数据库。而且MySQL不确定,对于这方面的新手还不是来自php的输入数据。将数据输入php表单后,它会请求registration.php页面,该页面可以成功运行,但不会使用输入的数据填充表格。

1 个答案:

答案 0 :(得分:0)

您应该先主动检查连接是否成功,然后再检查查询是否也成功执行,以便进一步调试。

  if ($conn->connect_error) {
      die("Connection failed: " . $conn->connect_error);
  } 


   if (mysqli_query($conn, $reg)) {
       echo "New record created successfully";
    } else {
       echo "Error: " . $reg . "" . mysqli_error($conn);
    }

然后您还应该关闭连接