[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 263
  • Last Modified:

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

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
LB1234
Asked:
LB1234
  • 4
  • 2
1 Solution
 
Ray PaseurCommented:
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
 
Ray PaseurCommented:
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
 
LB1234Author Commented:
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LB1234Author Commented:
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
 
Ray PaseurCommented:
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
 
Ray PaseurCommented:
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now