Jump to content

values not being entered into table


woodplease

Recommended Posts

hi. I;ve created a form, so that when a user enters data into it, it gets added to a table in a database. the form submits some data to one table, and other data to another table. my problem is that the data only gets added to the first table, but data doesnt enter correnctly into the other one.

<?PHP

$questionno = $_POST['questionno'];
$question = $_POST['question'];
$quizref = $_POST['quizref'];
$answerno = $_POST['answerno'];
$answer = $_POST['answer'];
$answervalue = $_POST['answervalue'];

pg_connect("host=database.*******.uk
port=5432 dbname=***** user=***** password=******");
?>

<html>
<head>
<title>Add </title>
</head>
<body>

<?php

$query="INSERT INTO questions (questionno, question, quizref)VALUES ('".$questionno."', '".$question."', '".$quizref."')";

pg_query($query) or die ('Error adding new question');

echo "The question has been added to the quiz";
$queryanswers="INSERT INTO answers (answerno, answer, answervalue, questionno, quizref)VALUES ('".$answerno."', '".$answer."','".$answervalue."', '".$questionno."', '".$quizref."')";

pg_query($queryanswers) or die ('Error adding new answers');

echo "The answers has been added to the question";
?>
<form method="post" action="">

quizref: <br/>
<input type="text" name ="quizref" size="5" /><br/>

Question Number: <br/>
<input type="text" name="questionno" size="5" /><br/>

Question: <br/>
<input type="text" name="question" size="60" /><br/>

Answers<br/><br/>

Answer number:<br/>
<input type="text" name ="answerno" size="5" /><br/>

Answer<br/>
<input type="text" name ="answer" size="60" /><br/>

Answer value<br/>
<input type="text" name ="answervalue" size="5" /><br/>
<br/><br/>
Answer number:<br/>
<input type="text" name ="answerno" size="5" /><br/>

Answer<br/>
<input type="text" name ="answer" size="60" /><br/>

Answer value<br/>
<input type="text" name ="answervalue" size="5" /><br/>
<br/><br/>
Answer number:<br/>
<input type="text" name ="answerno" size="5" /><br/>

Answer<br/>
<input type="text" name ="answer" size="60" /><br/>

Answer value<br/>
<input type="text" name ="answervalue" size="5" /><br/>
<br/><br/>
Answer number:<br/>
<input type="text" name ="answerno" size="5" /><br/>

Answer<br/>
<input type="text" name ="answer" size="60" /><br/>

Answer value<br/>
<input type="text" name ="answervalue" size="5" /><br/>
<br/><br/>

<input type="submit" value="Send and Add another Add Questions" />
<a href="addfinish.php"><input type="submit" value="Add and finish" /></a>

</form>

</body>
</html>

 

Any help would be great.

 

thanks

Link to comment
Share on other sites

is that good or bad?  i don't feel like deciphering your question/statement.

 

what exactly do you want each query to do?

 

ex. i would like variables x,y,z to go into query #1 and ONLY query #1.

 

i would then like ONLY variables a, b, c to go into query #2, etc.

Link to comment
Share on other sites

i dont get any error messages coming up. the form is supposed to add one question to the question table, and four answers to the answer table, hence why some code is copied and pasted several times. the question gets added fine, but when it comes to the answers only the last answer is added to the table, i.e answerno:4 answer:answer 4 and value: 1. There should have been 3 other ansers added to the table aswell

Link to comment
Share on other sites

There is the problem :) Of course you are only getting the last answer, you do not have those options set as arrays. How do you expect PHP to know that you have 5 fields named the exact same and how to handle them?

 

You may want to look at this article for a better understanding of what is happening.

Link to comment
Share on other sites

never seen this before:

 

<a href="addfinish.php"><input type="submit" value="Add and finish" /></a>

 

i'm assuming you want that as the form action, so make this change:

 

<form method="post" action="addfinish.php">
...
<input type="submit" name="submit" value="Add and finish" />

 

without the <a> wrapper.

 

EDIT: and surround you queries like so:

 

<?php
if (isset ($_POST['submit']))
{
     $query="INSERT INTO questions (questionno, question, quizref)VALUES ('".$questionno."', '".$question."', '".$quizref."')";

     pg_query($query) or die ('Error adding new question');

     echo "The question has been added to the quiz";
     $queryanswers="INSERT INTO answers (answerno, answer, answervalue, questionno, quizref)VALUES ('".$answerno."', '".$answer."','".$answervalue."', '".$questionno."', '".$quizref."')";

     pg_query($queryanswers) or die ('Error adding new answers');

     echo "The answers has been added to the question";
}
?>

 

the reason you're only getting the last value (which is information that would've been splendid in your first post and not your 4th), is because all of you <input> fields share the same name.

 

what you would want to do is gather each answer into an array by changing the name attribute in the <input>:

 

Answer<br/>
<input type="text" name ="answer[]" size="60" /><br/>

 

do that for each name="answer[]" and you can then access those values as an array in $_POST['answer'], where you can then work the query accordingly.

 

are each of these answers going in as their own record?  so, if three (3) answers are submitted with one (1) question, one (1) record is created in the `question` table while three (3) records are created in the `answers` table, correct?  with `questionno` being the relationship between each question and answer?

Link to comment
Share on other sites

are each of these answers going in as their own record?  so, if three (3) answers are submitted with one (1) question, one (1) record is created in the `question` table while three (3) records are created in the `answers` table, correct?  with `questionno` being the relationship between each question and answer?

 

this is exaactly what i want to happen.

 

i have made the changes you mentioned, so i have changed every "name =answer[]".

 

i'm just not sure what code i would need to insert so that the values are actually entered into the table.

Link to comment
Share on other sites

Personally I would do something like this:

// HTML //
Answer number:<br/>
<input type="text" name ="answerno[1]" size="5" /><br/>

Answer<br/>
<input type="text" name ="answer[1]" size="60" /><br/>

Answer value<br/>
<input type="text" name ="answervalue[1]" size="5" /><br/>
<br/><br/>

Answer number:<br/>
<input type="text" name ="answerno[2]" size="5" /><br/>

Answer<br/>
<input type="text" name ="answer[2]" size="60" /><br/>

Answer value<br/>
<input type="text" name ="answervalue[2]" size="5" /><br/>
<br/><br/>

etc...

then in the PHP:
$value_pairs = array();
foreach ($_POST['answer'] as $key=>$answer) {
$answerno = $_POST['answerno'][$key];
$answervalue = $_POST['answervalue'][$key];
$value_pairs = "('".$answerno."', '".$answer."','".$answervalue."', '".$questionno."', '".$quizref."')";
}

$queryanswers="INSERT INTO answers (answerno, answer, answervalue, questionno, quizref) VALUES " . join(',',$value_pairs);

 

I havent checked the syntax of this but its somewhere for you to start anyway..

Link to comment
Share on other sites

The syntax should be correct, I modified the code quite a bit, first up I am using a for loop to loop through the data. Second up, I am actually checking if POST data is there, this will avoid any notice errors (That you probably were not seeing due to error_reporting level). Third I am defining the values so if one is missing it does not break the script. Finally I am escaping the data as it goes into the database, this will help prevent SQL injection from happening.

 

A short tidbit the ? : seen in the code where I set $question = etc is called the Ternary Operator which acts like a short If / else statement. The code is untested as I do not have a pg database, but should work pending any minor syntax issues that may come up. You will also notice I used trigger_error instead of die if the query fails, this is preferred as the error will be logged to the log where as die does not log errors.

 

You will also notice I moved all the PHP stuff above the HTML output and put the output from the script in a string called "output". This is to avoid header errors later on and it is often better practice to output your stuff at the end of processing and not during. If you have questions regarding the code let me know.

 

