Jump to content

[SOLVED] Different database results using a variable and hard coding in a search value.


Recommended Posts

Hi

 

I'm trying to create an online photo gallery and I'm pretty new to php. 

My problem is when I try and search my database.  When I use the following code..

 

$result = mysql_query("SELECT * FROM Images 
INNER JOIN Images_In_Album 
ON Images.ID = Images_In_Album.PicID 
INNER JOIN Albums 
ON Images_In_Album.AlbumID = Albums.ID 
WHERE Albums.ID = 1");

 

I get all the photos in the album with ID = 1.  So when I click my 'Next' link to browse through the pics they are all there.  However, when I use this code...

 

$result = mysql_query("SELECT * FROM Images 
INNER JOIN Images_In_Album 
ON Images.ID = Images_In_Album.PicID 
INNER JOIN Albums 
ON Images_In_Album.AlbumID = Albums.ID 
WHERE Albums.ID = \"$albumID\"");

 

Only the first picture is there.  When I click the 'Next' link, the wee box with the red cross in it appears.

 

Do you have any idea why this happens and how I can fix it??

 

This is my complete code...  $albumID comes from the previous page and changes depending on which photo album the user has clicked.

 

<?php
$username = "root";
$password = "";
$hostname = "localhost"; 


//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password) 
  or die("Unable to connect to MySQL");
//echo "Connected to MySQL<br>";

$selected = mysql_select_db("MyWebsite",$dbhandle) 
  or die("Could not select examples");

//code here to determine what link has been clicked.
$albumID = $_GET['ID'];

//sql query to get records from that album.
$result = mysql_query("SELECT * FROM Images 
INNER JOIN Images_In_Album 
ON Images.ID = Images_In_Album.PicID 
INNER JOIN Albums 
ON Images_In_Album.AlbumID = Albums.ID 
WHERE Albums.ID = \"$albumID\"");

//arrays to hold the titles, descriptions and paths separately
$titles = array();
$descriptions = array();
$paths = array();

$counter = 0;
//fetch tha data from the database
while ($row = mysql_fetch_array($result)) 
{

$titles[$counter] = $row['Title'];
$descriptions[$counter] = $row['Description'];
$paths[$counter] = substr(strrchr($row['Path'],92),1);
   
   $counter++;
   
}

$imgIndex = $_GET['img'];


if(!isset($paths[$imgIndex]))
{
    $imgIndex = 0;
}

$currentImage = "images\\" . $paths[$imgIndex];

if ($imgIndex<=0)
{
    $prev = "Previous";
}
else
{
    	$prev = "<a href=\"".$_SERVER['SCRIPT_NAME']."?page=photo&img=".($imgIndex-1)."\">Previous</a>";

}

if ($imgIndex>=(count($paths)-1))
{
    $next = "Next";
}
else
{
   	$next = "<a href=\"".$_SERVER['SCRIPT_NAME']."?page=photo&img=".($imgIndex+1)."\">Next</a>";

}

echo " albumID " . $albumID;
echo "<pre>$prev                       $next</pre><br>";
echo "<div id='photoBody'></br><h2>$titles[$imgIndex]</h2>";
echo "<img src=\"{$currentImage}\"></br></br>";
echo "<p>$descriptions[$imgIndex]</p></div>";


mysql_close($dbhandle);
?>

 

Thank you.

When you pass on to the next page, you don't resend the AlbumID. You just have the page number. Add the ID part to the next page hyperlink. Something like:

 

<?php

$next = "<a href=\"".$_SERVER['SCRIPT_NAME']."?page=photo&img=".($imgIndex+1)."&ID=".$albumID."\">Next</a>";

?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.