提交表单时将php数据输入MySQL

时间:2017-09-15 03:23:23

标签: php mysql

我已成功将数据输入我的MySQL数据库,但在提交表单时无法插入数据。此时,只要我加载register.php页面,就会插入空白数据。我读到我需要将此代码放在一个单独的文档中,该文档仅包含php代码。我试过了,并将该页面添加到表单操作中。它重定向到proccess.php页面,但实际上没有插入数据。这是我的php:

<?php
  $servername = "localhost";
  $username = "root";
  $password = "@Passw0rd";
  $dbname = "accounts";

  // Create connection
  $conn = mysqli_connect($servername, $username, $password, $dbname);
  // Check connection
  if (!$conn) {
      die("Connection failed: " . mysqli_connect_error());
  }

  $name = mysqli_real_escape_string($link, $_REQUEST['Ausername']);
  $email = mysqli_real_escape_string($link, $_REQUEST['email']);
  $userPassword = mysqli_real_escape_string($link, $_REQUEST['Apassword']);

  $sql = "INSERT INTO users (username, email, password)
  VALUES ('$name', '$email', '$userPassword')";

  if (mysqli_query($conn, $sql)) {
      echo "New record created successfully";
  } else {
     echo "Error: " . $sql . mysqli_error($conn);
  }

  mysqli_close($conn);
?>

和我的表格:

<form class="form-inline" action="register.php" method="post">
    <div class="input-group">
        <span class="input-group-addon"><i class="fa fa-user"></i></span>
        <input type="text" class="form-control" id="Ausername" placeholder="Username" name="Ausername" required>
    </div>
    <br> <br>
    <div class="input-group">
        <span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span>
        <input type="text" class="form-control" id="email" placeholder="Email" name="email" required>
    </div>
    <br> <br>
    <div class="input-group">
        <span class="input-group-addon"><i class="glyphicon glyphicon-lock"></i></span>
        <input type="text" class="form-control" id="Apassword" placeholder="Password" name="Apassword" required>
    </div>
    <br> <br> <br>
    <button type="submit" class="btn btn-primary" id="button" name="submit" value="submit">
        <i class="fa fa-user-plus"></i> Sign Up
    </button>
    <br> <br>
</form>

3 个答案:

答案 0 :(得分:0)

使用$_POST代替$_REQUEST并尝试

  $name = mysqli_real_escape_string($link, $_POST['Ausername']);
  $email = mysqli_real_escape_string($link, $_POST['email']);
  $userPassword = mysqli_real_escape_string($link, $_POST['Apassword']);

答案 1 :(得分:0)

您的表单操作&#34; POST&#34;,您应该使用$ _POST检索数据。

我的IF语句的条件,所有值都是强制性的,您可以编辑自己的值。

 <?php
      $servername = "localhost";
      $username = "root";
      $password = "@Passw0rd";
      $dbname = "accounts";

      // Create connection
      $conn = mysqli_connect($servername, $username, $password, $dbname);
      // Check connection
      if (!$conn) {
          die("Connection failed: " . mysqli_connect_error());
      }

    if (isset($_POST["submit"]) && !empty($_POST["Ausername"] && !empty($_POST["email"] && !empty($_POST["Apassword"])) {
      $name = mysqli_real_escape_string($link, $_POST['Ausername']);
      $email = mysqli_real_escape_string($link, $_POST['email']);
      $userPassword = mysqli_real_escape_string($link, $_POST['Apassword']);
      }else {
   // redirect to form page 
      }
      $sql = "INSERT INTO users (username, email, password)
      VALUES ('$name', '$email', '$userPassword')";

      if (mysqli_query($conn, $sql)) {
          echo "New record created successfully";
      } else {
         echo "Error: " . $sql . mysqli_error($conn);
      }

      mysqli_close($conn);
    ?>

答案 2 :(得分:0)

请先将表格中的数据保存在变量中,先检查是否提交了帖子并查询以下代码

<?php
      if(isset($_POST['submit']){
           $name = mysqli_real_escape_string($link, $_POST['Ausername']);
          $email = mysqli_real_escape_string($link, $_POST['email']);
         $userPassword=mysqli_real_escape_string($link,$_POST['Apassword']);
    $query="INSERT INTO users(username, email, password) 
                         VALUES('$name','$email','$userPassword')";
if($query){
   ?>
 <script>
  alert("record added");
  window.location="index.php";//where index.php is a page with your html codes
</script>
}
else
   die(mysqli_error($link));
<?php
 }
?>

或者您可以在从数据中插入数据之后使用unset($_POST);,请通知我们,但请使用$ _POST代替$ _REQUEST