vinsux Posted July 10, 2012 Share Posted July 10, 2012 The logic of my code is Suppose that the student is already logged in his account.. there's a page where it restricts a user to enter the page and he needs to register on that page.. without entering any details or it's just a button saying "Ask To Join".. Please.. people with a good heart.. pls. help me... it will be very much appreciated.. Thank you very much.. There's no syntax error.. but it directly goes to verified.php but the user needs to be verified.. to explain it simply, it's just like the YAHOO GROUPS.. where you need to join to access the group.. <?php mysql_connect("localhost","root","") or die(mysql_error()); mysql_select_db("login") or die(mysql_error()); $errors=""; $message=""; $username = $_COOKIE['ID_my_site']; if (isset($_POST['submit'])) { $check = "SELECT $username FROM students"; $result = mysql_query($check) or die(mysql_error()); while($row = mysql_fetch_array($result)) { $id = $row['id']; $username = $row['username']; $namelast = $row['namelast']; $namefirst = $row['namefirst']; $namemi = $row['namemi']; } $insert = "INSERT INTO pendings (id, username, namelast, namefirst, namemi) VALUES ($id, $username, $namelast, $namefirst, $namemi)"; $add_member = mysql_query($insert) or die(mysql_error()); if (mysql_affected_rows()==1){ $message .= "<h1>Registered</h1> <p>Thank you, Please wait for the approval of the faculty teacher who handles the subject.</p>"; } } else { header("location:verified.php"); } ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <br /> <input type="text" name="username" maxlength="60"value="$usernamet"/> <input type="text" name="username" maxlength="60"value="$namelast"/> <input type="hidden" name="namelast" maxlength="20" value="$namefirst ?>" /> <input type="hidden" name="namefirst" maxlength="20" value="$namemi" />< <input type="hidden" name="namemi" maxlength="20" value="$id" /> <center><br /><br /><input type="submit" name="submit" value="Ask to Join" style="width: 100px; height: 30px"/></center> </form> Quote Link to comment https://forums.phpfreaks.com/topic/265472-pls-help-i-need-it-badly/ Share on other sites More sharing options...
Psycho Posted July 10, 2012 Share Posted July 10, 2012 Well, it seems pretty obvious to me. I'm not going to give you a solution as this seems like a homework problem, but I'll help illuminate the problem. You first do a query to get a matching record (if one exists). Assuming that query does not fail, it will return a result - even if that result is empty. Then you have a loop to populate the results of the query into variables. NOTE: I would assume there should only be 0 or 1 results from the query. So, a loop is unnecessary. Anyway, if 0 records were returned from that first query the only difference up to this point would be that the variables would not be defined. After the loop to process the records you generate an insert query and run it. If you don't have error reporting set to show ALL errors/warning those undefined variables will be replaced with empty string in that INSERT query. Then, after the insert query, you check the affected rows. Well, if the values for some of the fields in the query are empty strings and those fields allow empty values the INSERT query will still succeed - you will just have a mostly empty record. Your check for whether the user exists or not should be done based upon the result of the first query. Quote Link to comment https://forums.phpfreaks.com/topic/265472-pls-help-i-need-it-badly/#findComment-1360538 Share on other sites More sharing options...
Barand Posted July 11, 2012 Share Posted July 11, 2012 The SELECT statement itself looks pretty suspect SELECT $username FROM students Does that table have a column for every student username? Further, if only one column is selected, how does he expect to get the other four values, such as id, from the row data? Quote Link to comment https://forums.phpfreaks.com/topic/265472-pls-help-i-need-it-badly/#findComment-1360723 Share on other sites More sharing options...
vinsux Posted July 11, 2012 Author Share Posted July 11, 2012 Thank you very much,.. it worked now, but my problem now is how to know if the user is registered on that group.. I'm sorry i'm newbie on php, i just analyzing the codes what i saw on the tutorials.. "You first do a query to get a matching record (if one exists). Assuming that query does not fail, it will return a result - even if that result is empty. Then you have a loop to populate the results of the query into variables. NOTE: I would assume there should only be 0 or 1 results from the query. So, a loop is unnecessary. Anyway, if 0 records were returned from that first query the only difference up to this point would be that the variables would not be defined." Is it the one that i need to do? I'm sorry, i can't analyze the flow of that scenario Thank you very much again for your help.. Sir Psycho and Sir Barand <?php mysql_connect("localhost","root","") or die(mysql_error()); mysql_select_db("login") or die(mysql_error()); $errors=""; $message=""; if (isset($_POST['submit'])) { if(isset($_COOKIE['ID_my_site'])) { $username = $_COOKIE['ID_my_site']; $result = mysql_query("SELECT * FROM students WHERE username = '$username'"); while($row = mysql_fetch_array($result)) { //prints output lines $un = $row["username"]; $nl = $row["namelast"]; $nf = $row["namefirst"]; $mi = $row["namemi"]; } $insert = "INSERT INTO pendings (username, namelast, namefirst, namemi) VALUES ('$un', '$nl', '$nf', '$mi')"; $add_member = mysql_query($insert) or die(mysql_error()); if($add_member) header("location:verified.php"); else print("data not inserted"); } else { header("location:verified.php"); } } ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <br /> <h2> _________________________________________________________________________</h2> <br /> <center><br /><br /><input type="submit" name="submit" value="Ask to Join" style="width: 100px; height: 30px"/></center> <br /> <h2> _________________________________________________________________________</h2> <br /><br /> <br /><br /> </form> Quote Link to comment https://forums.phpfreaks.com/topic/265472-pls-help-i-need-it-badly/#findComment-1360776 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.