vinsux Posted June 20, 2012 Share Posted June 20, 2012 In my code.. i have 2 tables, the faculty and student tables.. if the type is faculty, the registration will be placed in the faculty table, if the type is student, the registration will be placed in the student table.. i'm debugging it since yesterday pls. help this is for our thesis project and php is not my comfort zone in terms of programming... please please pleaseeeee <?php mysql_connect("localhost","root","") or die(mysql_error()); mysql_select_db("login") or die(mysql_error()); if (isset($_POST['submit'])) { $typecheck = $_POST['type']; $tcheck2 = mysql_query("SELECT type FROM students, faculty WHERE type = '$typecheck'") or die(mysql_error()); $typecheck2 = mysql_num_rows($tcheck2); if ($typecheck2 != 0) { if (!$_POST['username'] | !$_POST['pass'] | !$_POST['pass2']| !$_POST['namelast']| !$_POST['namefirst']| !$_POST['namemi'] | !$_POST['type']| !$_POST['gender']| !$_POST['course'] ) { die('You did not complete all of the required fields'); } if (!get_magic_quotes_gpc()) { $_POST['username'] = addslashes($_POST['username']); } $usercheck = $_POST['username']; $check = mysql_query("SELECT fusername FROM faculty WHERE fusername = '$usercheck'") or die(mysql_error()); $check2 = mysql_num_rows($check); if ($check2 == 1) { die('Sorry, the username '.$_POST['username'].' is already in use.'); } if ($_POST['pass'] != $_POST['pass2']) { die('Your passwords did not match. '); } $_POST['pass'] = md5($_POST['pass']); if (!get_magic_quotes_gpc()) { $_POST['pass'] = addslashes($_POST['pass']); $_POST['username'] = addslashes($_POST['username']); $_POST['namelast'] = addslashes($_POST['namelast']); $_POST['namefirst'] = addslashes($_POST['namefirst']); $_POST['namemi'] = addslashes($_POST['namemi']); $_POST['type'] = addslashes($_POST['type']); $_POST['gender'] = addslashes($_POST['gender']); $_POST['course'] = addslashes($_POST['course']); } $insert = "INSERT INTO faculty (fusername, fpassword, fnamelast, fnamefirst, fnamemi, ftype, fgender, fcourse) VALUES ('".$_POST['username']."', '".$_POST['pass']."', '".$_POST['namelast']."', '".$_POST['namefirst']."', '".$_POST['namemi']."' , '".$_POST['type']."', '".$_POST['gender']."', '".$_POST['course']."')"; $add_member = mysql_query($insert); } if($typecheck2 == 0) { if (!$_POST['username'] | !$_POST['pass'] | !$_POST['pass2']| !$_POST['namelast']| !$_POST['namefirst']| !$_POST['namemi'] | !$_POST['type']| !$_POST['gender']| !$_POST['course'] ) { die('You did not complete all of the required fields'); } if (!get_magic_quotes_gpc()) { $_POST['username'] = addslashes($_POST['username']); } $usercheck = $_POST['username']; $check = mysql_query("SELECT fusername FROM faculty WHERE fusername = '$usercheck'") or die(mysql_error()); $check2 = mysql_num_rows($check); if ($check2 != 0) { die('Sorry, the username '.$_POST['username'].' is already in use.'); } if ($_POST['pass'] != $_POST['pass2']) { die('Your passwords did not match. '); } $_POST['pass'] = md5($_POST['pass']); if (!get_magic_quotes_gpc()) { $_POST['pass'] = addslashes($_POST['pass']); $_POST['username'] = addslashes($_POST['username']); $_POST['namelast'] = addslashes($_POST['namelast']); $_POST['namefirst'] = addslashes($_POST['namefirst']); $_POST['namemi'] = addslashes($_POST['namemi']); $_POST['type'] = addslashes($_POST['type']); $_POST['gender'] = addslashes($_POST['gender']); $_POST['course'] = addslashes($_POST['course']); } $insert = "INSERT INTO students (username, password, namelast, namefirst, namemi, type, gender, course) VALUES ('".$_POST['username']."', '".$_POST['pass']."', '".$_POST['namelast']."', '".$_POST['namefirst']."', '".$_POST['namemi']."' , '".$_POST['type']."', '".$_POST['gender']."', '".$_POST['course']."')"; $add_member = mysql_query($insert); } ?> <h1>Registered</h1> <p>Thank you, you have registered - you may now login</a>.</p> <?php } else { ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <table border="0"> <tr><td>Username:</td><td> <input type="text" name="username" maxlength="60"><br /><br /> </td></tr> <tr><td>Password:</td><td> <input type="password" name="pass" maxlength="10"><br /><br /> </td></tr> <tr><td>Confirm Password:</td><td> <input type="password" name="pass2" maxlength="10"><br /><br /> </td></tr> </td></tr> <tr><td>Name:</td><td> <input type="text" name="namelast" maxlength="15"> <input type="text" name="namefirst" maxlength="15"> <input type="text" name="namemi" maxlength="15"><br /> Last First Middle Name<br /><br /> </td></tr> <tr><td>Course:</td><td> <select name= "course"> <option value="na">---N.A.----</option> <option value="bscs">B.S. Computer Science</option> <option value="bsit">B.S. Information Technology</option> <option value="bsba">B.S. Business Administration</option> </select><br/><br /><br /> <tr><td>Gender:</td><td> <select name= "gender"> <option value="Male">Male</option> <option value="Female">Female</option> </select><br/><br /><br /> </td></tr> <tr><td>Type of Account:</td><td> <select name= "type"> <option value="student">Student</option> <option value="teacher">Teacher</option> </select><br/><br /><br /> </td></tr> <tr><th colspan=2><input type="submit" name="submit" value="Register"></th></tr> </table> </form> <?php } ?> </div> vinsux New php-forum User Posts: 1 Joined: Wed Jun 20, 2012 6:26 am Quote Link to comment https://forums.phpfreaks.com/topic/264507-need-help-about-this-code-please-please-please/ Share on other sites More sharing options...
gristoi Posted June 20, 2012 Share Posted June 20, 2012 what errors are you getting? just posting the code without defining what the issue is is not going to get us very far Quote Link to comment https://forums.phpfreaks.com/topic/264507-need-help-about-this-code-please-please-please/#findComment-1355542 Share on other sites More sharing options...
Maq Posted June 20, 2012 Share Posted June 20, 2012 As gristoi mentioned, we need more information. Honestly, we really don't care that this is for your thesis paper, tell us useful information. What exactly are you trying to do? What happens? What doesn't happen? Errors? If so, what did you try to do to debug them? Quote Link to comment https://forums.phpfreaks.com/topic/264507-need-help-about-this-code-please-please-please/#findComment-1355547 Share on other sites More sharing options...
xyph Posted June 20, 2012 Share Posted June 20, 2012 You should format your code. Nested conditionals and loops are hard to track when they're all vertically aligned. If something is nested, it should be visually obvious. if( $condition ) { nestedCode(); while( $nested_loop ) { evenDeeper(); } } You'll find people will be more likely to help you, and help will come faster if you put effort into the appearance of your code. As it is, it's very difficult to follow. Quote Link to comment https://forums.phpfreaks.com/topic/264507-need-help-about-this-code-please-please-please/#findComment-1355559 Share on other sites More sharing options...
Xtremer360 Posted June 20, 2012 Share Posted June 20, 2012 I suggest using some sort of code separation. Quote Link to comment https://forums.phpfreaks.com/topic/264507-need-help-about-this-code-please-please-please/#findComment-1355563 Share on other sites More sharing options...
Drummin Posted June 20, 2012 Share Posted June 20, 2012 See if this helps. <?php mysql_connect("localhost","root","") or die(mysql_error()); mysql_select_db("login") or die(mysql_error()); $errors=""; $message=""; if (isset($_POST['submit'])) { if (!$_POST['username'] || !$_POST['pass'] || !$_POST['pass2'] || !$_POST['namelast']| !$_POST['namefirst'] || !$_POST['namemi'] || !$_POST['type'] || !$_POST['gender'] || !$_POST['course'] ) { $errors .= "You did not complete all of the required fields"; } if (empty($errors)){ //continue //Check for valid type $allowed_types = array('student', 'faculty'); if (!in_array($_POST['type'],$allowed_types)){ $errors .= "Please select a valid account type."; }else{ $typecheck = $_POST['type']; } } if (empty($errors)){ //continue if ($_POST['course']=="na"){ $errors .= "Please select a course."; } } if (empty($errors)){ //continue if (get_magic_quotes_gpc()){ function stripslashes_deep($value) { $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value); return $value; } $_POST = array_map('stripslashes_deep', $_POST); } } if (empty($errors)){ if ($_POST['pass'] != $_POST['pass2']) { $errors .= "Your passwords did not match."; } } if (empty($errors)){ //continue query DB $_POST['pass'] = mysql_real_escape_string($_POST['pass']); $_POST['username'] = mysql_real_escape_string($_POST['username']); $_POST['namelast'] = mysql_real_escape_string($_POST['namelast']); $_POST['namefirst'] = mysql_real_escape_string($_POST['namefirst']); $_POST['namemi'] = mysql_real_escape_string($_POST['namemi']); $_POST['type'] = mysql_real_escape_string($_POST['type']); $_POST['gender'] = mysql_real_escape_string($_POST['gender']); $_POST['course'] = mysql_real_escape_string($_POST['course']); $pass = md5($_POST['pass']); $check = "SELECT fusername FROM $typecheck WHERE fusername = '" . $_POST['username'] . "'"; $result = mysql_query($check) or die(mysql_error()); $result_count = mysql_num_rows($result); if ($result_count == 1) { $errors .= "Sorry, the username " . $_POST['username'] . " is already in use."; } //Add New User else{ $insert = "INSERT INTO $typecheck (fusername, fpassword, fnamelast, fnamefirst, fnamemi, ftype, fgender, fcourse) VALUES ('".$_POST['username']."', '$pass', '".$_POST['namelast']."', '".$_POST['namefirst']."', '".$_POST['namemi']."' , '".$_POST['type']."', '".$_POST['gender']."', '".$_POST['course']."')"; $add_member = mysql_query($insert) or die(mysql_error()); if (mysql_affected_rows()==1){ $message .= "<h1>Registered</h1> <p>Thank you, you have registered - you may now <a href=\"login.php\">login</a>.</p>"; } } } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Registration</title> </head> <body> <?php //Show success message else show form// if (!empty($message)){ echo "$message"; }else{ //echo any errors if (!empty($errors)){ echo "$errors";} ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <table border="0"> <tr> <td>Username:</td> <td colspan="3"><input type="text" name="username" maxlength="60" value="<?php if(isset($_POST['username'])){ echo "{$_POST['username']}";} ?>" /></td> </tr> <tr> <td>Password:</td> <td colspan="3"><input type="password" name="pass" maxlength="10" value="<?php if(isset($_POST['pass'])){ echo "{$_POST['pass']}";} ?>" /></td> </tr> <tr> <td>Confirm Password:</td> <td colspan="3"><input type="password" name="pass2" maxlength="10" value="<?php if(isset($_POST['pass2'])){ echo "{$_POST['pass2']}";} ?>" /></td> </tr> <tr> <td>Name:</td> <td><input type="text" name="namelast" maxlength="15" value="<?php if(isset($_POST['namelast'])){ echo "{$_POST['namelast']}";} ?>" /></td> <td><input type="text" name="namefirst" maxlength="15" value="<?php if(isset($_POST['namefirst'])){ echo "{$_POST['namefirst']}";} ?>" /></td> <td><input type="text" name="namemi" maxlength="15" value="<?php if(isset($_POST['namemi'])){ echo "{$_POST['namemi']}";} ?>" /></td> </tr> <tr> <td> </td> <td>Last</td> <td>First</td> <td>Middle Name</td> </tr> <tr> <td>Course:</td> <td 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> </td> </tr> <tr> <td>Gender:</td> <td colspan="3"> <select name= "gender"> <?php $gender = array("Male","Female"); foreach($gender as $g){ $selected_gender = (isset($_POST['gender']) && $_POST['gender']==$g ? 'selected="selected"' : ''); echo "<option value=\"$g\" $selected_gender>$g</option>\r"; } ?> </select> </td> </tr> <tr> <td>Type of Account:</td> <td colspan="3"> <select name= "type"> <?php $atype = array( "student" => "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> </td> </tr> <tr> <th colspan="4"><input type="submit" name="submit" value="Register" /></th> </tr> </table> </form> <?php } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/264507-need-help-about-this-code-please-please-please/#findComment-1355597 Share on other sites More sharing options...
Drummin Posted June 20, 2012 Share Posted June 20, 2012 Using empty() for the first checks is a better option as all fields will be posted. <?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($_POST['username']) || empty($_POST['pass']) || empty($_POST['pass2']) || empty($_POST['namelast']) || empty($_POST['namefirst']) || empty($_POST['namemi']) || empty($_POST['type']) || empty($_POST['gender']) || empty($_POST['course'])) { $errors .= "You did not complete all of the required fields"; } if (empty($errors)){ //continue //Check for valid type $allowed_types = array('student', 'faculty'); if (!in_array($_POST['type'],$allowed_types)){ $errors .= "Please select a valid account type."; }else{ $typecheck = $_POST['type']; } } if (empty($errors)){ //continue if ($_POST['course']=="na"){ $errors .= "Please select a course."; } } if (empty($errors)){ //continue if (get_magic_quotes_gpc()){ function stripslashes_deep($value) { $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value); return $value; } $_POST = array_map('stripslashes_deep', $_POST); } } if (empty($errors)){ if ($_POST['pass'] != $_POST['pass2']) { $errors .= "Your passwords did not match."; } } if (empty($errors)){ //continue query DB $_POST['pass'] = mysql_real_escape_string($_POST['pass']); $_POST['username'] = mysql_real_escape_string($_POST['username']); $_POST['namelast'] = mysql_real_escape_string($_POST['namelast']); $_POST['namefirst'] = mysql_real_escape_string($_POST['namefirst']); $_POST['namemi'] = mysql_real_escape_string($_POST['namemi']); $_POST['type'] = mysql_real_escape_string($_POST['type']); $_POST['gender'] = mysql_real_escape_string($_POST['gender']); $_POST['course'] = mysql_real_escape_string($_POST['course']); $pass = md5($_POST['pass']); $check = "SELECT fusername FROM $typecheck WHERE fusername = '" . $_POST['username'] . "'"; $result = mysql_query($check) or die(mysql_error()); $result_count = mysql_num_rows($result); if ($result_count == 1) { $errors .= "Sorry, the username " . $_POST['username'] . " is already in use."; } //Add New User else{ $insert = "INSERT INTO $typecheck (fusername, fpassword, fnamelast, fnamefirst, fnamemi, ftype, fgender, fcourse) VALUES ('".$_POST['username']."', '$pass', '".$_POST['namelast']."', '".$_POST['namefirst']."', '".$_POST['namemi']."' , '".$_POST['type']."', '".$_POST['gender']."', '".$_POST['course']."')"; $add_member = mysql_query($insert) or die(mysql_error()); if (mysql_affected_rows()==1){ $message .= "<h1>Registered</h1> <p>Thank you, you have registered - you may now <a href=\"login.php\">login</a>.</p>"; } } } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Registration</title> </head> <body> <?php //Show success message else show form// if (!empty($message)){ echo "$message"; }else{ //echo any errors if (!empty($errors)){ echo "$errors";} ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <table border="0"> <tr> <td>Username:</td> <td colspan="3"><input type="text" name="username" maxlength="60" value="<?php if(isset($_POST['username'])){ echo "{$_POST['username']}";} ?>" /></td> </tr> <tr> <td>Password:</td> <td colspan="3"><input type="password" name="pass" maxlength="10" value="<?php if(isset($_POST['pass'])){ echo "{$_POST['pass']}";} ?>" /></td> </tr> <tr> <td>Confirm Password:</td> <td colspan="3"><input type="password" name="pass2" maxlength="10" value="<?php if(isset($_POST['pass2'])){ echo "{$_POST['pass2']}";} ?>" /></td> </tr> <tr> <td>Name:</td> <td><input type="text" name="namelast" maxlength="15" value="<?php if(isset($_POST['namelast'])){ echo "{$_POST['namelast']}";} ?>" /></td> <td><input type="text" name="namefirst" maxlength="15" value="<?php if(isset($_POST['namefirst'])){ echo "{$_POST['namefirst']}";} ?>" /></td> <td><input type="text" name="namemi" maxlength="15" value="<?php if(isset($_POST['namemi'])){ echo "{$_POST['namemi']}";} ?>" /></td> </tr> <tr> <td> </td> <td>Last</td> <td>First</td> <td>Middle Name</td> </tr> <tr> <td>Course:</td> <td 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> </td> </tr> <tr> <td>Gender:</td> <td colspan="3"> <select name= "gender"> <?php $gender = array("Male","Female"); foreach($gender as $g){ $selected_gender = (isset($_POST['gender']) && $_POST['gender']==$g ? 'selected="selected"' : ''); echo "<option value=\"$g\" $selected_gender>$g</option>\r"; } ?> </select> </td> </tr> <tr> <td>Type of Account:</td> <td colspan="3"> <select name= "type"> <?php $atype = array( "student" => "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> </td> </tr> <tr> <th colspan="4"><input type="submit" name="submit" value="Register" /></th> </tr> </table> </form> <?php } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/264507-need-help-about-this-code-please-please-please/#findComment-1355613 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.