PHP validation not working

Hi there, i have made a system where an admin can login and create a poll, the problem is even when no data is entered the poll is still being created, i want to prevent this. I have tried using if(empty but it doesn’t seem to work, any help would be appreciated. I apologise if this is not formatted correctly.

<?php
 include 'functions.php';
 $pdo = pdo_connect_mysql();
 $msg = '';

 // Check if POST data is not empty
 if (!empty($_POST)) {
     // Post data not empty insert a new record
     // Check if POST variable "title" exists, if not default the value to blank, basically the same for all variables
     $title = isset($_POST['title']) ? $_POST['title'] : '';
     $desc = isset($_POST['desc']) ? $_POST['desc'] : '';
     // Insert new record into the "polls" table
     $stmt = $pdo->prepare('INSERT INTO polls VALUES (NULL, ?, ?)');
     $stmt->execute([$title, $desc]);
     // Below will get the last insert ID, this will be the poll id
     $poll_id = $pdo->lastInsertId();

     // Get the answers and convert the multiline string to an array, so we can add each answer to the "poll_answers" table
     $answers = isset($_POST['answers']) ? explode(PHP_EOL, $_POST['answers']) : '';
     foreach ($answers as $answer) {
         // If the answer is empty there is no need to insert
         if (empty($answer)) {
             echo 'no entry';
         } else {
             echo 'continue';
         }
         // Add answer to the "poll_answers" table
         $stmt = $pdo->prepare('INSERT INTO poll_answers VALUES (NULL, ?, ?, 0)');
         $stmt->execute([$poll_id, $answer]);
     }
     // Output message
     $msg = 'Created Successfully!';
 }
 ?>

Just check to see if the array is empty BEFORE trying to add the data.

Hello, thank you for your response, is it possible for you to go further with what you mean? Do i need to check further down the page? thankyou.

Just put null in the answers spots if the array is blank.

I have a quiz that I made and with I use javascript and FETCH, the principle is the same. When I generate the questions and answers I just do the following in javascript:

/* Populate Question, Create Answer Buttons */
const createQuiz = (gameData) => {

/*
 * The Element interface's scrollIntoView() method scrolls the element's
 * parent container such that the element on which 
 * scrollIntoView() is called is visible to the user
 */
d.getElementById('mainGame').scrollIntoView();

startTimer(dSec);

question.textContent = gameData.question;

/*
 * Create Buttons then insert answers into buttons that were
 * create. 
 */
gameData.answers.forEach((value, index) => {
    /*
     * Don't Show Answers that have a Blank Field
     */
    if (value !== "") {
        var gameButton = buttonContainer.appendChild(d.createElement('button'));
        gameButton.id = 'answer' + (index + 1);
        gameButton.className = 'answerButton';
        gameButton.setAttribute('data-correct', (index + 1));
        gameButton.addEventListener('click', clickHandler, false);
        gameButton.appendChild(d.createTextNode(value));
    }
});
};

Granted yours would be different as I take it would be the whole array and you have MySQL database table setup up different. However, you can do it in PHP, but if you want realtime results you’ll need to use javascript/Ajax or NodeJS. (Among many other ways)

Sponsor our Newsletter | Privacy Policy | Terms of Service