simple validation - newbie question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
abionifade
Forum Commoner
Posts: 34
Joined: Thu Apr 18, 2002 5:32 pm

simple validation - newbie question

Post by abionifade »

hi guys,

tryin to get this validation script off the ground to check for the presence of the user_id field. Im unsuccesful in doing so as the record is still created in the database regardless of if the user_id field is complete or blank.

Please do not laugh :)

Code: Select all

<?php

if ($submitted) &#123;

	    $query = "insert into student_table "
                ." (user_id, first_name, last_name, course_id) values "
                ."('$user_id', '$first_name', '$last_name', '$course_id')"
        ;
        $result = mysql_query($query);
		
		if($result) &#123;
			echo "submission succesful"; &#125;
		else &#123;
			echo "error"; 
			&#125;
&#125; else &#123;
print ("You need to enter the user_id"); &#125;		

?>




<head>
</head>

<form method=post action="../create_student_entry.php">
  
  <input type=hidden name="submitted" value="true">
  
  <div align="left">
  <pre><b><font face="Verdana, Arial, Helvetica, sans-serif" size="1">User ID:</font></b><font size="1">	</font>	<input type=text size=5 name=user_id maxlength="5">
<b><font face="Verdana, Arial, Helvetica, sans-serif"><font size="1">First Name:</font>	</font></b><input type=text size=20 name=first_name>   
<font face="Verdana, Arial, Helvetica, sans-serif"><b><font size="1">Last Name:</font></b></font>	<input type=text size=15 name=last_name>   
<b><font face="Verdana, Arial, Helvetica, sans-serif"><font size="1">Course ID:</font>		<select size="1" name="course_id"><option selected>Please Select Course</option><option value="G618">Media Communication Systems</option><option value="G600">Computer Systems Engineering</option><option value="H6H5">Electrical & Electronic Engineering</option></select></font></b>
 
   
<input type=submit name=submit value="Submit">   <input type=reset name=reset value="Clear"> </pre>
  </div>
</form>
<p>&nbsp;</p>
Any help as to where i've gone wrong is much appreciated

thank you :)
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post by dethron »

In your code, at line 2, you are using

Code: Select all

if ($submitted) &#123;
If you use

Code: Select all

if ($user_id) {
It will be better.

However in this system, leaving blank still accepted. To avoid this, get the first 3 character and compare them with nullity.
User avatar
abionifade
Forum Commoner
Posts: 34
Joined: Thu Apr 18, 2002 5:32 pm

Post by abionifade »

thank you very much,

it seems to work now with your help.

-Abi
Post Reply