alwaysbrasilian Posted March 17, 2009 Share Posted March 17, 2009 Hey guys. Its been almost two days searching the web for script,tutorials, etc. . My problem is that the form posts my data to mysql and then when the next form posts it erases the first and so on. I'm trying to keep all the questions in the same php file while erasing the last. I'm n00b to php and I'ma try picking up XML but i haven't found any good tutorials. any help appreciated, thanks file is question_same.php <form method="post" action="question_same.php"> <input type="submit" value="Begin" name="submit"> </form> <?php $Q01 = @$_POST['Q01']; $Q02 = @$_POST['Q02']; $Q03 = @$_POST['Q03']; $ques = array($Q01, $Q02, $Q03); $Q0X= @$_POST['submit']; if(isset($Q0X)) { if($Q0X == 'Begin') { ?> <h1> Question 1 </h1> <form method="post" action="question_same.php"> <input type="radio" value="x" name="Q01"> wrong <input type="radio" value="o" name="Q01"> right <input type="submit" name="submit"> </form></form> <?php } header("question_same.php"); } $Q01= @$_POST['Q01']; if(isset($Q01)) { if($Q01){ ?> <h1> Question 2 </h1> // <form method="post" action="question_same.php"> <input type="radio" value="x" name="Q02"> wrong <input type="radio" value="o" name="Q02"> right <input type="submit" name="submit"> </form> <?php } header("question_same.php"); } $Q02= @$_POST['Q02']; if(isset($Q02)) { if($Q02){ ?> <h1> Question 3 </h1> <form method="post" action="question_same.php"> <input type="radio" value="x" name="Q03"> wrong <input type="radio" value="o" name="Q03"> right <input type="submit" name="submit"> </form> <?php } header("question_same.php"); } ?> <form method="post" action="question_same.php"> <?php $db_uname = ""; $db_pword = ""; $db = "localhost"; mysql_connect($db, $db_uname, $db_pword) or die("couldnt connect"); mysql_select_db("languages"); //$sql = "UPDATE lesson01 SET question01=replace(question01, '/', '$question01')"; $sql = "UPDATE lesson01 SET Q01='$ques[0]' , Q02='$Q02', Q03='$Q03' WHERE ID = '1' "; mysql_query($sql) or die (mysql_error()); if($Q03) { header("Location: final.php"); } mysql_close(); ?> (edited by kenrbnsn to add tags) Quote Link to comment https://forums.phpfreaks.com/topic/149867-ive-become-stumped-on-my-issue/ Share on other sites More sharing options...
alwaysbrasilian Posted March 17, 2009 Author Share Posted March 17, 2009 bump.. still working on it. still nothin Quote Link to comment https://forums.phpfreaks.com/topic/149867-ive-become-stumped-on-my-issue/#findComment-787225 Share on other sites More sharing options...
sloth456 Posted March 17, 2009 Share Posted March 17, 2009 Shouldn't you be using an INSERT statement to add to your database rather than replacing an existing record? Quote Link to comment https://forums.phpfreaks.com/topic/149867-ive-become-stumped-on-my-issue/#findComment-787235 Share on other sites More sharing options...
alwaysbrasilian Posted March 17, 2009 Author Share Posted March 17, 2009 tried that and i get the same result plus an error "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE ID = '1'' at line 1" Quote Link to comment https://forums.phpfreaks.com/topic/149867-ive-become-stumped-on-my-issue/#findComment-787242 Share on other sites More sharing options...
Maq Posted March 17, 2009 Share Posted March 17, 2009 You were updating the same record over and over again. The reason you got an error for when you tried the INSERT is because you were using a WHERE clause. Try this instead: $sql = "INSERT INTO lesson01 (Q01, Q02, Q03) VALUES ('$ques[0]' , '$Q02', '$Q03')"; Quote Link to comment https://forums.phpfreaks.com/topic/149867-ive-become-stumped-on-my-issue/#findComment-787245 Share on other sites More sharing options...
alwaysbrasilian Posted March 18, 2009 Author Share Posted March 18, 2009 i just finished trying $sql = "INSERT INTO lesson01 (Q01, Q02, Q03) VALUES ('$ques[0]' , '$Q02', '$Q03')"; and it made a entry for every answer. then i tried adding WHERE ID='1' and also nothing Quote Link to comment https://forums.phpfreaks.com/topic/149867-ive-become-stumped-on-my-issue/#findComment-787256 Share on other sites More sharing options...
Maq Posted March 18, 2009 Share Posted March 18, 2009 Why are you adding WHERE id = 1? That's your problem, ID should be an auto-increment datatype in your database. You don't need to include it in your query. If you do, you will be overwriting what you just put in, which is the problem you stated in your original post. My understanding of your problem is that every time someone finishes the quiz or w/e the results are stored in the database. That's what the INSERT will accomplish. Quote Link to comment https://forums.phpfreaks.com/topic/149867-ive-become-stumped-on-my-issue/#findComment-787263 Share on other sites More sharing options...
alwaysbrasilian Posted March 18, 2009 Author Share Posted March 18, 2009 well in my row inside my database i have a structure with the fields ID(for member ids) Q01,Q02,Q03(which is the questions) but when i use "Insert" it assigns the answers for the questions into different IDs Quote Link to comment https://forums.phpfreaks.com/topic/149867-ive-become-stumped-on-my-issue/#findComment-787268 Share on other sites More sharing options...
trq Posted March 18, 2009 Share Posted March 18, 2009 You seriously need to look into database normalization. Your schema is floored. Theres a book in my sig (Hudzilla) that has a chapter on the subject. Quote Link to comment https://forums.phpfreaks.com/topic/149867-ive-become-stumped-on-my-issue/#findComment-787290 Share on other sites More sharing options...
alwaysbrasilian Posted March 18, 2009 Author Share Posted March 18, 2009 Ill deffinetly take a look at hudzill tomorrow. Im beat for today. Thx for the help so far and I'll appreciate any more insight on the subject. Till tomorrow Quote Link to comment https://forums.phpfreaks.com/topic/149867-ive-become-stumped-on-my-issue/#findComment-787302 Share on other sites More sharing options...
alwaysbrasilian Posted March 18, 2009 Author Share Posted March 18, 2009 hey i read the hudzilla information about normalisation but ive already built a manageable database where i plan to have a table for user info and tables for lessons01 - 20 and inside each lesson they'll be about 10+ with an ID number to keep track of user stats. Quote Link to comment https://forums.phpfreaks.com/topic/149867-ive-become-stumped-on-my-issue/#findComment-787625 Share on other sites More sharing options...
alwaysbrasilian Posted March 18, 2009 Author Share Posted March 18, 2009 BUMP.. i got an idea but i think its a little cumbersome to maintain(i'ma go try it out). Still need help for multiple forms on a single row of info on my database. Quote Link to comment https://forums.phpfreaks.com/topic/149867-ive-become-stumped-on-my-issue/#findComment-787813 Share on other sites More sharing options...
Maq Posted March 18, 2009 Share Posted March 18, 2009 Have you implemented our suggestions? If so, can you post your current code? Quote Link to comment https://forums.phpfreaks.com/topic/149867-ive-become-stumped-on-my-issue/#findComment-787816 Share on other sites More sharing options...
alwaysbrasilian Posted March 18, 2009 Author Share Posted March 18, 2009 yea i tried them and then i tried my idea but no luck. i was planning on doing first question with a form and post but it seems that i would have to do that for every one. If something should change it wouldn't be fun. I'm still with the same code. Quote Link to comment https://forums.phpfreaks.com/topic/149867-ive-become-stumped-on-my-issue/#findComment-788011 Share on other sites More sharing options...
jlavik Posted March 18, 2009 Share Posted March 18, 2009 $sql = "UPDATE lesson01 SET Q01='$ques[0]' , Q02='$Q02', Q03='$Q03' WHERE ID = '1' "; (edited by kenrbnsn to add tags) I'm quite green myself, but even I could see that an UPDATE ... WHERE ID = '1' is going to keep rewriting the same record. You do need INSERT as others have said, but if your ID is not an auto-increment, you'd want to add that field to your table as well, I would think. I'm guessing it would be something like: $sql = "INSERT INTO lesson01 (Q01, Q02, Q03, ID) VALUES ('$ques[0]' , '$Q02', '$Q03', $User_ID)"; W3schools has a great free online introductory course for SQL basics at http://www.w3schools.com/sql/default.asp Quote Link to comment https://forums.phpfreaks.com/topic/149867-ive-become-stumped-on-my-issue/#findComment-788087 Share on other sites More sharing options...
Maq Posted March 18, 2009 Share Posted March 18, 2009 You should already have an auto-incremented id in your table (usually the primary key) so your records always have a unique identifier. If you have one then you don't even need to include it in the INSERT statement, hence the name auto-increment. Quote Link to comment https://forums.phpfreaks.com/topic/149867-ive-become-stumped-on-my-issue/#findComment-788091 Share on other sites More sharing options...
alwaysbrasilian Posted March 18, 2009 Author Share Posted March 18, 2009 i just tried what jlavik posted but i get the same thing.. it post the first question in database but then once the second question is submitted the first is erased. Quote Link to comment https://forums.phpfreaks.com/topic/149867-ive-become-stumped-on-my-issue/#findComment-788122 Share on other sites More sharing options...
trq Posted March 19, 2009 Share Posted March 19, 2009 Post your current code. Quote Link to comment https://forums.phpfreaks.com/topic/149867-ive-become-stumped-on-my-issue/#findComment-788150 Share on other sites More sharing options...
alwaysbrasilian Posted March 19, 2009 Author Share Posted March 19, 2009 its still pretty the same except for the line with UPDATE <form method="post" action="question_same.php"> <input type="submit" value="Begin" name="submit"> </form> <?php $Q01 = @$_POST['Q01']; $Q02 = @$_POST['Q02']; $Q03 = @$_POST['Q03']; //$ques = array($Q01, $Q02, $Q03); $Q0X= @$_POST['submit']; if(isset($Q0X)) { if($Q0X == 'Begin') { ?> <h1> Question 1 </h1> <form method="post" action="question_same.php"> <input type="radio" value="x" name="Q01"> wrong <input type="radio" value="o" name="Q01"> right <input type="submit" name="submit"> </form></form> <?php } header("question_same.php"); } $Q01= @$_POST['Q01']; if(isset($Q01)) { if($Q01){ ?> <h1> Question 2 </h1> <form method="post" action="question_same.php"> <input type="radio" value="x" name="Q02"> wrong <input type="radio" value="o" name="Q02"> right <input type="submit" name="submit"> </form> <?php } header("question_same.php"); } $Q02= @$_POST['Q02']; if(isset($Q02)) { if($Q02){ ?> <h1> Question 3 </h1> <form method="post" action="question_same.php"> <input type="radio" value="x" name="Q03"> wrong <input type="radio" value="o" name="Q03"> right <input type="submit" name="submit"> </form> <?php } header("question_same.php"); } ?> <form method="post" action="question_same.php"> <?PHP $db_uname = "root"; $db_pword = "woopwoop"; $db = "localhost"; mysql_connect($db, $db_uname, $db_pword) or die("couldnt connect"); mysql_select_db("languages"); //$sql = "UPDATE lesson01 SET question01=replace(question01, '/', '$question01')"; $sql = "UPDATE lesson01 SET Q01='$Q01', Q02='$Q02', Q03='$Q03' Where ID='1' "; mysql_query($sql) or die (mysql_error()); if($Q03) { header("Location: final.php"); } mysql_close(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/149867-ive-become-stumped-on-my-issue/#findComment-788153 Share on other sites More sharing options...
alwaysbrasilian Posted March 19, 2009 Author Share Posted March 19, 2009 this is what i have so far after a few mods but the questions are on different .php files and i would like them all to be in one(since i plan to have 10 questions at a time) and then i would show a result which is the file final.php. Also like this my program updates my database correctly(unlike before). question01.php .. <body> <h1> Question 1 </h1> <form method="post" action="question01.php"> <input type="radio" value="x" name="Q01"> wrong <input type="radio" value="o" name="Q01"> right <input type="submit" name="submit"> </form> </body> </html> <?PHP $Q01 = @$_POST['Q01']; if(isset($Q01)) { include('config.php'); //$sql = "UPDATE lesson01 SET Q01=replace(Q01, '/', '$Q01') WHERE ID='1' "; $sql = "UPDATE lesson01 SET Q01='$Q01' WHERE ID='1' "; mysql_query($sql) or die (mysql_error()); header("Location: question02.php"); mysql_close(); } ?> question02.php .. <body> <h1> Question 2 </h1> <form method="post" action="question02.php"> <input type="radio" value="x" name="Q02"> wrong <input type="radio" value="o" name="Q02"> right <input type="submit" name="submit"> </form> </body> </html> <?PHP $Q02 = @$_POST['Q02']; if(isset($Q02)) { include('config.php'); //$sql = "UPDATE lesson01 SET question01=replace(question01, '/', '$question01')"; $sql = "UPDATE lesson01 SET Q02='$Q02' WHERE ID='1' "; mysql_query($sql) or die (mysql_error()); header("Location: final.php"); mysql_close(); } ?> final.php .. <body> <h1> Wrong and right</h1> </body> </html> <?php include('config.php'); $sql = "SELECT * FROM lesson01 WHERE ID='1' "; $result = mysql_query($sql); if($result) { if(mysql_num_rows($result) == 1) { $data = mysql_fetch_assoc($result); $first = $data['Q01']; $second= $data['Q02']; $thrid = $data['Q03']; } else { print "sorry"; } } echo $first; echo " "; echo $second; print "<a href= 'question01.php'> Go to page 1</a>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/149867-ive-become-stumped-on-my-issue/#findComment-788161 Share on other sites More sharing options...
alwaysbrasilian Posted March 19, 2009 Author Share Posted March 19, 2009 bump bump.. Quote Link to comment https://forums.phpfreaks.com/topic/149867-ive-become-stumped-on-my-issue/#findComment-788470 Share on other sites More sharing options...
trq Posted March 19, 2009 Share Posted March 19, 2009 Firstly, post code in tags. Have you thought about the fact that your using an update statement to update a record will overide the existing record? I see you also simply ignored my suggestion of database normalization to continue with this poor design. Quote Link to comment https://forums.phpfreaks.com/topic/149867-ive-become-stumped-on-my-issue/#findComment-788475 Share on other sites More sharing options...
alwaysbrasilian Posted March 19, 2009 Author Share Posted March 19, 2009 sorry about the tags.. well i plan to have user info in one table and then each leasson would be in another table and inside each lesson table there would be a series of questions.. i think that sound pretty normalized. still a noob(its been like 2wks learning php, css, mysql and html(again)) Quote Link to comment https://forums.phpfreaks.com/topic/149867-ive-become-stumped-on-my-issue/#findComment-788551 Share on other sites More sharing options...
alwaysbrasilian Posted March 19, 2009 Author Share Posted March 19, 2009 bump from page 2 Quote Link to comment https://forums.phpfreaks.com/topic/149867-ive-become-stumped-on-my-issue/#findComment-788888 Share on other sites More sharing options...
trq Posted March 19, 2009 Share Posted March 19, 2009 Stop bumping this thread. The problem is your using an update statement which will update the record in question. Read the previous replies. Quote Link to comment https://forums.phpfreaks.com/topic/149867-ive-become-stumped-on-my-issue/#findComment-788898 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.