timecatcher Posted December 7, 2008 Share Posted December 7, 2008 Hey guys, I have made this page, which basically allows the user to choose a 'companion' and then set the name of it. Its supposed to then set up these details in one of the fields on my table however when I click 'submit' it just takes me to the page where you choose your 'companion' again and doesn't enter there details. Now heres the code: <? require("includes/connect.inc.php") ; require("includes/navbar.inc.php") ; echo '<link rel=\'stylesheet\' href=\'includes/layoutstylesheet.css\' type=\'text/css\'><div id=\'content\'>' ; $sql = "SELECT COUNT(*) FROM user WHERE username='".mysql_real_escape_string($_COOKIE['kurukouser'])."'"; $query = mysql_query($sql) or die(mysql_error()); $r = mysql_fetch_row($query); if($r[0] == 1){ $username = htmlentities($_COOKIE['kurukouser'],ENT_QUOTES,"utf-8"); echo '<center>' ; $sql = "SELECT * FROM companion "; $result = mysql_query($sql) ; $companion = $_GET['companion'] ; if(!isset($companion)) { echo'Hey '.$username.' welcome to companion rocks, here you can choose what you want you companion to be during your journeys, <br />of course this is optional but the benefits can be great!' ; while($row=mysql_fetch_array($result)) { $cname = $row['name'] ; $cimage = $row['image'] ; echo'<br /><br />' ; echo'<img src="'.$cimage.'">' ; echo'<br />' ; echo $cname ; echo'<br />' ; echo'<a href="http://kurukolands.co.uk/companion.php?companion='.$cname.'">Take</a>' ; } }elseif(isset($companion)) { $sql = "SELECT * FROM companion" ; $result = mysql_query($sql) ; while($row=mysql_fetch_array($result)) { if($row['name'] == $companion) { $cimage = $row['image'] ; $cdescription = $row['description'] ; echo $companion ; echo'<br /><br /><img src="'.$cimage.'">' ; echo'<br /><br />'.$cdescription.'' ; echo'<form method="post" action="companion.php"><br />Name: <input type="text" name="name"><br /><input type="submit" name="submit" value="Submit">' ; if(isset($_POST['Submit'])) { $name = $_POST['name'] ; $food = 0 ; $energy = 0 ; mysql_query("INSERT INTO usercompanion(username, name, food, energy) VALUES ('$username', '$name', '$food', '$energy')") ; } } } } }else{ echo'Error: You must be logged in to view this page.' ; } echo'</div></center>' ; ?> I basically want to know what im doing wrong if anyone could tell me, then I can try to fix it. Thanks. Timecatcher. Quote Link to comment https://forums.phpfreaks.com/topic/135949-solved-submit-button-help-needed/ Share on other sites More sharing options...
jnerotrix Posted December 7, 2008 Share Posted December 7, 2008 I think the form needs to be placed after the if(isset($_POST['Submit'])) { //Form Proccessing } Not Before Quote Link to comment https://forums.phpfreaks.com/topic/135949-solved-submit-button-help-needed/#findComment-708661 Share on other sites More sharing options...
Flames Posted December 7, 2008 Share Posted December 7, 2008 firstly dont use short start tags as it stops syntax highlighting. change mysql_query("INSERT INTO usercompanion(username, name, food, energy) VALUES ('$username', '$name', '$food', '$energy')") ; to mysql_query("INSERT INTO usercompanion(username, name, food, energy) VALUES ('$username', '$name', '$food', '$energy')") or die("Error with query. "mysql_error()); and change $result = mysql_query($sql) ; to $result = mysql_query($sql) or die("Error with query2. "mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/135949-solved-submit-button-help-needed/#findComment-708663 Share on other sites More sharing options...
timecatcher Posted December 7, 2008 Author Share Posted December 7, 2008 hehe alright however that didn't really help with the submit issue but I know what you mean xD. Timecatcher. and ok jnero I might try that. Quote Link to comment https://forums.phpfreaks.com/topic/135949-solved-submit-button-help-needed/#findComment-708677 Share on other sites More sharing options...
premiso Posted December 7, 2008 Share Posted December 7, 2008 Just a side note, I would also use indentation in my code. It makes it soo much easier to read and debug. As you have it there you have like 5 } in a row and no one can tell which goes with what unless they do a painful trace back. If you are just using notepad, I would find a program that syntax highlights as well, such as notepad ++. It makes it a ton easier to code with. Quote Link to comment https://forums.phpfreaks.com/topic/135949-solved-submit-button-help-needed/#findComment-708686 Share on other sites More sharing options...
timecatcher Posted December 7, 2008 Author Share Posted December 7, 2008 Oh hehe yeah second person to mention indents xD didn't think about the }'s. Thanks. Oh and im using dreamweaver. Thanks. TC. Still no avail with this whole insert info thing by the way. Quote Link to comment https://forums.phpfreaks.com/topic/135949-solved-submit-button-help-needed/#findComment-708692 Share on other sites More sharing options...
premiso Posted December 7, 2008 Share Posted December 7, 2008 Oh hehe yeah second person to mention indents xD didn't think about the }'s. Thanks. Oh and im using dreamweaver. Thanks. TC. Still no avail with this whole insert info thing by the way. Dreamweaver is great for designing pages, but I have never used it for PHP coding, but knowing how dreamweaver works I probably wouldn't recommend it. Dreamweaver is all about just getting it running and not caring if someone can actually read through stuff. My 2 cents anyhow, or search if dreamweaver has a php plugin for proper indents etc. Quote Link to comment https://forums.phpfreaks.com/topic/135949-solved-submit-button-help-needed/#findComment-708694 Share on other sites More sharing options...
timecatcher Posted December 7, 2008 Author Share Posted December 7, 2008 Well I haven't really had any problems, I didn't really like moving from dreamweaver to notepad++ however and needed to change all the colours again xD. I think i'll stick with dreamweaver for now. Any ideas on my actual problem. Timecatcher. Quote Link to comment https://forums.phpfreaks.com/topic/135949-solved-submit-button-help-needed/#findComment-708698 Share on other sites More sharing options...
premiso Posted December 7, 2008 Share Posted December 7, 2008 No, I do not like trying to cipher non-indented code. Sorry man, it requires too much of my brain power to go and indent it all then find the source of the issue etc, especially when it could easily just be a missing } etc which you have been found with proper indentation. I figure if you cannot take the 5 minutes to properly indent it for us, than yea I cannot take the 5 minutes to diagnose your problem. At least that is my perspective on it. EDIT: I am not meaning to be rude, sorry just read it and it seemed rude. I am just trying to help you get more help out of these forms, cause I know there are others who think along the same lines. Quote Link to comment https://forums.phpfreaks.com/topic/135949-solved-submit-button-help-needed/#findComment-708700 Share on other sites More sharing options...
timecatcher Posted December 7, 2008 Author Share Posted December 7, 2008 No thats fine. I will repost the form indented, however im not sure exactly how to so I hope it looks ok! No it wasn't rude , I understand people arn't just here to help me hehe! Thanks, Timecatcher. EDIT: Just realised I can't edit previous posts xD. Well heres the code again. <?php require("includes/connect.inc.php") ; require("includes/navbar.inc.php") ; echo '<link rel=\'stylesheet\' href=\'includes/layoutstylesheet.css\' type=\'text/css\'><div id=\'content\'>' ; $sql = "SELECT COUNT(*) FROM user WHERE username='".mysql_real_escape_string($_COOKIE['kurukouser'])."'"; $query = mysql_query($sql) or die(mysql_error()); $r = mysql_fetch_row($query); if($r[0] == 1){ $username = htmlentities($_COOKIE['kurukouser'],ENT_QUOTES,"utf-8"); echo '<center>' ; $sql = "SELECT * FROM companion "; $result = mysql_query($sql) ; $companion = $_GET['companion'] ; if(!isset($companion)) { echo'Hey '.$username.' welcome to companion rocks, here you can choose what you want you companion to be during your journeys, <br />of course this is optional but the benefits can be great!' ; while($row=mysql_fetch_array($result)) { $cname = $row['name'] ; $cimage = $row['image'] ; echo'<br /><br />' ; echo'<img src="'.$cimage.'">' ; echo'<br />' ; echo $cname ; echo'<br />' ; echo'<a href="http://kurukolands.co.uk/companion.php?companion='.$cname.'">Take</a>' ; } }elseif(isset($companion)) { $sql = "SELECT * FROM companion" ; $result = mysql_query($sql) ; while($row=mysql_fetch_array($result)) { if($row['name'] == $companion) { $cimage = $row['image'] ; $cdescription = $row['description'] ; echo $companion ; echo'<br /><br /><img src="'.$cimage.'">' ; echo'<br /><br />'.$cdescription.'' ; echo'<form method="post" action="companion.php"><br />Name: <input type="text" name="name"><br /><input type="submit" name="submit" value="Submit">' ; if(isset($_POST['Submit'])) { $name = $_POST['name'] ; $food = 0 ; $energy = 0 ; mysql_query("INSERT INTO usercompanion(username, name, food, energy) VALUES ('$username', '$name', '$food', '$energy')") ; } } } } }else{ echo'Error: You must be logged in to view this page.' ; } echo'</div></center>' ; ?> Quote Link to comment https://forums.phpfreaks.com/topic/135949-solved-submit-button-help-needed/#findComment-708716 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.