为什么我的表单数据没有在php中发布到数据库

时间:2015-01-08 18:07:38

标签: php html forms mysqli

我正在尝试使用PHP将数据插入MySQL表。我有一个表单,我想发布表单数据,如用户名和密码。一切似乎都没问题,但数据没有发布到数据库。我的数据库连接很简单......这里来自一个名为db_connection.php的文件:

<?php
  define("DB_SERVER", "localhost");
  define("DB_USER", "");
  define("DB_PASS", "");
  define("DB_NAME", "global_gl");

  // 1. Create a database connection
  $connection = mysqli_connect(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
  // Test if connection succeeded
  if(mysqli_connect_errno()) {
    die("Database connection failed: " . 
      mysqli_connect_error() . 
      " (" . mysqli_connect_errno() . ")"
    );
  }
?>

具有该表单的页面名为newUser.php,并将其发布到自身。这是页面:

<?php 
    require_once("/includes/session.php");
    require_once("includes/db_connection.php");
    require_once("includes/functions.php");
    require_once("includes/validation_functions.php");
?>

<?php
  if (isset($_POST['submit'])) {
    // Process the form

    // validations
    $required_fields = array("firstName", "lastName", "email", "username",  "password");//TODO:  add country and birthday to this after debugging
    validate_presences($required_fields);

    $fields_with_max_lengths = array("username" => 30);
    validate_max_lengths($fields_with_max_lengths);

    if (empty($errors)) {
      // Perform Create
      $firstName = mysql_prep($_POST["firstName"]);
      $lastName = mysql_prep($_POST["lastName"]);
      $email = mysql_prep($_POST["email"]);
      $username = mysql_prep($_POST["username"]);
      $hashed_password = mysql_prep($_POST["password"]);

      $query  = "INSERT INTO useradmin (";
      $query .= "  firstName, lastName, email, username, hashed_password, ";
      $query .= ") VALUES (";
      $query .= "  '{$firstName}','{$lastName}','{$email}','{$username}','{$hashed_password}'";
      $query .= ")";

      $result = mysqli_query($connection, $query);

      if ($result) {
        // Success
        $_SESSION["message"] = "User created.";
        redirect_to("manage_admin.php");//TODO:  After cretion choose a page to return to.
      } else {
        // Failure
        $_SESSION["message"] = "User creation failed.";
      }
    }
  } else {
    // This is probably a GET request

  } // end: if (isset($_POST['submit']))

?>

<?php include("includes/layouts/header.php"); ?>

<head>

 <script type="text/javascript"    src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js"></script>
 <script type="text/javascript" src="scripts/bday-picker.js"></script>
 <script type="text/javascript">
  $(document).ready(function(){
    $("#createUserDatePicker").birthdaypicker({});

  });
 </script>

</head>
 <div id="main">
  <div id="navigation">
<?php// echo navigation($current_subject, $current_page); ?>
  </div>
  <div id="page">
   <form action="newUser.php" method ="post">  
    <p>First Name:
     <input type = "text" name ="firstName" value=""/>
    </p>
    <p>Last Name:
     <input type = "text" name ="lastName" value=""/>
    </p>
    <p>Email:
     <input type = "email" name ="email" value=""/>
    </p>
    <p>Username:
     <input type = "username" name ="username" value=""/>
    </p>
    <p>Password:
     <input type = "password" name ="password" value=""/>
    </p>
    Counntry:
<?php

  include("countrySelect/index.html");

?><br>

    </p>
    <p>Birthday:

     <div class="picker" id="createUserDatePicker"></div>

    </p>

    <p>
     <input type = "radio" name ="contactMe" value="0"/>&nbsp;  Contact me with news and exclusive conent about Global Gaming League and West Coast Chill products.
    </p>

    <p>
     <input type = "checkbox" name ="accept" value="0"/> &nbsp;I accept the terms of service and privacy policy.
    </p>
    <p>
     <input type="submit" name="submit" value="Create User" />
    </p>
   </form>

   <p>
    <a href ="createUser.php">Cancel</a> <!--Note: this should take us back to a previous page
   </p>

  </div>
 </div>

 <?php include("includes/layouts/footer.php"); ?>

当我单击提交按钮时,表单字段将被清除,并且不会将任何内容保存到数据库中。您能否帮助确定未发布数据的原因?谢谢!

1 个答案:

答案 0 :(得分:3)

尝试更改

<input type="username" name ="username" value=""/>更改type="text"

<强>以下

<form action="newUser.php" method ="post"> newUser.php它在哪里做什么?