duancuo1234 2016-08-04 11:40
浏览 72
已采纳

无法使用PHP将表单数据插入MySQL数据库

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 Stack Overflow, and found someone who probably worked by the same manual, but I work in localhost, he's on GoDaddy. The solution given there doesn't work for me.

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

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

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')");
  • 写回答

2条回答 默认 最新

  • dongshen4129 2016-08-04 11:50
    关注

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

    <input name="Register" id="Register" type="submit" value="Register" class="regButton">
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 房产抖音小程序苹果搜不到安卓可以付费悬赏
  • ¥15 STM32串口接收问题
  • ¥15 腾讯IOA系统怎么在文件夹里修改办公网络的连接
  • ¥15 filenotfounderror:文件是存在的,权限也给了,但还一直报错
  • ¥15 MATLAB和mosek的求解问题
  • ¥20 修改中兴光猫sn的时候提示失败
  • ¥15 java大作业爬取网页
  • ¥15 怎么获取欧易的btc永续合约和交割合约的5m级的历史数据用来回测套利策略?
  • ¥15 有没有办法利用libusb读取usb设备数据
  • ¥15 为什么openeluer里面按不了python3呢?