...

View Full Version : Database change problem



MHaris
08-17-2007, 01:25 PM
<?php

// Last Modified 16th August 2007

ob_start();
session_start(); // Loads session

include('templates/header.php'); // Header Template
include('../config/config.php'); // Includes configuration files
include('../lib/functions.php'); // Load all functions from Library

if(!isset($_SESSION["username"])){
header('Location: index.php');
exit();
}
else {

include('templates/select_candidates.php'); // Selects candidate template

$selected_candidate = sql_safe($_GET['candidates']); // Gets candidate

echo "<h1>Questions and their answers of the candidate <u>".$selected_candidate."</u></h1>";

$sql = mysql_query("SELECT c.*, q.*, a.* FROM (( answers a LEFT JOIN candidates c ON a.candidate_id = c.id ) LEFT JOIN questions q ON a.question_id = q.id ) WHERE c.name = '$selected_candidate'") or die(mysql_error()); // Query for linking table
include('templates/change_answer.php'); // Selects modify answer template

while($row = mysql_fetch_array($sql, MYSQL_BOTH)){
$candidate_id = $row['candidate_id'];
$question = $row['question'];
$qID = $row['question_id'];
$answer = $row['answer'];
if($answer == 1){
$answer = "For";
}
else{
$answer = 'Against';
}
echo $question." - <b><u>Current Opinion:</u> ".$answer."</b><br/>";
};

mysql_close(); // Closes DB connection
}

include('templates/footer.php'); // Footer Template

?>

change_answer.php

<form method="post" action="<?php $_SERVER['PHP_SELF'] ?>">
Change the answer to
<select name="changeanswer_opinion">
<option>For</option>
<option>Against</option>
</select>
for the question
<select name="changeanswer_question">
<?php

$get_question_id = mysql_query("SELECT c.*, q.*, a.* FROM (( answers a LEFT JOIN candidates c ON a.candidate_id = c.id ) LEFT JOIN questions q ON a.question_id = q.id ) WHERE c.name = '$selected_candidate'") or die(mysql_error()); // Query for linking tables

while($row = mysql_fetch_array($get_question_id)){
$candidate_id = $row['candidate_id'];
$changeanswer_questions = $row['question_id'];
?>
<option><?php echo $changeanswer_questions; ?></option>
<?php
}
?>

</select>
<input type="submit" name="change_opinion">
</form>

<?php

$change_opinion = $_POST['change_opinion'];
$changeanswer_opinion = $_POST['changeanswer_opinion'];
if($changeanswer_opinion == "For"){
$changeanswer_opinion = 1;
}
else {
$changeanswer_opinion = 2;
}

$changeanswer_question = $_POST['changeanswer_question'];
$changeanswer_candidate = $candidate_id;

if(isset($change_opinion)){
mysql_query("UPDATE answers SET answer='$changeanswer_opinion' WHERE question_id='$changeanswer_question' AND candidate_id='$changeanswer_candidate'") or die(mysql_error());
}

?>

After changing the answer to either For or Against, I have to reload the browser to see the database change. How can I overcome this problem?

mcjwb
08-17-2007, 02:12 PM
What happens if you move the include from below to above this line:

$sql = mysql_query("SELECT c.*, q.*, a.* FROM (( answers a LEFT JOIN candidates c ON a.candidate_id = c.id ) LEFT JOIN questions q ON a.question_id = q.id ) WHERE c.name = '$selected_candidate'") or die(mysql_error()); // Query for linking table



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum