• Hello WordPress community,
    I’m currently developing Star Rating plugin and I get the following error:

    <b>Fatal error</b>: Call to a member function fetch_assoc() on a non-object in <b>C:\Users\User\Dropbox\XAMPP\htdocs\events\wp-content\plugins\review-plugin\inc\data-processing.php</b> on line <b>45</b>”
    when I try to save data in database.

    I’m new in plugin development, please help me resolve this issue.

    Thanks!

    Here is my front-end code:

    $post_rating = "
    			 <fieldset id='demo1' class='rating'>
                            <input class='stars' type='radio' id='star5' name='rating' value='5' />
                            <label class = 'full' for='star5' title='Awesome - 5 stars'></label>
                            <input class='stars' type='radio' id='star4' name='rating' value='4' />
                            <label class = 'full' for='star4' title='Pretty good - 4 stars'></label>
                            <input class='stars' type='radio' id='star3' name='rating' value='3' />
                            <label class = 'full' for='star3' title='Meh - 3 stars'></label>
                            <input class='stars' type='radio' id='star2' name='rating' value='2' />
                            <label class = 'full' for='star2' title='Kinda bad - 2 stars'></label>
                            <input class='stars' type='radio' id='star1' name='rating' value='1' />
                            <label class = 'full' for='star1' title='Sucks big time - 1 star'></label>
    
                        </fieldset>
    
                      <script src='https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js'></script>
                        <script>
    					    $('.stars').click(function () {
    
    					        $.post('localhost/events/wp-content/plugins/review-plugin/inc/data-processing.php',{rate:$(this).val()},function(d){
    					        	console.log(d, 'here');
    					            if(d>0)
    					            {
    					                alert('You already rated');
    					            }else{
    					                alert('Thanks For Rating');
    					            }
    					        });
    					        $(this).attr('checked');
    					    });
                        </script>
    
    		";

    (jQuery is here because I’m testing)

    Saving in database code (data-proccesing.php) :

    <?php
    $ipaddress = md5($_SERVER['REMOTE_ADDR']); // here I am taking IP as UniqueID but you can have user_id from Database or SESSION
    
    $servername = "localhost"; // Server details
    $username = "root";
    $password = "";
    $dbname = "events";
    
    $conn = new mysqli($servername, $username, $password, $dbname);
    if ($conn->connect_error) {
        die("Unable to connect Server: " . $conn->connect_error);
    }
    
    if (isset($_POST['rate']) && !empty($_POST['rate'])) {
    
        $rate = $conn->real_escape_string($_POST['rate']);
    // check if user has already rated
        $sql = "SELECT <code>id</code> FROM <code>tbl_rating</code> WHERE <code>user_id</code>='" . $ipaddress . "'";
        $result = $conn->query($sql);
        $row = $result->fetch_assoc();
        if ($result->num_rows > 0) {
            echo $row['id'];
        } else {
    
            $sql = "INSERT INTO <code>tbl_rating</code> ( <code>rate</code>, <code>user_id</code>) VALUES ('" . $rate . "', '" . $ipaddress . "'); ";
            if (mysqli_query($conn, $sql)) {
                echo "0";
            }
        }
    }
    $conn->close();
    ?>

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Plugin development – Database error (Fatal error: Call to a member function..)’ is closed to new replies.