vote function not working in php application

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1685 Views - Last Post: 29 January 2015 - 02:08 PM Rate Topic: -----

#1 jvanasco   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 28-January 15

vote function not working in php application

Posted 28 January 2015 - 02:33 PM

I'm looking for someone to help me out with an application I am trying to fix for someone. I cannot figure out why this last part is not working. There is a vote button on a gallery page which should insert a vote and associate it with the uploaded video. For some reason the function is not making the entry into the database. The vote button is in a file called gallery.php and the function lives in a file called users.php

This is the button that should call the vote function -
<div style="margin:0 3px;background-image: url('<?php echo base_url(); ?>images/vote.png');background-repeat:no-repeat;float:left;">
<a id="vote_<?php echo $photo['id']; ?>" href="hello" onclick='vote("<?php echo $captions; ?>", "<?php echo $photo['video_id']; ?>", <?php echo $photo['id']; ?>, <?php echo $user_id[0]->id; ?>)' class="vote">
<?php echo ($this->users->has_voted_source($photo['id'], $user_id[0]->id,$iframe)) ? "Voted" : "Vote"; ?>
</a>
</div>
[b]This is the function that should be inserting the vote -[/b]


public function vote($pic_id, $user_id,$source) { //$sqlQuery = "SELECT * FROM votes WHERE pic_id = ".$pic_id." AND user_id = ".$user_id;//." AND vote_time > ".time();
        $userObject['pic_id'] = $pic_id;
        $userObject['user_id'] = $user_id;
        $userObject['source'] = $source;
        $userObject['vote_time'] = date('Y-m-d H:i:s');
        $sqlQuery = "INSERT INTO votes ('pic_id','user_id','vote_time','source') VALUES (".$pic_id.",".$user_id.",".$source.",now())";
        $this->db->insert('votes', $userObject);
        $newID = $this->db->insert_id();
        if ($newID) {
            $update = "UPDATE photos set votes=vote+1 WHERE `id` = $pic_id";
            $query = $this->db->query($update);
            return true;
        } else {
            return false;
        }

    }


This post has been edited by CTphpnwb: 28 January 2015 - 02:36 PM


Is This A Good Question/Topic? 0
  • +

Replies To: vote function not working in php application

#2 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Re: vote function not working in php application

Posted 28 January 2015 - 02:41 PM

See where you've added variables to your queries? Those variables likely contain data supplied by a user, which means that if you get this working, you'll be hacked. It's just a matter of time. Read up on prepared statements.

Your other problem is that PHP is not Javascript. It doesn't run on the browser like Javascript: it runs on the server, before (and after) HTML is processed, so it cannot be called directly from HTML.
Was This Post Helpful? 0
  • +
  • -

#3 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3150
  • View blog
  • Posts: 11,955
  • Joined: 03-December 12

Re: vote function not working in php application

Posted 28 January 2015 - 02:45 PM

