MySQL数据库没有填充

时间:2015-11-11 18:08:03

标签: php mysql database


我正在尝试创建一个注册/登录html / php脚本。我创建了一个数据库,我相信我的html / php代码是正确的。我想知道我是否遗漏了一些小事。到目前为止,这是我的代码。

这是数据库

<?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>
        <?
        }
?>

每当我在MySQL中输入SELECT * FROM member;时,我的数据库都是空的。

3 个答案:

答案 0 :(得分:2)

mysqli_commit($con);
mysqli_query($con,$sql);
mysqli_close($con);

您在插入之前提交,然后关闭未提交的事务。尝试在上面的摘录中交换前两行。

答案 1 :(得分:1)

正如您在评论中已经回答的那样,

该文件的名称不是main_login.php,而是proinput.php

您的表单将其数据发布到main_login.php,我假设您在该页面上没有插入查询,您的代码根本不会运行。< / p>

选项:

  1. 尝试将此php文件的名称更改为main_login.php,然后将echo重定向到用户登录页面

  2. 将插入的这一部分移动到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. 用于调试在php

    打开后立即将以下内容添加到php文件的最顶层
     error_reporting(E_ALL);
     ini_set('display_errors', 'on');
    
  4. 这对我有用

      <?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 :(得分:1)

请在查询结束时添加'或die(mysqli_error())'并获取sql错误报告。这应该告诉你sql究竟是什么问题。 PHP错误报告只会出现php错误。