Solved

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

Posted on 2014-01-15
6
252 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Creating and Managing Databases with phpMyAdmin in cPanel.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

895 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

21 Experts available now in Live!

Get 1:1 Help Now