PHP-MYSQL连接问题

问题描述 投票:0回答:1

我正在使用XXAMP,我正在尝试向数据库中添加新用户。但是在Register.php中完成表单后,不会将用户添加到数据库(phpmyadmin)中。我没有产生任何错误消息,因此很难进行故障排除。

目标是将新用户添加到localhost:8080的phpmyadmin数据库中。谢谢。

Register.php

    <!-- Modal -->
<div class="modal fade" id="addadminprofile" tabindex="-1" role="dialog" aria-labelledby="addadminprofile" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="addadminprofile">Add Admin Data</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <form action="code.php" method="POST">
      <div class="modal-body">


       <div class="form-group">
        <label>Username</label>
        <input type="text" name="username" class="form-control" placeholder="Enter Username">
      </div>
      <div class="form-group">
        <label>Email</label>
        <input type="text" name="email" class="form-control" placeholder="Enter Email">
      </div>
      <div class="form-group">
        <label>Password</label>
        <input type="text" name="password" class="form-control" placeholder="Enter Password">
      </div>
      <div class="form-group">
        <label>Confirm Password</label>
        <input type="text" name="confirmpassword" class="form-control" placeholder="Confirm Password">
      </div>

  </div>


      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="submit" name="registerbtn" class="btn btn-primary">Save</button>
      </div>
  </form>
    </div>
  </div>
</div>

Code.php

<?php
$connection = mysqli_connect("localhost","root","","adminpanel");

if(isset($_POST['registerbtn']))
{
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $cpassword = $_POST['confirmpassword'];

    if($password === $cpassword)
    {
        $query = "INSERT INTO register (username,email,password) VALUES ('$username','$email','$password')";
        $query_run = mysqli_query($connection, $query);

        if(query_run)
        {
            //echo "Saved";
            $_SESSION['success'] = "Admin Profile Added";
            header('Location: register.php');
        }
        else
        {
            $_SESSION['status'] = "Admin Profile NOT Added";
            header('Location: register.php');
        } 
    }
    else
    {
        $_SESSION['status'] = "Password and Confirm Password Does Not Match";
        header('Location: register.php');
    }
}
?>
php mysql
1个回答
0
投票

请尝试...如果失败,您会收到错误消息。所以,知道原因

    <!-- Modal -->
<div class="modal fade" id="addadminprofile" tabindex="-1" role="dialog" aria-labelledby="addadminprofile" aria-hidden="true">

  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="addadminprofile">Add Admin Data</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <form action="Code.php" method="POST">
      <div class="modal-body">
        <?php session_start(); echo $_SESSION['err']; ?>

       <div class="form-group">
        <label>Username</label>
        <input type="text" name="username" class="form-control" placeholder="Enter Username">
      </div>
      <div class="form-group">
        <label>Email</label>
        <input type="text" name="email" class="form-control" placeholder="Enter Email">
      </div>
      <div class="form-group">
        <label>Password</label>
        <input type="text" name="password" class="form-control" placeholder="Enter Password">
      </div>
      <div class="form-group">
        <label>Confirm Password</label>
        <input type="text" name="confirmpassword" class="form-control" placeholder="Confirm Password">
      </div>

  </div>


      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
        <button type="submit" name="registerbtn" class="btn btn-primary">Save</button>
      </div>
  </form>
    </div>
  </div>
</div>

Code.php

<?php
session_start();
$connection = mysqli_connect("localhost","root","","adminpanel") or die($connection->connect_errno);

$_SESSION['err']=$connection->error;

if(isset($_POST['registerbtn']))
{
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $cpassword = $_POST['confirmpassword'];

    if($password === $cpassword)
    {   
        $query = "INSERT INTO `register`(`username`, `email`, `password`) VALUES ('".$username."','".$email."','".$password."')";
        $query_run = mysqli_query($connection, $query);

        if($connection->error){
            $_SESSION['err'] = $connection->error;
        }
        else $_SESSION['err'] = "Congratulations! You are Successfully Added";
        if($query_run)
        {
            //echo "Saved";
            $_SESSION['success'] = "Admin Profile Added";
            header('Location: register.php');
        }
        else
        {
            $_SESSION['status'] = "Admin Profile NOT Added";
            header('Location: register.php');
        } 
    }
    else
    {
        $_SESSION['status'] = "Password and Confirm Password Does Not Match";
        header('Location: register.php');
    }
}

?>

确定数据库的结构

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.