Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2014-01-15
6
Medium Priority
?
260 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 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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 111

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 111

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

Independent Software Vendors: 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!

Question has a verified solution.

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

By, Vadim Tkachenko. In this article we’ll look at ClickHouse on its one year anniversary.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…
Suggested Courses

636 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