Solved

The following PHP code is not writing to the database.  How can I insert code to troubleshoot?

Posted on 2014-01-15
6
255 Views
Last Modified: 2014-01-15
Here it is.  I'm not getting an error message, it's just not writing to the database!

<?php session_start();?> 
<?php require_once("includes/functions.php"); ?>
<?php require_once("includes/db_connection.php"); ?>
<?php include ("includes/layouts/header.php");?>

	  
<div id="small_content">
<div id=small_content_headline><h1>Please create user</h1></div>

<?php


  
	  $errors = array();  
		
		if(isset($_POST["submit"])) {
	  	$first_name = ucfirst($_POST["first_name"]);
	  	$last_name = ucfirst($_POST["last_name"]);
	  	$user_name = strtolower($first_name[0] . $last_name);
	  	$access_level = $_POST["access_level"];
		$department = $_POST["department"];
		$password = $_POST["password"];
	  	$password2 = $_POST["password2"];
		
		 	
		/*	 if ($password !== $password2) {
				$errors[] = "Passwords do not match, please re-enter";  
			 if (empty($password)) {
				 $errors[] = "Password is empty, please enter a password";
			 }
					 
					  } else {*/
		$query = "INSERT INTO users (first_name, last_name, user_name,  department, password, password2, access_level) VALUES (";
		$query .= "'{$first_name}', '{$last_name}', '{$user_name}',  '{$department}', '{$password}', '{$password2}', '{$access_level}')";
		$result = mysqli_query($connection, $query);
		
		if (!$result) {
			die("no result added");	
		}
	
		$_SESSION["first_name"] = $first_name;
		$_SESSION["last_name"] = $last_name;
		$_SESSION["user_name"] = $user_name;
		$_SESSION["access_level"] = $access_level;
		$_SESSION["department"] = $department;
		$_SESSION["password"] = $password;
		$_SESSION["password2"] = $password2;
						
		redirect_to("account_created.php");
		}
		
	
	
		
	 else {
  	  	$first_name = "";
	  	$last_name = "";
	  	$user_name = "";
	  	$access_level = "";
		$department = "";
		$password = "";
	  	$password2 = "";
	  	$message = "Please log in";
		
	}
?>

<?php
$options = array("User", "Administrator");
$html_options = "";


$departments = array("Administration", "Research", "Sales", "Trading");
$departments_options = "";

?>
    
    
<form class="form_indent_50" style="width: 400px" action="create_user.php" method="post">
    <table class="user_table" height="200" width = "400" border="0" cellspacing="0" cellpadding="2">
      <tr>
        <td>Enter First Name</td>
        <td><input name="first_name" type="text" value="<?php echo $first_name ?>"></td>
      </tr>
      <tr>
        <td>Enter Last Name</td>
        <td><input type="text" name="last_name" value="<?php echo $last_name ?>"> </td>
      </tr>
      <tr>
        <td>Enter Password</td>
        <td> <input type="password" name="password"> </td>
      </tr>
      <tr>
        <td>Re-Enter Password</td>
        <td><input type="password" name="password2"></td>
      </tr>
       <tr>
        <td>Choose Access Level</td>
        <td><select name="access_level" id="access_level">
                  
            <?php
               foreach ($options as $option) {
               $selected = ($access_level == $option)  ? "selected" : "";
               $html_options .=  "<option value='{$option}' {$selected}>{$option}</option>";
               }
                echo $html_options;
             ?>
              </select>
        </td>
       </tr>
       <tr>
       	<td>Choose Department</td>
        <td><select name="department">
	   <?php
			foreach ($departments as $choice) {
			$selected2 = ($department == $choice) ? "selected" : "";
			$departments_options .= "<option value='{$choice}' {$selected2}>{$choice}</option>";
			}
			echo $departments_options;
		?>
        	 </select>
        
       </td>
       
       </tr>
        


    </table>
    <input style="al name="submit" type="submit" value="Done" >
</form>


</div>

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

Open in new window

0
Comment
Question by:LB1234
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39782212
Your best friend is error_reporting(E_ALL) followed closely by var_dump().   You can use the dump() function to visualize internal data, and it can also tell you where the script logic has taken the execution.  After that, make sure that the code avoids the AntiPHPractices, such as ignoring the return values from PHP functions.
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39782228
In the instant case, line 17 is important:

            if(isset($_POST["submit"])) {

Compare that to line 130, where you will find mismatched quote marks:

<input style="al name="submit" type="submit" value="Done" >

If you use var_dump($_POST) you can pick these things off very easily.
0
 
LVL 1

Author Comment

by:LB1234
ID: 39782233
Gotcha Ray, thanks.  The thing that throws me is line 37, which is a conditional that should help out somewhat, however it's not killing the code AND nothing's being written to the database.

The die statement should error out the page, correct?
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 1

Author Closing Comment

by:LB1234
ID: 39782240
Oh for the love of all that's holy. That was it.  It's not often I feel stupid, but now is one of those times.  Thanks a lot Ray.  I was spending so much time at the top of the code page that I completely ignored that typo.  I was in the process of adding something to the HTML submit button, stopped to do something else, and then came back a few hours later, completely forgetting that.  

Again, thank you.   Spent so much time debugging this.  How silly!!
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39782272
A die() statement should stop script execution, however the destructors() will still be run.  A smarter way to do that sort of thing is to use trigger_error() and throw a message.  Trigger_error() will let you use an error handler, which is a good idea in deployed code.

It's also wise to use a code scanner to isolate things like this:

$errors = array();

The $errors variable is not used anywhere in the code, which begs the question, "Why?"

You may also find that the simple act of lining up your code neatly, so that control structures are readily visible, is a useful thing.  I like to place the curly braces immediately below the statement that controls the logic flow, with consistent indenting.  If the code is easier to read, it follows that it's easier to debug.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 39782274
Thanks for the points, and thanks for using EE.  Sometimes a fresh pair of eyes can spot things that are invisible in plain sight :-)  A quick trip to the W3C Validator is often useful.
http://validator.w3.org/

Best regards, ~Ray
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question