Quote

	public function vote($pic_id, $user_id,$source) { //$sqlQuery = "SELECT * FROM votes WHERE pic_id = ".$pic_id." AND user_id = ".$user_id;//." AND vote_time > ".time();
	        $userObject['pic_id'] = $pic_id;
	        $userObject['user_id'] = $user_id;
	        $userObject['source'] = $source;
	        $userObject['vote_time'] = date('Y-m-d H:i:s');



The function doesn't make make much sense. You pass in variables to use, then change those values to something else... Where is $userObject defined? If you are looking to just pass in an array, you can do that, but don't pass in arguments just to change what they are holding.
Was This Post Helpful? 0
  • +
  • -

#4 jvanasco   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 28-January 15

Re: vote function not working in php application

Posted 28 January 2015 - 03:00 PM

This whole piece of code doesn't make sense to me.. there is a vote button on a gallery page and when a user clicks on it, the function is supposed to save a vote into a database table. The table already has data in it such as user_id, video_id, etc. there is a column in that table called votes, the button is supposed to call that php function to save that data. I did not write any of this code so I am kind of lost at the moment as to where to fix this.
Was This Post Helpful? 0
  • +
  • -

#5 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2760
  • View blog
  • Posts: 8,062
  • Joined: 15-January 14

Re: vote function not working in php application

Posted 28 January 2015 - 03:21 PM

Hopefully there is a corresponding Javascript function called vote which sends the data to PHP via ajax. Otherwise, it looks like the code is trying to call a PHP function from Javascript and that's not going to work, that's not how the languages work. See if you can find a Javascript function called vote.
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,563
  • Joined: 08-June 10

Re: vote function not working in php application

Posted 29 January 2015 - 01:29 AM

and on top of that the insert query would throw an error.
Was This Post Helpful? 0
  • +
  • -

#7 jvanasco   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 28-January 15

Re: vote function not working in php application

Posted 29 January 2015 - 07:18 AM

I found an appajax.php file with the below code in it, does this help?

* This function is used for voting via ajax request
         *
         * @access public
         * @author Developer16

          public function ajax_vote() {

          $iframe = (int) $this->input->post('iframe');
          $video_id = $this->input->post('video_id');
          if (!$iframe) { $iframe = 0; }
          if ($iframe != 1 && $iframe != 0) { $iframe = 0; }
          $user_id = $this->users->get_user_by_uid($this->session->userdata('facebook_uid'));

          if ($this->users->has_voted_source($video_id, $user_id[0]->id, $iframe)) {
          echo json_encode(array('status' => 'fail','msg' => '<span style="font-size:16px;font-weight:bold;color:red;">You are already vote today.</span>'));
          } elseif ($this->users->vote($video_id, $user_id[0]->id, $iframe)) {
          echo json_encode(array('status' => 'success','msg' => '<span style="font-size:16px;font-weight:bold;color:green;">Your vote has been counted. Thank you!</span>'));
          }
          }
         */

Was This Post Helpful? 0
  • +
  • -

#8 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2760
  • View blog
  • Posts: 8,062
  • Joined: 15-January 14

Re: vote function not working in php application

Posted 29 January 2015 - 10:17 AM

That's PHP code, you need to see if there is a Javascript function called vote. This line:

<a id="vote_<?php echo $photo['id']; ?>" href="hello" onclick='vote("<?php echo $captions; ?>", ...

Is writing a link tag with a click handler to call a Javascript function called vote. If you can pull that page up in a browser then do so and open your browser's developer tools to look for error messages in the console when you click on that link. If there's not a function called vote then an error message will show up (you'll have to click on the console option to persist messages so that the error doesn't go away when the page reloads).
Was This Post Helpful? 0
  • +
  • -

#9 jvanasco   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 28-January 15

Re: vote function not working in php application

Posted 29 January 2015 - 10:27 AM

okay I tried clicking on the button with developer tools open and this is the error I am seeing

POST https://sonnyspianos...154107704607227 500 (Internal Server Error)jquery.min.js:6 x.support.cors.e.crossDomain.sendjquery.min.js:6 x.extend.ajax1?fb_page_id=154107704607227&app_data=gallery:81 vote1?fb_page_id=154107704607227&app_data=gallery:386 onclick
Was This Post Helpful? 0
  • +
  • -

#10 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4278
  • View blog
  • Posts: 13,563
  • Joined: 08-June 10

Re: vote function not working in php application

Posted 29 January 2015 - 10:29 AM

this happens when the PHP script encounters a fatal error. but it may have other reasons. best check your PHP error log.
Was This Post Helpful? 0
  • +
  • -

#11 jvanasco   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 28-January 15

Re: vote function not working in php application

Posted 29 January 2015 - 11:22 AM

ok I checked the log file and this is what I am getting

ERROR - 2015-01-29 12:18:10 --> Severity: Notice --> Undefined variable: fb_page_id /home/sonnyspianos/public_html/application/controllers/app.php 678
ERROR - 2015-01-29 12:19:53 --> Severity: Notice --> Undefined variable: fb_page_id /home/sonnyspianos/public_html/application/controllers/app.php 678
ERROR - 2015-01-29 12:20:48 --> Severity: Notice --> Undefined variable: fb_page_id /home/sonnyspianos/public_html/application/controllers/app.php 678

I looked at line 678 in my app.php file and this is the code

if($fb_page_id == '' && $this->session->userdata('fb_page_id')) {
                $fb_page_id = $this->session->userdata('fb_page_id');
            }



View PostDormilich, on 29 January 2015 - 10:29 AM, said:

this happens when the PHP script encounters a fatal error. but it may have other reasons. best check your PHP error log.

Was This Post Helpful? 0
  • +
  • -

#12 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2760
  • View blog
  • Posts: 8,062
  • Joined: 15-January 14

Re: vote function not working in php application

Posted 29 January 2015 - 11:32 AM

You just need to define that variable before testing it, you can initialize it to an empty string. Those notices wouldn't cause the 500 error though, a 500 is a fatal error, not a notice.

What PHP framework are you using for this?
Was This Post Helpful? 0
  • +
  • -

#13 jvanasco   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 28-January 15

Re: vote function not working in php application

Posted 29 January 2015 - 11:41 AM

The project is using codeigniter, which I am not familiar with really. The guy who was working on this is no longer working with my friend so i've been given the task to fix this vote aspect which is the last part needed to work. I was hoping to get it fixed by tonight as we meet later on about the project, but I thought it was a much more simple fix than it seems to be.. I'm sure if I could get someone who knows more than me to look at the files they could figure it out much faster than me. Ive been banging my head on this for almost 2 weeks.

This post has been edited by Dormilich: 30 January 2015 - 12:55 AM

Was This Post Helpful? 0
  • +
  • -

#14 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2760
  • View blog
  • Posts: 8,062
  • Joined: 15-January 14

Re: vote function not working in php application

Posted 29 January 2015 - 01:42 PM

The way forward with this is to figure out what the actual error is. You know there's an error occurring, but not what it is. I don't have any experience with Codeigniter, but it looks like there is an article here about it:

http://www.askaboutp...ing-errors.html

With vanilla PHP, you can have PHP write all error messages to a log file with code like this (assuming that PHP has permission to write to the file):

ini_set('error_log', __DIR__ . DIRECTORY_SEPARATOR . 'error.log');
error_reporting(E_ALL);


It looks like Codeigniter sets the error reporting level, so make sure it's reporting all errors.
Was This Post Helpful? 0
  • +
  • -

#15 jvanasco   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 79
  • Joined: 28-January 15

Re: vote function not working in php application

Posted 29 January 2015 - 02:01 PM

I changed the error reporting level to 4 but that doesn't seem to really give me any additional information about the error when I look at the updated error log..

This post has been edited by Dormilich: 30 January 2015 - 12:55 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2