<?php
pg_connect("host=database.*******.uk
port=5432 dbname=***** user=***** password=******");

$output = "";
if (isset($_POST['questionno']) && (isset($_POST['answerno']) && is_array($_POST['answerno']))) { // make sure that we have both sections, just in case.
// first lets insert the question data:
$questionno = isset($_POST['questionno'][$i]) ? pg_escape_string($_POST['questionno']) : ''; // ternary operator (? : ) acts as a short if/else. If isset set the variable to it after escaping it, else set it to ''
$question = isset($_POST['question'][$i]) ? pg_escape_string($_POST['question']) : '';
$quizref = isset($_POST['quizref'][$i]) ? pg_escape_string($_POST['quizref']) : '';

$query="INSERT INTO questions (questionno, question, quizref) VALUES ('".$questionno."', '".$question."', '".$quizref."')";
pg_query($query) or trigger_error ('Error adding new question: ' . pg_last_error());

$output .= "Question has been added succesfully to the quiz reference {$quizref}<br />";

$cnt = count($_POST['answerno']);
for ($i=0; $i < $cnt; $i++) {
	// first define variables to avoid notice errors and escape the data:
	$answerno = isset($_POST['answerno'][$i]) ? pg_escape_string($_POST['answerno'][$i]) : null; 
	$answer = isset($_POST['answer'][$i]) ? pg_escape_string($_POST['answer'][$i]) : null;
	$answervalue = isset($_POST['answervalue'][$i]) ? pg_escape_string($_POST['answervalue'][$i]) : null;

	// check if all values are null, if they are skip the insert. 
	if (is_null($answer) && is_null($answerno) && is_null($answervalue)) 
		continue; // continue the loop as there was no data entered there to insert.

	$queryanswers="INSERT INTO answers (answerno, answer, answervalue, questionno, quizref)VALUES ('".$answerno."', '".$answer."','".$answervalue."', '".$questionno."', '".$quizref."')";

	pg_query($queryanswers) or trigger_error ('Error adding new answers: ' . pg_last_error());
}

$output .= "The answers have been succesfully added to the quiz reference {$quizref}.<br />";
}
?>
<html>
<head>
<title>Add </title>
</head>
<body>
<?php echo $output; ?>

<form method="post" action="">

quizref: <br/>
<input type="text" name ="quizref" size="5" /><br/>

Question Number: <br/>
<input type="text" name="questionno" size="5" /><br/>

Question: <br/>
<input type="text" name="question" size="60" /><br/>

Answers<br/><br/>

Answer number:<br/>
<input type="text" name ="answerno[]" size="5" /><br/>

Answer<br/>
<input type="text" name ="answer[]" size="60" /><br/>

Answer value<br/>
<input type="text" name ="answervalue[]" size="5" /><br/>
<br/><br/>
Answer number:<br/>
<input type="text" name ="answerno[]" size="5" /><br/>

Answer<br/>
<input type="text" name ="answer[]" size="60" /><br/>

Answer value<br/>
<input type="text" name ="answervalue[]" size="5" /><br/>
<br/><br/>
Answer number:<br/>
<input type="text" name ="answerno[]" size="5" /><br/>

Answer<br/>
<input type="text" name ="answer[]" size="60" /><br/>

Answer value<br/>
<input type="text" name ="answervalue[]" size="5" /><br/>
<br/><br/>
Answer number:<br/>
<input type="text" name ="answerno[]" size="5" /><br/>

Answer<br/>
<input type="text" name ="answer[]" size="60" /><br/>

Answer value<br/>
<input type="text" name ="answervalue[]" size="5" /><br/>
<br/><br/>

<input type="submit" value="Send and Add another Add Questions" />
<a href="addfinish.php"><input type="submit" value="Add and finish" /></a>

</form>

</body>
</html>

 

EDIT:

Fixed a syntax issue in the if, also removed the question array element as the questions are not arrays.

Link to comment
Share on other sites

This may not be perfect but yeah, im trying to get my dishes done before the gf yells at me again :P

 

<html>
<head>
<title>Add</title>
</head>
<body>

<?php

function createForm($message = '') {
$form = '<form method="post" action="woodplease.php">

Quiz Ref: <br/>
<input type="text" name="quizref" size="5" /><br/>

Question Number: <br/>
<input type="text" name="questionno" size="5" /><br/>

Question: <br/>
<input type="text" name="question" size="60" /><br/>

Answers<br/><br/>';

for ($i=0;$i<4;$i++) {
	$form .= 'Answer number:<br/>
	<input type="text" name="answer['.$i.'][no]" size="5" /><br/>

	Answer<br/>
	<input type="text" name="answer['.$i.'][answer]" size="60" /><br/>

	Answer value<br/>
	<input type="text" name="answer['.$i.'][value]" size="5" /><br/>
	<br/><br/>';
}

$form .= '
<span style="color:red">'.$message.'</span><br/><br/>
<input type="submit" value="Save and Add another Question" name="add[more]" />
<input type="submit" value="Save and Finish" name="add[finish]" />
</form>';

return $form;
}
$message = '';
// Make sure the Form was posted //
if (isset($_POST['add'])) {

// Get the Question $_POST variables //
$questionno = $_POST['questionno'];
$question = $_POST['question'];
$quizref = $_POST['quizref'];

$query = "INSERT INTO questions (questionno, question, quizref)VALUES ('".$questionno."', '".$question."', '".$quizref."')";
// Make sure the questions query executes correctly. Else write an error message;
if (pg_query($query)) {
	$pairs = array();
	// Loop through all the answers //
	foreach ($_POST['answer'] as $ans) {
		$answerno = $ans['no'];
		$answer = $ans['answer'];
		$answervalue = $ans['value'];
		$pairs[] = "('".$answerno."', '".$answer."','".$answervalue."', '".$questionno."', '".$quizref."')";
	}
	$queryanswers="INSERT INTO answers (answerno, answer, answervalue, questionno, quizref) VALUES " . join(',',$pairs);
	if (pg_query($queryanswers)) {
		// If the code reaches this point it has all been a success so we need to determine the action taken by the user //
		if (isset($_POST['add']['finish'])) {
			// Do not draw the form as they chose the 'Save and Finish' button so give them a finished message //
			echo 'You have completed entering questions and answers successfully.';
		} else {
			// They selected the 'Save and Add another Question' so give them the form again with a message telling them of the previous adding success //
			print(createForm('Question added successfully<br/>Answers added successfully.'));
		}
	} else {
		print(createForm('Error adding answers to previous question'));
	}
} else {
	print(createForm('Error adding a previous Question and answers'));
}
} else {
print(createForm());
}

?>

</body>
</html>

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.