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.
<?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 >'; $content .= "<h1 class='title'>Thank You!</h1>"; foreach($arr as $DBName){ $content .= '<div >'; $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 >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>
CodePudding user response:
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');
CodePudding user response:
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.