Jump to content

Recommended Posts

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)

Link to comment
https://forums.phpfreaks.com/topic/149867-ive-become-stumped-on-my-issue/
Share on other sites

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')";   

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.

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.

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.

$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

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.

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();

 

?>

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>";

?>

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))

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.