Saturday, 2 March 2019

WHY does the array execution in the function below throw an exception?

I'm working on a ussd voting application and Ive got everything right except for saving the votes to the database and updating the table.

The function that handles the save_vote is per below;

function save_vote($phone_number, $voted_for) {
        // Just the digits, please
        $phone_number = preg_replace('/\D/', '', $phone_number);

        // Check to see if person has already voted
        $stmt = $this->db->prepare('SELECT COUNT(*) FROM voters WHERE phone_number=?');
        $stmt->execute(array($phone_number));

        // If not, save their vote
        if ($stmt->fetchColumn() == 0)
        {
            // Save voter
            $stmt = $this->db->prepare('INSERT INTO voters (phone_number, voted_for) VALUES (?, ?)');
            $stmt->execute(array($phone_number, $voted_for));

            // Update vote count
            $stmt = $this->db->prepare('UPDATE brands SET votes = votes + 1 WHERE id=?');
            $stmt->execute(array($voted_for));

            return 'Thank you, your vote has been recorded';
        }
        else {
            return 'Sorry, you can only vote once.';
        }
    }

The values being passed into the function are DB->save_vote('02778995805', 2)

The server logs throws an exception as follows

1 {main}

2019-02-28T13:50:49.813224+00:00 app[web.1]: thrown in /app/db.php on line 57

and line 57 is where the code $stmt->execute(array($phone_number)); is at.

Will appreciate some help explaining what could be going wrong.

Thanks

As Requested from the comments, Please see below

db.php code:

