doufu1970 2015-11-11 18:08
浏览 51
已采纳

MySQL数据库没有填充


I am trying to create a register/login html/php script. I created a database and I believe my html/php code is correct. I was wondering if I am missing something small. Here is my code so far.

Here is the database

<?php
         error_reporting(E_ALL);
         ini_set('display_errors', 'on');
if(isset($_POST["submit"]))
        {
        $User = "**"; 
        $Password = "**";
        $Database = "member";
        $Host = "localhost"; 
        $con = mysqli_connect($Host, $User, $Password);
        if (mysqli_connect_errno())
        {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }

        mysqli_select_db($con,$Database);
        $myusername = $_POST["username"];
        $mypassword = $_POST["password"];
        $sql = "INSERT INTO member (username, password) VALUES ('$myusername','$mypassword')";
        mysqli_query($con,$sql);
        mysqli_commit($con);
        mysqli_close($con);

        echo "Thank You! Information entered.";
        }
else
        {
        ?>
        <form method="post" action="proInput.php">
        Username:<input type="Text" name="username"><br>
        Password:<input type="Text" name="password"><br>
        <input type="Submit" name="submit" value="Register"></form>
        <?
        }
?>

Every time I type SELECT * FROM member; in MySQL my database is empty.

  • 写回答

3条回答 默认 最新

  • dox19458 2015-11-12 03:34
    关注

    As you answered already in your comment,

    the name of the file isn't main_login.php but proinput.php

    Your form is posting it's data to main_login.php and I'm assuming you don't have the insert query on that page, your code isn't run at all.

    Options:

    1. Try changing the name of this php file to main_login.php and then instead of the echo redirect the user to the login page

    2. Move this part of the insert to your main_login.php

      if(isset($_POST["submit"]))
          {
          $User = ""; 
          $Password = "";
          $Database = "member";
          $Host = "localhost"; 
          $con = mysqli_connect($Host, $User, $Password);
          if (mysqli_connect_errno())
          {
          echo "Failed to connect to MySQL: " . mysqli_connect_error();
          }
          mysqli_select_db($con,$Database);
          $myusername = $_POST["username"];
          $mypassword = $_POST["password"];
          $sql = "INSERT INTO member (username, password) VALUES ('$myusername','$mypassword')";
          mysqli_query($con,$sql);
          mysqli_commit($con);
          mysqli_close($con);
      
      
      
      
          echo "Thank You! Information entered.";
          }
          else
          {
          ?>
      
    3. for debugging add the following to the very top of your php file right after the opening of php

       error_reporting(E_ALL);
       ini_set('display_errors', 'on');
      

    This worked for me

      <?php
    
      error_reporting(E_ALL);
      ini_set('display_errors', 'on');
    
      if(isset($_POST["submit"]))
        {
        $User = "db_user"; 
        $Password = "db_password";
        $Database = "db_name";
        $Host = "db_host"; 
        $con = mysqli_connect($Host, $User, $Password);
        if (mysqli_connect_errno())
        {
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
        }
        mysqli_select_db($con,$Database);
        $myusername = $_POST["username"];
        $mypassword = $_POST["password"];
        $sql = "INSERT INTO member (username, password) VALUES ('$myusername','$mypassword')";
        mysqli_query($con,$sql);
        mysqli_commit($con);
        mysqli_close($con);
    
        echo "Thank You! Information entered.";
        }
        else
        {
        ?>
        <form method="post" action="proinput.php">
        Username:<input type="Text" name="username"><br>
        Password:<input type="Text" name="password"><br>
        <input type="Submit" name="submit" value="Register"></form>
        <?php
        }
        ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题