0

The user submits the response to the poll it's not displaying the results from the database. This is the website http://josietaylor.byethost14.com/poll/ I'm unsure what's wrong and why nothing is showing? I didn't think I needed to add the HOST USER PASS BASE but I included it to see if it would work, still won't.

  • After submitting the answer it's just blank, nothing at all is showing. Below is the code. It should show on the same page just reload it.

*** Update Thank you to Ken Lee, I made a rookie mistake of not including the 'includes' file when uploading to my server. I fixed that, and now it's showing the "results" page but not actually displaying the results. I want it to show the different options and the results with the % from highest to lowest. This database isn't supposed to be safe as it's a project for college (first PHP/SQL class) and we aren't yet making it all secure.

<?php
    include 'includes/dp.php';

    //Function to create the page
    function createPage(){
       
        if(!isset($_POST['food'])){
            echo createQuestionare();
        }

        //If all variables are set, add to database and display results
        else{
            addToDataBase($_POST['food'], 'poll');
            displayResults();
        }
    }
    define("HOST", "****");
    define("USER", "****");
    define("PASS", "****");
    define("BASE", "****");

    $conn = mysqli_connect(HOST, USER, PASS, BASE);

    //Create questionare
    function createQuestionare(){
        
        $content = "";
        $content .= "<div class='main'>";
        $content .= "<h1 class='title'>Food Poll</h1>";
        $content .= "<form action='.' method='post'>";

        $content .= createQuestion();
        
        //Close form
        $content .= "<input type='submit'>";
        $content .= "</form>";
        $content .= "</div>";

        return $content;
    }


    //Create question
    function createQuestion(){
        $arr = ["Pizza", "Burger", "Salad", "Pasta"];

        //Question to ask
        $content = "";
        $content .= "<h1 class='question-text'>Which food is most satisfying?</h1>";

        //Create radio button and label for each possible question
        foreach($arr as $subject){
            $content .= "<input type='radio' id='$subject' value='$subject' name='food'>";
            $content .= "<label for='$subject'>$subject</label><br>";
        }

        
        return $content;
    }


    //Function adds data to DB 
    function addToDataBase($data, $DBName){
        
        //Edit string to be lowercase
        $data = strtolower($data);
        $conn = connectToDB();

        //Check database for primary key of answer 
        $sql = "SELECT * FROM $DBName WHERE name='$data';";
        $results = mysqli_query($conn, $sql);

        if(mysqli_num_rows($results) != 0){
            $key = mysqli_fetch_array($results, MYSQLI_ASSOC)['id'];
        }

        //Increment vote number and insert value
        $sql = "UPDATE $DBName SET votes = votes + 1 WHERE id=$key;";
        mysqli_query($conn, $sql);
        
        mysqli_close($conn);
    }

    //Function to display results
    function displayResults(){

        $arr = ['poll'];

        //Create results content
        $content = '';
        $content = '<div class="main">';
        $content .= "<h1 class='title'>Thank You!</h1>";
        
        foreach($arr as $DBName){
            $content .= '<div class="result-container">';
            $content .= getResults($DBName);
            $content .= '</div>';
        }
        $content .= '</div>';
        echo $content;
    }

    //Function will display results highest to lowest
    function getResults($DBName){
        $conn = connectToDB();

        //Results
        $sql = "SELECT * FROM $DBName;";
        $results = mysqli_query($conn, $sql);

        //Total
        $sql = "SELECT SUM(votes) as total FROM $DBName;";
        $total = mysqli_query($conn,$sql);
        $total = mysqli_fetch_assoc($total)['total'];


        //Create an associate array with percentage and name
        $sortedArray = array();
        while($row = mysqli_fetch_array($results, MYSQLI_ASSOC)){
            $name = $row['name'];
            $percentage = round($row['votes']/$total * 100);
            $sortedArray[$name] = $percentage; 
        }

        //Sort by percentage
        $content = '';
        $content = '<h1 class="result-text">Results</h1>';
        arsort($sortedArray);

        //Display results
        foreach($sortedArray as $name => $percentage  ){
            $content .= "<h2>". ucwords($name) ." has $percentage% of the votes</h2>";
        }

        mysqli_close($conn);
        
        return $content;
    }
?>
<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>Poll</title>
    <link rel="stylesheet" href="css/style.css">
</head>
<body>
<?php 
    createPage();
?>
</body>
</html>
Josie D
  • 9
  • 2
  • You have an error somewhere add `ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);` to the top of your file and you'll se whatever is wrong – Breezer Oct 31 '22 at 06:08
  • IMPORTANT: do not share your database credentials here even if it is just a staging server. – Anuj Shrestha Oct 31 '22 at 06:21
  • **Are you sure** that the file 'includes/dp.php' EXISTS (and contains the function connectToDB() which is 100% proper for db connection) ? – Ken Lee Oct 31 '22 at 07:21
  • 1) Check your PHP Error logs ([**how?**](https://stackoverflow.com/questions/12834583/where-can-i-find-error-log-files-for-php)). 2) **STOP** using `DEFINE` for setting constants that you are only ever going to use once. Simply use standard variables you can unset once used (for db connection) – Martin Oct 31 '22 at 13:20
  • And you need to stop SQL injection on your script, what happens if one of the HTML inputs is `` ? You currently do abolsutely NO checking the data received from the client is safe (pssst, it won't be safe); [**READ HERE**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) to learn how to fix this. – Martin Oct 31 '22 at 13:34
  • Thank you to Ken Lee, I made a rookie mistake of not including the 'includes' file when uploading to my server. I fixed that, and now it's showing the "results" page but not actually displaying the results. I want it to show the different options and the results with the % from highest to lowest. This database isn't supposed to be safe as it's a project for college (first PHP/SQL class) and we aren't yet making it all secure. – Josie D Nov 02 '22 at 05:07

2 Answers2

1

Your POST request produces 500 error.

Check the error_reporting, display_errors and display_startup_errors settings in your php.ini file. They should be set to E_ALL and "On" respectively or change through code as follows. This will display the actual error produced. That will give insight on what goes wrong.

error_reporting(E_ALL);
ini_set('display_errors', 'On');
  • It would be better to educate the OP about how to display errors in their error log file rather than having errors on show to the general public on the page. – Martin Oct 31 '22 at 14:14
1

Add the following on the top of your code.

ini_set('display_errors', 1); 
ini_set('display_startup_errors', 1); 
error_reporting(E_ALL); 

Always, use the try-catch exception method. Exceptions are used to change the normal flow of a script if a specified error occurs.

Moreover, you can always check apache2 error logs from where you will get error logs based on timestamps.

Shashank Shah
  • 2,077
  • 4
  • 22
  • 46
  • 1
    re PHP Errors; It would be better to educate the OP about how to display errors in the error log file rather than having errors on show to the general public upon page load. – Martin Oct 31 '22 at 14:15
  • Agreed @Martin its always good to monitor the apache error log files. – Shashank Shah Nov 01 '22 at 09:15