Jump to content

[SOLVED] Curly brackets in the script, dont understand them


Recommended Posts

Hello, i have this script tp edit users that works, i made it using this book, but there is a line that goes like "} else {", and the closing bracket on that line doesnt have an opening bracket, and opening bracket doesnt have a closing bracket, but it must be like that in order to work (i have tried variations). I dont understand how it works ? Heres the whole script, and then just the dodgy part:

 


		 <?php

$page_title = "Edit user";

include('includes/header.html');

echo "<h1>Edit user</h1>";

if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) {

   $id = $_GET['id'];
   
   } elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) {
   
    $id = $_POST['id'];
} else {
echo "this pages been accessed in error";
include('includes/footer.html');
exit();

}


require_once('includes/mysqli_connect.php');

if (isset($_POST['submitted'])) {

   $errors = array();

if (empty($_POST['first_name'])) {
   $errors[] = "Didnt enter FIRST NAME";
   } else {
   $fn = $_POST['first_name'];
   }
   
if (empty($_POST['last_name'])) {
   $errors[] = "Didnt enter LAST NAME";
   } else {
   $ln = $_POST['last_name'];
   }
   
if (empty($_POST['email'])) {
   $errors[] = "Didnt enter EMAIL";
   } else {
   $e = $_POST['email'];
   }
   
 if (empty($errors)) {


$q = "SELECT user_id FROM users WHERE email='$e' AND user_id!=$id ";

 $r = mysqli_query($dbc, $q);

 // THE CODE ABOVE THIS DOESNT MATTER! THE DOGY PART STARTS RIGHT HERE

 if (mysqli_num_rows($r) == 0) {

  $q = "UPDATE users SET first_name='$fn', last_name='$ln', email='$e' WHERE user_id=$id LIMIT 1";

  
$r = mysqli_query($dbc, $q);
  
  if (mysqli_affected_rows($dbc) == 1) {
  
     echo "User HAS BEEN UPDATED<BR />";
	 } else {

	   echo "system error".mysqli_error($dbc);
	   }
	   
	   } else {
	   echo "the email address has already been registered";
	   } 
	   } else { //here are the shifty brackets, closing one without an opener and vice versa
	   
	   echo "the following errors occured: <br />";
	   
	   foreach ($errors as $msg) {
	   
	      echo $msg."<br />";
		  
		  }  //end of foreach
		  }  //end of if (empty($errors)
		  } //end of main if conditional
		  // but where the hell is the end for the opening bracket  before "echo following errors occured??????
		  
//THE CODE BELOW THIS DOESNT MATTER, THE DODGY PART ENDS HERE 

      $q = "SELECT first_name, last_name, email FROM users WHERE user_id=$id";
		  
		  $r = mysqli_query($dbc, $q);
		  
		  if (mysqli_num_rows($r) == 1) {
		  
		     $row = mysqli_fetch_array($r, MYSQLI_NUM);


			 echo '<form action="edit_user.php" method="post">
                  
		   
			  <p>First Name: <input type="text" name="first_name" value="' .$row[0]. '"/></p>
			  
			  <p>Last Name: <input type="text" name="last_name" value="' .$row[1]. '"/></p>
   
              <p>Email: <input type="text" name="email" value="'.$row[2].'"/></p>
			  
			  <input type="submit" name="submit" value="Update" />
			  
			  <input type="hidden" name="submitted" value="TRUE" />
			  
			  <input type="hidden" name="id" value="'.$id.'" />
			  
			  </form>';
			    } else {
			  echo "this page has been accessed in error<br />";
			  }
			  include('includes/footer.html');
			?>

 

<?php
if (mysqli_num_rows($r) == 0) {

  $q = "UPDATE users SET first_name='$fn', last_name='$ln', email='$e' WHERE user_id=$id LIMIT 1";

  
$r = mysqli_query($dbc, $q);
  
  if (mysqli_affected_rows($dbc) == 1) {
  
     echo "User HAS BEEN UPDATED<BR />";
	 } else {

	   echo "system error".mysqli_error($dbc);
	   }
	   
	   } else {
	   echo "the email address has already been registered";
	   } 
	   } else { //here are the shifty brackets, closing one without an opener and vice versa
	   
	   echo "the following errors occured: <br />";
	   
	   foreach ($errors as $msg) {
	   
	      echo $msg."<br />";
		  
		  }  //end of foreach
		  }  //end of if (empty($errors)
		  } //end of main if conditional
		  // but where the hell is the end for the opening bracket  before "echo following errors occured??????
?>

 

 

 

 

<?php

$page_title = "Edit user";

include('includes/header.html');

echo "<h1>Edit user</h1>";

if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) {

$id = $_GET['id'];

} elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) {

$id = $_POST['id'];
} else {
echo "this pages been accessed in error";
include('includes/footer.html');
exit();

}


require_once('includes/mysqli_connect.php');

if (isset($_POST['submitted'])) {

$errors = array();

if (empty($_POST['first_name'])) {
	$errors[] = "Didnt enter FIRST NAME";
} else {
	$fn = $_POST['first_name'];
}

if (empty($_POST['last_name'])) {
	$errors[] = "Didnt enter LAST NAME";
} else {
	$ln = $_POST['last_name'];
}

if (empty($_POST['email'])) {
	$errors[] = "Didnt enter EMAIL";
} else {
	$e = $_POST['email'];
}

if (empty($errors)) {


	$q = "SELECT user_id FROM users WHERE email='$e' AND user_id!=$id ";

	$r = mysqli_query($dbc, $q);

	// THE CODE ABOVE THIS DOESNT MATTER! THE DOGY PART STARTS RIGHT HERE

	if (mysqli_num_rows($r) == 0) {

		$q = "UPDATE users SET first_name='$fn', last_name='$ln', email='$e' WHERE user_id=$id LIMIT 1";


		$r = mysqli_query($dbc, $q);

		if (mysqli_affected_rows($dbc) == 1) {

			echo "User HAS BEEN UPDATED<BR />";
		} else {

			echo "system error".mysqli_error($dbc);
		}

	} else {
		echo "the email address has already been registered";
	} 
} else { //here are the shifty brackets, closing one without an opener and vice versa

	echo "the following errors occured: <br />";

	foreach ($errors as $msg) {

		echo $msg."<br />";

	}  //end of foreach
}  //end of if (empty($errors)
} //end of main if conditional
// but where the hell is the end for the opening bracket  before "echo following errors occured??????

//THE CODE BELOW THIS DOESNT MATTER, THE DODGY PART ENDS HERE 

$q = "SELECT first_name, last_name, email FROM users WHERE user_id=$id";

$r = mysqli_query($dbc, $q);

if (mysqli_num_rows($r) == 1) {

$row = mysqli_fetch_array($r, MYSQLI_NUM);


echo '<form action="edit_user.php" method="post">


<p>First Name: <input type="text" name="first_name" value="' .$row[0]. '"/></p>

<p>Last Name: <input type="text" name="last_name" value="' .$row[1]. '"/></p>

<p>Email: <input type="text" name="email" value="'.$row[2].'"/></p>

<input type="submit" name="submit" value="Update" />

<input type="hidden" name="submitted" value="TRUE" />

<input type="hidden" name="id" value="'.$id.'" />

</form>';
} else {
echo "this page has been accessed in error<br />";
}
include('includes/footer.html');
?>

 

That's your code againt with the correct indentation, all the code is fine. All openning brackets have a closing bracket.

u are confused by the else before the foreach, as u can see below the if (empty($errors)) ends before the foreach and not after it, i've also added comments after each closing braces "}" to show which if has ended

<?php
$page_title = "Edit user";
include('includes/header.html');
echo "<h1>Edit user</h1>";

if ((isset($_GET['id'])) && (is_numeric($_GET['id']))) {
   		$id = $_GET['id'];
   	} else if ((isset($_POST['id'])) && (is_numeric($_POST['id']))) {
    	$id = $_POST['id'];
} else {//end of if else if ((isset($_POST['id'])) && (is_numeric($_POST['id'])))
	echo "this pages been accessed in error";
	include('includes/footer.html');
	exit();
}// end of of if ((isset($_GET['id'])) && (is_numeric($_GET['id'])))

require_once('includes/mysqli_connect.php');

if (isset($_POST['submitted'])) {
	$errors = array();
	if (empty($_POST['first_name'])) {
   		$errors[] = "Didnt enter FIRST NAME";
   	} else { // end of if (empty($_POST['first_name']))
   		$fn = $_POST['first_name'];
   	}
   
	if (empty($_POST['last_name'])) {
		$errors[] = "Didnt enter LAST NAME";
	} else { // end of if (empty($_POST['last_name']))
		$ln = $_POST['last_name'];
   	}
   
	if (empty($_POST['email'])) {
   		$errors[] = "Didnt enter EMAIL";
   	} else { //end of if (empty($_POST['email']))
   		$e = $_POST['email'];
   	} 

 	if (empty($errors)) {
		$q = "SELECT user_id FROM users WHERE email='$e' AND user_id!=$id ";

 		$r = mysqli_query($dbc, $q);

 		// THE CODE ABOVE THIS DOESNT MATTER! THE DOGY PART STARTS RIGHT HERE

 		if (mysqli_num_rows($r) == 0) {
  			$q = "UPDATE users SET first_name='$fn', last_name='$ln', email='$e' WHERE user_id=$id LIMIT 1";
				$r = mysqli_query($dbc, $q);
  
  			if (mysqli_affected_rows($dbc) == 1) {
    			echo "User HAS BEEN UPDATED<BR />";
	 		} else { //end of if (mysqli_affected_rows($dbc) == 1)
	 			echo "system error".mysqli_error($dbc);
	  		}
	   	} else { // end of if (mysqli_num_rows($r) == 0)
			echo "the email address has already been registered";
	   	} 
	} else { // end of if (empty($errors))
		echo "the following errors occured: <br />";
		foreach ($errors as $msg) {
			echo $msg."<br />";
		}  // end of foreach
	}  // end of else (above)
} // end of if (isset($_POST['submitted']))
?>

Hey man, thanks for replying. I know the ode is correct, that is what confuses me.

 

i still do not know where is the closing bracket for the opening one that starts above "echo "the following errors occured" ?

 

The last 3 brackets dont close that one as far as i can tell, actually i am sure they dont. Can you pinpoint the closing bracket for that opening one please ?

My previous posty was for gevans.

 

Exhaler, yes i see now, you might be right. THANK YOU.

 

But when i look at the commented script from the author, it says the "if (empty($errros)) { finished where I said/thought it did.

 

I guess he made the mistake, because your explanation makes sense.

 

thanks again for helping.

Yes i am using dreameaver. The problem wasnt in being unable to visual the different brackets, but THE DAMN AUTHOR OF THE BOOK I AM USING DIDNT COMMENT THE SCRIPT RIGHT.

 

thanks again to exhaler for solving the mistery.

 

All is well now, lock this topic if you wish.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.