-2

I'm trying to insert data to a MySQL table with PHP. I have a form that I would like to post form data like username and password. Everything seems to go fine but the data does not get posted to the database. My database connection is simple... Here it is from a file called 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() . ")"
    );
  }
?>

The page that has the form is called newUser.php and it posts to itself. Here's the page:

<?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"); ?>

When I click on the submit button, the form fields are cleared and nothing is saved to the database. Can you please help with determining why data is not posted? Thanks!

Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
CloudyKooper
  • 727
  • 3
  • 19
  • 47
  • 1
    Done any basic debugging like `var_dump($result)` to see if it's boolean false? You're simply ASSUMING your query will always succeed. Because if you had done even BASIC error checking, you'd have been told about the syntax error caused by the extra comma after `hashed_password` in your field list. – Marc B Jan 08 '15 at 18:09
  • Rogue comma for one thing `hashed_password,` <= `$result = mysqli_query($connection, $query) or die(mysqli_error($connection));` would have told you that, as would http://php.net/manual/en/function.error-reporting.php – Funk Forty Niner Jan 08 '15 at 18:15

1 Answers1

2

try changing

<input type="username" name ="username" value=""/> change type="text"

Here

<form action="newUser.php" method ="post"> newUser.php where is it what does it do??

sanoj lawrence
  • 951
  • 5
  • 29
  • 69
  • **Why** add `enctype="multipart/form-data"`? OP's not uploading nor attaching anything. It's redundant. – Funk Forty Niner Jan 08 '15 at 18:20
  • 1
    Look at the [accepted answer](http://stackoverflow.com/a/4526286/), and I quote: *"When you are writing client-side code, all you need to know is use `multipart/form-data` **when your form includes** any `` elements."* - OP does not have any **input files.** So, please remove that from your answer, it serves no purpose and is misleading. – Funk Forty Niner Jan 08 '15 at 18:24
  • @Fred-ii- thanks little over confident about that – sanoj lawrence Jan 08 '15 at 18:27
  • You're welcome Sanoj. However, have a look at [my comment to OP](http://stackoverflow.com/questions/27846738/why-is-my-form-data-not-posting-to-the-database-in-php#comment44097442_27846738), that is another issue you can look at. It shouldn't be there ;-) – Funk Forty Niner Jan 08 '15 at 18:28
  • @Fred-ii- that's right – sanoj lawrence Jan 08 '15 at 18:31
  • @Fred-ii- as O.P posted question 23 min ago not yet a single comment from O.P feel like giving `-10` reward – sanoj lawrence Jan 08 '15 at 18:32
  • I checked my form data with print_r($_POST); ?> and they were ok so I stripped down the INSERT command and made sure that the database columns matched what was being sent from the form. Not sure what was wrong but I think it had something to do with matching form fields with database. – CloudyKooper Jan 09 '15 at 18:59