0

i have this problem where my query wont pull the data from the database into the website, i've tried many things but i either get an offset error or nothing appears at all. Could someone have a look please?

website with dropdown -

$dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' . mysqli_connect_error() );
//echo "Connected to MySQL<br>";
$q = "SELECT artistname FROM tbl_artists ORDER BY artistname ASC";      
$r = @mysqli_query ($dbc, $q);

    // Form start
echo '<form action="php/process_dropdown.php" method="post">

<select name="selection">
<option value=""> -- Please select an Artist -- </option>';


// Fetch and print all the records:
    while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
        echo '<option>' . $row['artistname'] . '</option>
        ';
    }

    echo '<p><input type="submit" value="Send"></p>

    </select>

</form>'; // End of form.

?>

The dropdown process

$dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die ('Could not connect to MySQL: ' . mysqli_connect_error() );

$name = $_POST['selection'];


$q = "SELECT tbl_songs.songtitle, tbl_songs.yearrelease, tbl_artists.genre, tbl_songs.price
FROM tbl_artists INNER JOIN tbl_songs ON tbl_artists.artistID = tbl_songs.artistID
WHERE (((tbl_artists.artistID)='$name'))";

$r = @mysqli_query ($dbc, $q) or die("Error: ".mysqli_error($dbc));

    // Table header.
    echo '<table>
    <tr>
    <th scope="col">Song</th>
    <th scope="col">Year Released</th>
    <th scope="col">Genre</th>
    <th scope="col">Price</th>
    </tr>
';

    // Fetch and print all the records:
    while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) {
        echo '<tr><td>' . $row['songtitle'] . '</td><td>' . $row['yearrelease'] . '</td><td>' . $row['genre'] . '</td><td>' . $row['price'] . '</td></tr>   ';
    }

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

?>

Thank you.

1
  • Do you get any error report? Commented Feb 21, 2015 at 2:52

1 Answer 1

1

EDIT:

$link = mysqli_connect('host', 'user', 'password', 'database');

Change into:

$dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) OR die 
('Could not connect to MySQL: ' . mysqli_connect_error() );
//echo "Connected to MySQL<br>";
$q = "SELECT artistname FROM tbl_artists ORDER BY artistname ASC";      
$r = mysqli_query ($dbc, $q);
..................

And see this:

WHERE (((tbl_artists.artistID)='$name'))";

You missed: tbl_songs. before $name.

Besides, It's not good to use @ since it hides the error message. (It's a poor scripting)

Sign up to request clarification or add additional context in comments.

2 Comments

I don't really know what you changed, but something has fixed it. Also, adding the tbl_songs before $name doesn't make a difference. It works without it, but won't work with it. Also, i realised that i wrote WHERE (((tbl_artists.artistID)='$name'))"; rather than WHERE (((tbl_artists.artistname)='$name'))"; which meant that i defined the wrong field
that's what makes me confused too. you need to find artistID but grabbing it from name. now you're working with Inner Join. there are three types of it, one of them is here > tutorialspoint.com/sql/sql-inner-joins.htm

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.