Jump to content

pls. help on error trapping


vinsux

Recommended Posts

hi again...

 

i need a help again,.. i need an error trapping for my code..

 

if the selected type of account is faculty, the default course in the drop down menu must be "na"

if the selected type of account is student, then it cannot accept the "na" value because he's a student...

 

i'm tired finding a solution and researching, please help me again..

 

tnx in advance..

 

<?php 
mysql_connect("localhost", "root","")or die(mysql_error()); 
mysql_select_db("login") or die(mysql_error()); 

$errors="";
$message="";

if (isset($_POST['submit'])) 

	{ 
if (empty($errors)){

		$_POST['type'] = mysql_real_escape_string($_POST['type']);
		$_POST['course'] = mysql_real_escape_string($_POST['course']);			

		$check = "SELECT STATEMENT";
		$result = mysql_query($check) or die(mysql_error());

			$insert = "INSERT STATEMENT";
			$add_member = mysql_query($insert) or die(mysql_error());	

			if (mysql_affected_rows()==1){
				echo '<script type="text/javascript">alert(" '.$_POST['namelast'].', '.$_POST['namefirst'].' has been Registered!")</script>';
				$message .= "<br><br><h1>Registered</h1>
				<br><br><p>Thank you, the user ".$_POST['namelast']." registered..</p>
				<p><a href = 'faculty.php'>[Click here]</a> to go back to home page or <a href = 'register.php'>[click here]</a> to register again<br>             <br>";
			}
		}

?>
<?php
if (!empty($message)){ 
echo "$message";
}else{
if (!empty($errors)){ echo "<br />$errors";}
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border="0">
	<tr>
		<th>Type of Account:</th>
		<th colspan="3">
			<select name= "type">
			<?php
				$atype = array(
				"students" => "Student",
				"faculty" => "Teacher"
				);
				foreach($atype as $tkey => $tvalue){
				$selected_type = (isset($_POST['type']) && $_POST['type']==$tkey ? 'selected="selected"' : '');
				echo "<option value=\"$tkey\" $selected_type>$tvalue</option>\r";
				}
				?>	
			</select>
               </th>
		</tr> 

	<tr>
		<th>  *Course:</th>
		<th colspan="3">
			<select name= "course">
				<?php
				$courses = array(
				"na" => "---N.A.----", 
				"bscs" => "B.S. Computer Science", 
				"bsit" => "B.S. Information Technology", 
				"bsba" => "B.S. Business Administration"
				);
				foreach($courses as $key => $value){
				$selected_course = (isset($_POST['course']) && $_POST['course']==$key ? 'selected="selected"' : '');
				echo "<option value=\"$key\" $selected_course>$value</option>\r";
				}
				?> 						
			</select>
              
        </th>
	</tr>

       	</table>

</form>

Link to comment
Share on other sites

Well, it all depends on what you want to do with the error, but you could do something like this:

if( ($_POST['type']=='faculty' && $_POST['course'] != 'na') ||
    ($_POST['type']!='faculty' && $_POST['course'] == 'na'))
{
    //Invalid
}

 

or you could just populate the course list without the 'na' option. Then, put some text on the form that when selecting 'student' that a course must also be selected. Then when the form is posted if faculty is selected just ignore the course selection.

 

Also, why are you escaping the input for query use before you have done all your validations? That will lead to problems eventually.

Link to comment
Share on other sites

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.