Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    60
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Database change problem

    PHP Code:
    <?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($sqlMYSQL_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
    PHP Code:
    <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?

  • #2
    Regular Coder
    Join Date
    Jul 2007
    Location
    UK
    Posts
    223
    Thanks
    0
    Thanked 14 Times in 14 Posts
    What happens if you move the include from below to above this line:
    PHP Code:
    $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 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •