a_dam12
a_dam12

Reputation: 1

Using A HREF in a while loop to send data from my database to another page

I need to make a movie rater website and on the front page it has the ten most recently added movies. Whenever you click on the movie title its supposed to take you to another page where you can see more info on the movie. I cant seem to get my a href to properly send any of the data to the other page.

code for displaying my homepage

while($row = $result->fetch()){

$MovieName = $row['name'];
$MoviePic = $row['picture'];


echo "<div align=center>";
if($i==1){
    echo '<tr>';
}
$id = $row['MovieId'];
echo '<td><a href = "MovieInfo.php?id = $id"> <img src="data:image/jpeg;base64,'.base64_encode( $MoviePic).'"width = 115 height = 150 name = $MovieName/>';

echo "<br>";
echo $MovieName, '</td>';
echo "</a href>";
echo "<br>";

$i = $i + 1;  
if($i==4){
    echo '</tr>';
    $i=1;
    }
}

code from the second webpage

<?php
require('DBConnect.php');
echo "<div align=center>";
if(isset($_GET['id'])){
    $Name = $_GET['id'];
}
else{
    $Name = 'no';
}
echo $Name;

currently, I'm just testing with the movie's id but no data is being sent to the new page.

Upvotes: 0

Views: 152

Answers (3)

webmatrix
webmatrix

Reputation: 122

Issue is with proper concatenation of id in href. $id is being read as string rather than PHP variable. Replace href with the following line:

href = "MovieInfo.php?id='.$id.'

Upvotes: 0

Sarah Gro&#223;
Sarah Gro&#223;

Reputation: 10879

There are several issues with your code:

  1. remove the spaces before and after the equal sign in your URL (you also don't need them in your HTML attribute/value pairs, and while it may not be invalid, it's not common to have those)
  2. PHP variables are not resolved in single quote strings. You need to concatenate the variable into your single quote string like this: '<td><a href = "MovieInfo.php?id=' . $id . '"> <img...' (and the same with name = $MovieName, where you should also add double quotes around the attribute value)
  3. echo "</a href>"; - you cannot have any attributes on a closing HTML tag. </a> is enough to close the opened tag.
  4. You are closing the <td> before the <a>, but the <a> was opened inside the <td>, so you'll have to close it in reverse order as well.

Upvotes: 3

FunkyMonk91
FunkyMonk91

Reputation: 1481

You will need to do an additional query on the page load for your movie page. Something like SELECT * FROM movies WHERE id = MOVIE_ID. I strongly suggest you use PDO to do your queries with prepared statements to avoid SQL injection.

Upvotes: 0

Related Questions