0

I can't get the page to display the game information I think something is wrong with my select statement.

The game table has following fields:

game_id, game_name, platform, genre, game_description, game_price,
game_image, game_headlines, and game_added_date.

I only what the page to display:

 game_name, platform, genre, game_description, game_price, game_image, and game_headlines. 

The page runs but won't display the game even if it in the database.

Website URL: http://cs.neiu.edu/~fsef15g9/CS319_Project_Group_9/view_games.php

game tabel

<?php # add_games.php 
    include ('includes/session.php');

    $page_title = 'Add Games';
    include ('includes/header.php');

    require_once ('mysqli_connect.php'); // Connect to the db.

    // Check if the form has been submitted:
    if (isset($_POST['submitted'])){

        $errors = array(); // Initialize an error array.

        // Check for a game name:
        if (empty($_POST['game_name'])) {
                $errors[] = 'You forgot to enter game name.';
        } else {
                $search = mysqli_real_escape_string($dbc, trim($_POST['game_name']));
        }

        //$where = '%' + '$search' + '%';
        $q = "SELECT 
              game_name AS game, 
              platform AS pf, 
              genre AS ge, 
              game_description AS gd, 
              game_price AS gp, 
              game_image AS gi, 
              game_headlines AS gh 
              FROM game WHERE game_name LIKE '$search'%";
        $r = @\mysqli_query($dbc, $q); // Run the query.

        // Count the number of returned rows:
        $num = mysqli_num_rows($r);

        if ($num > 0) { // If it ran OK, display the records.

            // Print how many users there are:
            echo "<p>There are currently $num number of games for '$search'.</p>\n";

            // Table header.
            echo '<table align="center" cellspacing="3" cellpadding="3" width="75%">
            <tr><td align="left"><b>Game Name</b></td><td align="left"><b>Platform</b></td><td align="left"><b>Genre</b></td><td align="left"><b>Game Description</b></td><td align="left"><b>Game Price</b></td><td align="left"><b>Game Headlines</b></td></tr>';

            // Fetch and print all the records:
            while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
                echo '<tr><td align="left">' . $row['game'] . '</td><td align="left">' . $row['pf'] . '</td><td align="left">' . $row['ge'] . '</td><td align="left">' . $row['gd'] . '</td><td align="left">' . $row['gp'] . '</td><td align="left">' . $row['gi'] . '</td><td align="left">' . $row['gh'] . '</td></tr>';
            }

            echo '</table>'; // Close the table.

            mysqli_free_result ($r); // Free up the resources.  

        } else { // If no records were returned.

            echo '<p class="error">There are currently no registered users.</p>';

        }

        mysqli_close($dbc); // Close the database connection.
    }
    ?>

    <div class="page-header">

        <h1 style="font-size:40px"><center>Search Games</center></h1>
    </div>
    <form class="form-signin" role="form" action="view_games.php" method="post">

        <center><p><input type="normal" placeholder="Enter Game Name" required autofocus name="game_name" maxlength="60" value="<?php if (isset($_POST['game_name'])) echo $_POST['game_name']; ?>" />
        <button style="display:inline; padding:10px; margin-bottom:5px" type="submit" name="submit" class="btn btn-sm btn-primary" />Add Game</button>
        <input type="hidden" name="submitted" value="TRUE" /></p></center>

    </form>

    <?php
    include ('includes/footer.html');
    ?>
4
  • 1
    Remove that @ symbol when you run your query so you can actually get an error displayed. Then if the error message doesn't help you please update the post with it so it might help me/other users finding the issue. Commented Nov 29, 2015 at 22:40
  • 1
    Have you manually tried to write the query to see if you get a result? Without the $search variable? Commented Nov 29, 2015 at 22:42
  • I have removed the "@ symbol" but it still prints out the else statement "There are currently no registered users" I will change this statement to "There are currently no game for the search game" Commented Nov 29, 2015 at 22:49
  • 2
    The % at the end of the SELECT statement should come before the closing single quote, like this : WHERE game_name LIKE '$search%' Commented Nov 29, 2015 at 22:54

1 Answer 1

3

Your query is incorrect, as you have your wildcard % outside the quotes

WHERE game_name LIKE '$search'%

Need to move the % inside the quotes

WHERE game_name LIKE '$search%'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.