不能使用PHP将表单数据插入MySQL数据库

时间:2022-09-25 19:01:37

I am trying to insert data submitted in a form, into a MySQL Database, and I can't see the problem in my code(except for MySQL injection, that I will work to resolve after actually being able to insert any data). I have searched in *, and found someone who probably worked by the same manual, but I work in localhost, he's on GoDaddy. The solution given there don't work for me. Link to the spoken answer

我正在尝试将以表单形式提交的数据插入到MySQL数据库中,并且我在代码中看不到问题(除了MySQL注入,我将在实际插入任何数据之后努力解决这个问题)。我在*上搜索了一下,找到了一个可能和我一样工作的人,但是我在localhost工作,他在GoDaddy上。给出的解决方案对我不起作用。链接到口语回答

Here is my code for the HTML and the PHP code:

下面是我的HTML和PHP代码代码:

<?php 
  require'connection/connect.php';
  ?>
  <?php
  if(isset($_POST['Register'])){
      session_start();
      $Fname = $_POST['FirstName'];
      $Lname = $_POST['LastName'];
      $Email = $_POST['Email'];
      $pw = $_POST['Password'];


  $sql= $con->query("INSERT INTO user (Fname,Lname,Email,Password) values('{$Fname}', '{$Lname}', '{$Email}', '{$pw}')");
  }

  ?>
  <!DOCTYPE html >
  <html>
  <head>
  <link href="css/Master.css" rel="stylesheet" type="text/css" />
  <link href="css/Menu.css" rel="stylesheet" type="text/css" />
  <title>Register</title>
  </head>

  <body>


  <div class="container">
    <div class="header">

    </div>
    <div class="menu">
      <div id="NavBar">
       <nav>
       <ul>
       <li>
       <a href="Login.php">Login</a>
       </li>
       <li>
       <a href="Register.php">Register</a>
       </li>
       </ul>
       </nav>
  </div>

    </div>
    <div class="leftBody"></div>
    <div class="rightBody">
      <form  action="" method="post" name="RegisterForm" id="RegisterForm">
      <div class="FormElement">
      <input name="FirstName" id="FirstName" type="text" placeholder="First Name" class="TField">
      </div><br>
       <div class="FormElement">
      <input name="LastName" id="LastName" type="text" placeholder="Last Name" class="TField">
      </div><br>
       <div class="FormElement">
      <input name="Email" id="Email" type="email" placeholder="E-Mail" class="TField">
      </div><br>
       <div class="FormElement">
      <input name="Password" id="Password" type="password" placeholder="Password" class="TField">
      </div><br>
      <input name="Register" id="Register" type="button" value="Register" class="regButton">
      </form>

    </div>
    <div class="footer"></div>


  </div>

  </body>
  </html>

And this is my connection.php file: That it does show that it connects.

这就是我的联系。php文件:它确实显示了它的连接。

<?php
$host="localhost";
$username="root";
$password="";
$dataBase="users";
$con=mysqli_connect($host,$username,$password,$dataBase);
if (!$con) {
    die("Could not connect: " . mysqli_error());
}
echo "Connected successfully";
?>

And a picture of my database in phpMyAdmin: Database

以及我的数据库在phpMyAdmin: database中的图片

I have tried also using this line like this for some reason, but to no avail.:

出于某种原因,我也试过用这句话,但没有用。

$sql= $con->query("INSERT INTO user
                          (Fname,Lname,Email,Password)   
                   values ('Fname', 'Lname', 'Email', 'pw')");

Any help would be greatly appreciated. Thanks!

如有任何帮助,我们将不胜感激。谢谢!

2 个解决方案

#1


5  

i didn't see submit button that actually submit the form. So if i am not wrong please try to use

我没有看到真正提交表单的submit按钮。所以如果我没有错,请尽量用

<input name="Register" id="Register" type="submit" value="Register" class="regButton">

#2


-1  

your code should be run

您的代码应该运行

$sql= $con->query("INSERT INTO user 
                           (Fname,Lname,Email,Password)   
                     VALUES('$Fname', '$Lname', '$Email', '$pw')");

#1


5  

i didn't see submit button that actually submit the form. So if i am not wrong please try to use

我没有看到真正提交表单的submit按钮。所以如果我没有错,请尽量用

<input name="Register" id="Register" type="submit" value="Register" class="regButton">

#2


-1  

your code should be run

您的代码应该运行

$sql= $con->query("INSERT INTO user 
                           (Fname,Lname,Email,Password)   
                     VALUES('$Fname', '$Lname', '$Email', '$pw')");