jk1984
jk1984

Reputation: 39

Undefined index on inner join

Trying to access CDID to set in the query and then use GET later to retrieve it on the next page.

I was always under the impression that INNER JOIN cdreview ON cdreview.CDID=cd.CDID would combine CDID as they are the same value, and then I could just access the value by setting it in the query as $cdid = $row['CDID']; but I keep getting Undefined index: CDID error message.

I'm a noob so any help would be appreciated.

<?php

    require_once 'database_conn.php';

    $userid = $_SESSION['userSession'];     

$sql = "SELECT cdreview.reviewDate, cdreview.reviewText, cd.CDTitle FROM cd 
INNER JOIN cdreview ON cdreview.CDID=cd.CDID AND cdreview.userID='$userid' ORDER BY cdreview.reviewDate;" or die;       


$result = mysqli_query($conn, $sql) or die(mysqli_error($conn));

   if (mysqli_num_rows($result) > 0) {                  

    while ($row = mysqli_fetch_assoc($result)){     

    $date = $row['reviewDate'];
    $album = $row['CDTitle'];
    $review = $row['reviewText'];
    $cdid = $row['CDID'];

            echo"<table align='center'>
            <tr align='center'>
                <td colspan='5'>
                    <h2>View All Reviews</h2>
                </td>
            </tr>

            <tr align='center'>
                <th>Date</th>
                <th>Album</th>
                <th>Reviews</th>
                <th>Edit Review</th>
                <th>Delete Review</th>
            </tr>

            <tr align='center'>
                    <td>$date</td>
                    <td>$album</td>
                    <td>$review</td>
                    <td><a href=\"album_reviews.php?id=$cdid\"></a></td>
                    <td><a href=\"review.php?id=$cdid\"></a></td>
                </tr>
            </table>";
    }   
} else {
    echo "<script>alert('You have not left any reviews!')</script>";    
            echo "<script>window.open('home.php', '_self')</script>";
}
mysqli_close($conn);
?>

Upvotes: 0

Views: 1000

Answers (2)

Mathias Bader
Mathias Bader

Reputation: 3826

You need to add the CDID column to the select statement either using:

$sql = "SELECT cd.CDID, cdreview.reviewDate, cdreview.reviewText, cd.CDTitle FROM cd [...];"

or using:

$sql = "SELECT * FROM cd [...];"

You idea is correct that when you join on the CDID-column, that column is available for SELECT and has the same value from both tables. But it is not automatically selected - if you want to retriev it, you still need to select it.

There is nothing like an implicit SELECT where attributes that are used in JOIN or other statements are added to the SELECT. The SELECT-statement always retrievs exactly what you specify and nothing more.

Upvotes: 0

ScaisEdge
ScaisEdge

Reputation: 133380

You are trying to get the value that you don't select

  $date = $row['reviewDate'];
  $album = $row['CDTitle'];
  $review = $row['reviewText'];
  $cdid = $row['CDID'];

but you select only

 SELECT cdreview.reviewDate, cdreview.reviewText, cd.CDTitle  from 

you don't have a CDID column in your select

Upvotes: 4

Related Questions