Solved

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

Posted on 2014-01-15
6
251 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
  • 4
  • 2
6 Comments
 
LVL 108

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 108

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
Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

 
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 108

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 108

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
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 …

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now