<?php
/**
 * Created by PhpStorm.
 * User: kqwameselase
 * Date: 2019-02-27
 * Time: 22:53
 */
    class DB {
        const DB_NAME = 'votes.sqlite';

    protected $db;

    function __construct() {
        $this->db = new PDO('sqlite:'.self::DB_NAME);
    }

    function init() {
        // Create two tables, one to store the brands being voted on and their vote counts (brands) and one to store the people that have voted (voters).
        $this->db->exec('CREATE TABLE IF NOT EXISTS brands (id INTEGER PRIMARY KEY, name TEXT, votes INTEGER);');
        $this->db->exec('CREATE TABLE IF NOT EXISTS voters (id INTEGER PRIMARY KEY, phone_number TEXT, voted_for INTEGER);');
    }

    function add_brand($name) {
        // Check to make sure the brand name doesn't already exist
        $stmt = $this->db->prepare('SELECT COUNT(*) FROM brands WHERE name=?');
        $stmt->execute(array($name));

        // If not, insert it
        if ($stmt->fetchColumn() == 0)
        {
            $stmt = $this->db->prepare('INSERT INTO brands (name, votes) VALUES (?, 0)');
            $stmt->execute(array($name));
        }
    }

    function get_brands() {
        $result = $this->db->query('SELECT * FROM brands');

        foreach ($result as $row)
        {
            $brand['id'] = $row['id'];
            $brand['name'] = $row['name'];
            $brand['votes'] = $row['votes'];

            $brands[] = $brand;
        }

        return $brands;
    }

    /**
     * @param $phone_number
     * @param $voted_for
     * @return string
     */


function save_vote($phone_number, $voted_for) {
        // Just the digits, please
        $phone_number = intval(preg_replace('/\D/', '', $phone_number));

        // Check to see if person has already voted
        $stmt = $this->db->prepare('SELECT COUNT(*) FROM voters WHERE phone_number=?');
        $stmt->bindParam(1, $phone_number, PDO::PARAM_INT);
        $stmt->execute();

        // If not, save their vote
        if ($stmt->fetchColumn() == 0)
        {
            // Save voter
            $stmt = $this->db->prepare('INSERT INTO voters (phone_number, voted_for) VALUES (?, ?)');
            $stmt->bindParam(1, $phone_number, PDO::PARAM_INT);
            $stmt->bindParam(2, $voted_for, PDO::PARAM_INT);
            $stmt->execute();

            // Update vote count
            $stmt = $this->db->prepare('UPDATE brands SET votes = votes + 1 WHERE id=?');
            $stmt->bindParam(1,$voted_for, PDO::PARAM_INT);
            $stmt->execute();

            return 'Thank you, your vote has been recorded';
        }
        else {
            return 'Sorry, you can only vote once.';
        }
    }
/*        function save_vote($phone_number, $voted_for) {
            // Just the digits, please
            $phone_number = intval(preg_replace('/\D/', '', $phone_number));

        // Check to see if person has already voted
        $stmt = $this->db->prepare('SELECT COUNT(*) FROM voters WHERE phone_number=?');
        $stmt->bindParam('i', $phone_number);
        $stmt->execute();

        // If not, save their vote
        if ($stmt->fetchColumn() == 0)
        {
            // Save voter
            $stmt = $this->db->prepare('INSERT INTO voters (phone_number, voted_for) VALUES (?, ?)');
            $stmt->bindParam('ii', $phone_number, $voted_for); // we suppose tha rhe $voted_for is integer if not use intval
            $stmt->execute();

            // Update vote count
            $stmt = $this->db->prepare('UPDATE brands SET votes = votes + 1 WHERE id=?');
            $stmt->bindParam('i',$voted_for);// we suppose tha rhe $voted_for is integer if not use intval
            $stmt->execute();

            return 'Thank you, your vote has been recorded';
        }
        else {
            return 'Sorry, you can only vote once.';
        }
    }*/

/*        function save_vote($phone_number, $voted_for) {
            // Just the digits, please
            $phone_number = preg_replace('/\D/', '', $phone_number);

        // Check to see if person has already voted
        $stmt = $this->db->prepare('SELECT COUNT(*) FROM voters WHERE phone_number=?');
        $stmt->bind_param(int, $phone_number);
        $stmt->execute();
        //$stmt->execute(array($phone_number));

        // If not, save their vote
        if ($stmt->fetchColumn() == 0)
        {
            // Save voter
            $stmt = $this->db->prepare('INSERT INTO voters (phone_number, voted_for) VALUES (?, ?)');
            $stmt->execute(array($phone_number, $voted_for));

            // Update vote count
            $stmt = $this->db->prepare('UPDATE brands SET votes = votes + 1 WHERE id=?');
            $stmt->execute(array($voted_for));

            return 'Thank you, your vote has been recorded';
        }
        else {
            return 'Sorry, you can only vote once.';
        }
    }*/
}

Here is the vote-now.php code where the useres interract with the system.

<?php
/**
 * Created by PhpStorm.
 * User: kqwameselase
 * Date: 2019-02-27
 * Time: 10:29
 */
date_default_timezone_set('Africa/Ghana');

require_once('db.php');
header('Content-type: application/json; charset=utf-8');
header("Access-Control-Allow-Origin: http://apps.smsgh.com");
header("Access-Control-Allow-Methods: POST, GET, OPTIONS");



// Begin by reading the HTTP request body contents.
// Since we expect is to be in JSON format, let's parse as well.
$ussdRequest = json_decode(@file_get_contents('php://input'));

// Our response object. We shall use PHP's json_encode function
// to convert the various properties (we'll set later) into JSON.
$ussdResponse = new stdClass;

