RichG Posted March 7, 2008 Share Posted March 7, 2008 Hi I am working my way through a php book and it is becoming aware that there may be typo's lol. I have done all the code from my latest bit and actualy managed to go through and sort out all my syntax errors but my page does not work as it should. www.richdgrimes.co.uk/phptest/rate_boss.php if you want to view the page. Here is the code <?php //First set the form srtings which will be displayed //in various cases below $thisfile = $_SERVER['PHP_SELF']; //Have to set this for heredoc $reg_form = <<<EOREGFORM <p>We must ask for your name and email address to ensure that no one votes more than once. We do not associate your personal information with your rating</p> <form method="post" action="$thisfile"> Name: <input type="text" size=25 name="name"><br> Email <input type="text" size=25 name="email"> <input type="hidden" name="stage" value="register"> <br> <input type="submit" name="submit" value="submit"> </form> EOREGFORM; $rate_form = <<< EORATEFORM <p>My boss is</p> <form method="post" action="$thisfile"> <input type="radio" name="rating" value=1> Driving me to look for a new job.<br> <input type="radio" name="rating" value=2> Not the worst. But pretty bad.<br> <input type="radio" name="rating" value=3> Just so-so.<br> <input type="radio" name="rating" value=4> Pretty good.<br> <input type="radio" name="rating" value=5> A pleasure to work with.<br><br> Boss name: <input type="text" size=25 name="boss"><br> <input type="hidden" name="stage" value="rate"><br><br> <input type="submit" name="submit" value="submit"> </form> EORATEFORM; if (!$_POST['submit']) {//O1 //First time. Just show registration form $message = $reg_form; }//C1 elseif ($_POST['submit'] == 'submit' && $_POST['stage'] == 'register') {//O2 //Second time. Show the registration for again on error //Rating form on successful INSERT if (!$_POST['name'] || $_POST['name'] == "" || strlen($_POST['name']>30) || !$_POST['email'] || $_POST['email'] == "" || strlen($_POST['email']>30)) {//O3 $message = '<p>There is a problem. Did you enter a name and email address?</p>'; $message .= $reg_form; }//C3 else {//O4 //Open connection to the database mysql_connect('xxxx', 'xxxx', 'xxxx') or die('failure to connect to database'); mysql_select_db('xxxx'); //Check to see if name and email have been used before $as_name = addslashes($_POST['name']); $tr_name = trim($as_name); $as_email = addslashes($_POST['email']); $tr_email = trim($as_email); $query = "SELECT sub_id FROM raters WHERE name = '$tr_name' AND email = '$tr_email'"; $result = mysql_query($query); if (mysql_num_rows($result) > 0) {//05 error_log(mysql_error()); $message = 'Someone with this name and password has already rated. If you think a mistake has been made, please email [email protected],'; }//C5 else {//O6 //Insert name and email address $query = "INSERT INTO raters (id, name, email) VALUES(NULL, '$tr_name', '$tr_email')"; $result = mysql_query($query); if (mysql_affected_rows() == 1) {//07 $message = $rate_form; }//C7 else {//08 error_log(mysql_error()); $message = '<p>Something went wrong with your signup attempt.</p>'; $message .= $reg_form; }//C8 }//C6 }//C4 }//C2 elseif ($_POST['submit'] == 'submit' && $_POST['stage'] == 'rate') {//O9 //Third time. Store the rating and boss's name //Open connection to the database mysql_connect('xxxx', 'xxxx', 'xxxx') or die('failure to connect to database'); mysql_select_db('xxxx'); //Insert rating and boss's name $as_boss = addslashes($_POST['boss']); $tr_boss = trim($as_boss); $rating = $_POST['rating']; $query = "INSERT INTO ratings (id, rating, boss) VALUES (NULL, '$rating', '$tr_boss')"; $result = mysql_query($query); if (mysql_affected_rows() == 1) {//O10 $message = '<p>Your rating has been submitted.</p>'; }//C10 else {//011 error_log(mysql_error()); $message = '<p>Something went wrong with your rating attempt. Please try again.</p>'; $message .= $rate_form; }//C11 }//C9 ?> <html> <head> </head> <body> <table border="0" cellpadding="10" width="100%"> <tr> <td bgcolor="#f0f8ff" align="center" valign="top" width="17%"></td> <td bgcolor="#ffffff" align="left" valign="top" width="83%"><?php echo $message; ?></td> </tr> </table> </body> </html> My problem is happening around block //O2 and //O3 if i leave the fields blank then i get the correct error message. If i enter an email address it says the problem with sign up attempt error //O8. So then i went to my database and put myself in so when i type my details in it would pick up my details and return the already registered error //O5. It failed to do this as well and went straight to the //O8 error again and did not check the database. As my error did not come up when connecting to the server can anyone see my problem? Cheers RichG Link to comment https://forums.phpfreaks.com/topic/94881-my-page-seems-to-miss-part-of-my-code/ Share on other sites More sharing options...
scvinodkumar Posted March 7, 2008 Share Posted March 7, 2008 change the submit name to different name. in your form you have same name for two submit button Link to comment https://forums.phpfreaks.com/topic/94881-my-page-seems-to-miss-part-of-my-code/#findComment-486039 Share on other sites More sharing options...
RichG Posted March 7, 2008 Author Share Posted March 7, 2008 No I changed the name and the value and it still did the same. Link to comment https://forums.phpfreaks.com/topic/94881-my-page-seems-to-miss-part-of-my-code/#findComment-486047 Share on other sites More sharing options...
RichG Posted March 7, 2008 Author Share Posted March 7, 2008 Tell me if i am wrong but i have looked at the code again and I have read it as follows $query variable is equal to a database query "INSERT or SELECT" and $result is equal to the result of that query BUT does the query actually run? What line actually tells the code to go and run the query. is this my problem. Cheers RichG Link to comment https://forums.phpfreaks.com/topic/94881-my-page-seems-to-miss-part-of-my-code/#findComment-486076 Share on other sites More sharing options...
revraz Posted March 7, 2008 Share Posted March 7, 2008 $result = mysql_query($query); What line actually tells the code to go and run the query. Link to comment https://forums.phpfreaks.com/topic/94881-my-page-seems-to-miss-part-of-my-code/#findComment-486085 Share on other sites More sharing options...
RichG Posted March 7, 2008 Author Share Posted March 7, 2008 OK getting somewhere I can now enter details i know are in the database and get the error someone with those details has already registered with the site. But if i enter new details then they do not get submitted to the DB. Have a look at that part please. I am working away while waiting for feedback. Cheers RichG Link to comment https://forums.phpfreaks.com/topic/94881-my-page-seems-to-miss-part-of-my-code/#findComment-486094 Share on other sites More sharing options...
RichG Posted March 8, 2008 Author Share Posted March 8, 2008 Hi again Still can't find the problem with this code. It does everything except insert the data to the DB. See section //O6 - //C2 it does not put new data into the database for some reason then i just get the error "Something went wrong with your signup attempt" Can anyone see the problem? <?php //First set the form srtings which will be displayed //in various cases below $thisfile = $_SERVER['PHP_SELF']; //Have to set this for heredoc $reg_form = <<<EOREGFORM <p>We must ask for your name and email address to ensure that no one votes more than once. We do not associate your personal information with your rating</p> <form method="post" action="$thisfile"> Name: <input type="text" size=25 name="name"><br> Email <input type="text" size=25 name="email"> <input type="hidden" name="stage" value="register"> <br> <input type="submit" name="submit" value="submit"> </form> EOREGFORM; $rate_form = <<< EORATEFORM <p>My boss is</p> <form method="post" action="$thisfile"> <input type="radio" name="rating" value=1> Driving me to look for a new job.<br> <input type="radio" name="rating" value=2> Not the worst. But pretty bad.<br> <input type="radio" name="rating" value=3> Just so-so.<br> <input type="radio" name="rating" value=4> Pretty good.<br> <input type="radio" name="rating" value=5> A pleasure to work with.<br><br> Boss name: <input type="text" size=25 name="boss"><br> <input type="hidden" name="stage" value="rate"><br><br> <input type="submit" name="submit" value="submit"> </form> EORATEFORM; if (!$_POST['submit']) {//O1 //First time. Just show registration form $message = $reg_form; }//C1 elseif ($_POST['submit'] == 'submit' && $_POST['stage'] == 'register') {//O2 //Second time. Show the registration for again on error //Rating form on successful INSERT if (!$_POST['name'] || $_POST['name'] == "" || strlen($_POST['name']>30) || !$_POST['email'] || $_POST['email'] == "" || strlen($_POST['email']>30)) {//O3 $message = '<p>There is a problem. Did you enter a name and email address?</p>'; $message .= $reg_form; }//C3 else {//O4 //Open connection to the database mysql_connect('xxxx', 'xxxx', 'xxxx') or die('failure to connect to server'); mysql_select_db('xxxx') or die('could not connect to database'); //Check to see if name and email have been used before $as_name = addslashes($_POST['name']); $tr_name = trim($as_name); $as_email = addslashes($_POST['email']); $tr_email = trim($as_email); $query = "SELECT * FROM raters WHERE name = '$tr_name' AND email = '$tr_email'"; $result = mysql_query($query); if (mysql_num_rows($result) > 0) {//05 error_log(mysql_error()); $message = 'Someone with this name and password has already rated. If you think a mistake has been made, please email [email protected],'; }//C5 else {//O6 //Insert name and email address $query = "INSERT INTO raters (id, name, email) VALUES (null, '$tr_name', '$tr_email')"; $result = mysql_query($query); if (mysql_affected_rows() == 1) {//07 $message = $rate_form; }//C7 else {//08 error_log(mysql_error()); $message = '<p>Something went wrong with your signup attempt.</p>'; $message .= $reg_form; }//C8 }//C6 }//C4 }//C2 elseif ($_POST['submit'] == 'submit' && $_POST['stage'] == 'rate') {//O9 //Third time. Store the rating and boss's name //Open connection to the database mysql_connect('db1341.oneandone.co.uk', 'dbo235515377', '2X5V6hgS') or die('failure to connect to database'); mysql_select_db('db235515377'); //Insert rating and boss's name $as_boss = addslashes($_POST['boss']); $tr_boss = trim($as_boss); $rating = $_POST['rating']; $query = "INSERT INTO ratings (id, rating, boss) VALUES (NULL, '$rating', '$tr_boss')"; $result = mysql_query($query); if (mysql_affected_rows() == 1) {//O10 $message = '<p>Your rating has been submitted.</p>'; }//C10 else {//011 error_log(mysql_error()); $message = '<p>Something went wrong with your rating attempt. Please try again.</p>'; $message .= $rate_form; }//C11 }//C9 ?> <html> <head> </head> <body> <table border="0" cellpadding="10" width="100%""> <tr> <td bgcolor="#f0f8ff" align="center" valign="top" width="17%"></td> <td bgcolor="#ffffff" align="left" valign="top" width="83%"><?php echo $message; ?></td> </tr> </table> </body> </html> Cheers RichG Link to comment https://forums.phpfreaks.com/topic/94881-my-page-seems-to-miss-part-of-my-code/#findComment-487262 Share on other sites More sharing options...
BlueSkyIS Posted March 8, 2008 Share Posted March 8, 2008 your parens are a little off strlen($_POST['name']>30) should be strlen($_POST['name'])>30 and strlen($_POST['email'])>30 Link to comment https://forums.phpfreaks.com/topic/94881-my-page-seems-to-miss-part-of-my-code/#findComment-487264 Share on other sites More sharing options...
RichG Posted March 8, 2008 Author Share Posted March 8, 2008 I made the changes you said and the first error kicked in and did not recognize my entry and said did you enter an email even thought i did. The i took the strlen out completly and the error came up again saying registration attempt failed so I think the example in the book must be right with the strlen example and it is something to do with my line i said from //O6. Could it be to do with the DB connection and i need to open and close the connection at each line of DB interactivity or am i ok opening the link and then leaving it open for the whole code? Link to comment https://forums.phpfreaks.com/topic/94881-my-page-seems-to-miss-part-of-my-code/#findComment-487273 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.