数据不使用mysql插入数据库

时间:2017-07-29 10:10:42

标签: php database insert

我试图使用表单将数据输入到我的数据库中,但它根本不起作用。所以我在表格中使用POST方法,我希望你们能帮助我,也希望我能学习,这样就不会再发生了!

我的connect.php

<?php
$con = mysqli_connect("localhost","root","hunzai112");
mysqli_select_db($con,"raty");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>

我的register.php

<?php
    include 'assets/connect.php';
    if(isset($_POST['submit'])){
        if(isset($_POST['fname']) && isset($_POST['lname']) && isset($_POSt['email']) && isset($_POST['password'])){
            $fname = $_POST['fname'];
            $lname = $_POST['lname'];
            $email = $_POST['email'];
            $password = $_POST['password'];
            $send = mysqli_query("INSERT INTO users (firstname,lastname,email,password) VALUES ('$fname','$lname','$email','$password')");

            if($send){
                echo "DONE";
            }
            else {
                echo "error";
            }
        }
    }

?>
<html>
    <head>
        <link href="https://fonts.googleapis.com/css?family=Oswald" rel="stylesheet">
        <link href="https://fonts.googleapis.com/css?family=Open+Sans+Condensed:300" rel="stylesheet">
        <link href="https://fonts.googleapis.com/css?family=Lobster+Two" rel="stylesheet">
        <link rel="stylesheet" type="text/css" href="style/index.min.css">
        <title>Register - RatyPeople</title>
    </head>
    <body>
        <div class="regcontainer">
            <h1>RatyPeople</h1>
            <form  action="" method="POST">
                <input type="text" name="fname" placeholder="First Name"/><br>
                <input type="text" name="lname" placeholder="Last Name"/><br>
                <input type="email" name="email" placeholder="Email Address"/><br>
                <input type="password" name="password" placeholder="Password"/><br>
                <input type="submit" name="submit" value="Register">
            </form>

        </div>

    </body>
</html>

`

4 个答案:

答案 0 :(得分:0)

在您的案例中提供$con的链接标识符

$send = mysqli_query($con, "INSERT INTO users ...

从doc:http://php.net/manual/en/mysqli.query.php

获取更多详细信息

答案 1 :(得分:0)

你的问题在第4行:

if(isset($_POST['fname']) && isset($_POST['lname']) && isset($_POSt['email']) && isset($_POST['password'])){

用$ _POST替换$ _POSt [&#39;电子邮件&#39;] [&#39;电子邮件&#39;]

如果问题存在,您可以替换:

$send = mysqli_query("INSERT INTO users (firstname,lastname,email,password) VALUES ('$fname','$lname','$email','$password')");

使用:

$send = mysqli_query($con,"INSERT INTO test (firstname,lastname,email,password) VALUES ('$fname','$lname','$email','$password')")or die (mysqli_error($con));

并告诉我错误返回。

答案 2 :(得分:0)

1)除了Rishi的回复,试试这个:

  • isset($_POSt['email'])更改为isset($_POST['email']),注意那里的“T”。
  • <form action="" method="POST">更改为<form action="register.php" method="POST">

2)此外,为安全起见,在插入数据库之前转义POST变量,以防止SQL注入:

  • 更改

      $fname = $_POST['fname'];
      $lname = $_POST['lname'];
      $email = $_POST['email'];
      $password = $_POST['password'];`
    

      $fname = mysqli_real_escape_string($con, $_POST['fname']);
      $lname = mysqli_real_escape_string($con, $_POST['lname']);
      $email = mysqli_real_escape_string($con, $_POST['email']);
      $password = mysqli_real_escape_string($con, $_POST['password']);
    

3)这也有助于调试,

  • 更改

           if($send){
               echo "DONE";
           }
           else {
               echo "error";
           }
    

           if($send){
               echo "DONE";
           }
           else {
               echo "Error: ". mysqli_error($con);
           }
    

    允许您查看是否存在mysql错误。

  • <?php之后的register.php顶部包含以下代码,这将有助于调试问题(完成时注释掉):

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

4)对于完整的答案,Rishi说:

  

在您的情况下提供$ con的链接标识符   $ send = mysqli_query($ con,“INSERT INTO users ...   从doc:http://php.net/manual/en/mysqli.query.php

获取更多详细信息

答案 3 :(得分:-1)

AA'b = 90 degree

您没有为查询功能提供数据库连接。