// Check if no errors occured.
if ($ussdRequest != NULL)
    switch ($ussdRequest->Type) {
        // Initiation request. This is the first type of request every
        // USSD application will receive. So let's display our main menu.
        case 'Initiation':

            $ussdResponse->Message =
                "Welcome to Ghana Beverage Awards 2019. Vote for your preferred product of the year.\n" .
                "1. Origin Beer \n2. Club Beer \n3. Star Beer \n4. Guinness \n5. Gulder";
            $ussdResponse->Type = 'Response';
            break;


        // Response request. This is where all other interactions occur.
        // Every time the mobile subscriber responds to any of our vote options,
        // this will be the type of request we shall receive.
        case 'Response':
            switch ($ussdRequest->Sequence) {

                // Menu selection. Note that everytime we receive a request
                // in a particular session, the Sequence will increase by 1.
                // Sequence number 1 was that of the initiation request.
                case 2:
                    $items = array('1' => 'Origin Beer', '2' => 'Club Beer', '3' => 'Star Beer', '4' => 'Guinness', '5' => 'Gulder');
                    if (isset($items[$ussdRequest->Message])) {
                        $ussdResponse->Message = 'Please confirm your preferred product of the year is  '
                            . $items[$ussdRequest->Message] . "?\n1. Yes\n2. No";
                        $ussdResponse->Type = 'Response';
                        $ussdResponse->ClientState = $items[$ussdRequest->Message];
                    } else {
                        $ussdResponse->Message = 'Invalid option.';
                        $ussdResponse->Type = 'Release';
                    }
                    break;

                // Order confirmation. Here the user has responded to our
                // previously sent menu (i.e. Please confirm your preferred product of the year is...)
                // Note that we saved the option the user selected in our
                // previous dialog into the ClientState property.
                case 3:
                    switch ($ussdRequest->Message) {
                        case '1':
                            $db = new DB();

                            // save_vote will check to see if the person has already voted
                            $phone_number = $ussdRequest->Mobile;

                            //Return the array number for the selected vote to be used when updated votes
                            $items2 = array('1' => 'Origin Beer', '2' => 'Club Beer', '3' => 'Star Beer', '4' => 'Guinness', '5' => 'Gulder');
                            $voted_for = array_search($ussdRequest->ClientState, $items2) ;

                            $response = $db->save_vote($phone_number, $voted_for);
                            //echo $response;

                            //Display Success message after vote saved.
                            $ussdResponse->Message =
                                'Thank you. You have successfully voted for '
                                . $ussdRequest->ClientState . ' as your preferred Product of the Year.';


                            break;
                        case '2':
                            $ussdResponse->Message = 'Vote cancelled.';
                            break;
                        default:
                            $ussdResponse->Message = 'Invalid selection.';
                            break;
                    }
                    $ussdResponse->Type = "Release";
                    break;

                // Unexpected request. If the code here should ever
                // execute, it means the request is probably forged.
                default:
                    $ussdResponse->Message = 'Unexpected request.';
                    $ussdResponse->Type = 'Release';
                    break;
            }
            break;

        // Session cleanup.
        // Not much to do here.
        default:
            $ussdResponse->Message = 'Duh.';
            $ussdResponse->Type = 'Release';
            break;
    }
// An error has occured.
// Probably the request JSON could not be parsed.
else {
    $ussdResponse->Message = 'Invalid USSD request.';
    $ussdResponse->Type = 'Release';
}
// Let's set the HTTP content-type of our response, encode our
// USSD response object into JSON, and flush the output.

header('Content-type: application/json; charset=utf-8');
echo json_encode($ussdResponse);

Full Error per heroku Logs:

2019-02-28T16:31:19.510613+00:00 app[web.1]: [28-Feb-2019 16:31:19 UTC] PHP Fatal error: Uncaught Error: Call to a member function bindParam() on bool in /app/db.php:62 2019-02-28T16:31:19.510703+00:00 app[web.1]: Stack trace: 2019-02-28T16:31:19.510862+00:00 app[web.1]:

0 /app/vote-now.php(77): DB->save_vote(277655805, 1) 2019-02-28T16:31:19.510947+00:00 app[web.1]: #1 {main}

2019-02-28T16:31:19.511072+00:00 app[web.1]: thrown in /app/db.php on line 62 2019-02-28T16:31:19.512333+00:00 app[web.1]: 10.45.101.19 - - [28/Feb/2019:16:31:19 +0000] "POST /vote-now.php HTTP/1.1" 500 - "http://apps.smsgh.com/USSDSimulator/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36



from WHY does the array execution in the function below throw an exception?

No comments:

Post a Comment