Jump to content

ive become stumped on my issue


alwaysbrasilian

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

?>

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.