...

View Full Version : Updating Answer



MHaris
08-16-2007, 09:43 AM
candidates_answer.php

<?php

// Last Modified 15th August 2007

include('../config/db.php'); // Includes Database
include('../lib/functions.php'); // Load all functions from Library

include('templates/select_candidates.php'); // Select 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'"); // Query for linking table

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

};

mysql_close(); // Closes DB connection

?>

select_candidate.php

<?php

// Last Modified 14th August 2007

$sql = mysql_query('SELECT name FROM candidates');
?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="get">
<select name="candidates">

<?php
while($row = mysql_fetch_array($sql)){
?>

<option><?php echo $row['name']; ?></option>

<?php

}
?>

</select>
<input type="submit" value="Find Candidate">
</form>

Above code is used to get the answer of the particular question from the answers table. Answer is in boolean form that is true(1) or false(2). I want to add more functionality to the script, how would I go and add a change opinion button to each answer. On click change the answer of the particular question to 1 if it's 2 and change it to 2 if it's 1?

pemcconnell
08-16-2007, 04:26 PM
if your site has a member system, and assuming your database of answers has a memberid slot in it (which it probibly should so that a user can't enter answers more than once), simply extract the users id (eg. $userid), and run a sql update on the answers table WHERE the user id is (eg. $userid)

MHaris
08-16-2007, 04:51 PM
if your site has a member system, and assuming your database of answers has a memberid slot in it (which it probibly should so that a user can't enter answers more than once), simply extract the users id (eg. $userid), and run a sql update on the answers table WHERE the user id is (eg. $userid)

Can you elaborate a bit more?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum