Jump to content

Hlep Understanding This Script


XGD

Recommended Posts

Ok people i am having a weird problem here. i am going through this book and i wrote this script which lets me edit users in my dtabse, and the script works fine, but the problem is in the middle part of the script there is one closing curly bracket that does not have its own opening bracket before that and there is also on the same line an opening curly bracket that doesnt have a closing bracket of its own, but IT MUST BE LIKE THAT IN ORDER TO WORK. So it works, but i dont understand why.If anyone is bothered check it out: (i am including the whole script, though only the middle part is important, i have clearly marked it.

 			 <?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 ###### 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');				?>

Link to comment
Share on other sites

this is the middle part only, the code above this and below this doesnt matter.

// 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 ###### is the end for the opening bracket  before "echo following errors occured??????			  	//THE CODE BELOW THIS DOESNT MATTER, THE DODGY PART ENDS HERE

Thanks

Link to comment
Share on other sites

The closing bracket on line 72 starts on line 57, the closing one on line 75 starts on line 48, and the brackets on lines 83, 84, and 85 start on lines 79, 75, and 26, respectively. Several text editors will show you where the matching bracket is, including ConTEXT.

Link to comment
Share on other sites

THANK YOU man, you are right i believe. I got a similar answer on another forum, the guy commented my code himself, and it seems the if(empty(errors)) finishes before i thought it did.The damn author didnt comment the script right, because what i commented were actually his comments (except for the confused parts).Thanks again

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...