Using assigned IDs

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Ok, your turn. Write some code that will work. :wink:

Code: Select all

<?php
$show_detail = false;

if (isset($_GET['id']))
{
    $dvd_id = mysql_real_escape_string($_GET['id']);
    $sql = "select `dvd_id`, `dvd_plot` from `dvd_details` where `dvd_id` = $dvd_id";
    if (!$result = mysql_query($sql))
    {
        die('Could not get the selected DVD using ' . $sql . ': ' . mysql_error());
    }

    $dvd_detail = mysql_fetch_array($result);
    if (!empty($dvd_detail))
    {
        $show_detail = true;
    }
}


if ($show_detail)
{
    // $dvd_detail houses the information in the detail
    echo 'The ID of this DVD: ' . $dvd_detail['dvd_id'] . '<br />';
    echo 'The plot of this DVD: ' . $dvd_detail['dvd_plot'] . '<br />';
}
else
{
  // Will add code for master/main table here
}
?>
phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Post by phpflixnewbie »

WERHOOOO! Just tested the ammended code and added my database connection strings and it worked!!
Still just need to add my master table code, but surely there will always be a match of details for the ID?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Not surely, since you are passing the ID through GET. There is nothing to stop someone from passing url?id=MyStringeHands. Always validate, and only show a detail if there was actually a record found, after validating the $_GET['id'] var.
phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Post by phpflixnewbie »

Everah, many thanks for the help. I am now trying to edit the code so it outputs exactly what im after. I would like it to print out the Title name, but i keep getting the error:
'Could not get the selected DVD using select dvd_id, dvd_title, dvd_plot from dvd_details, dvd_titles where dvd_titles.dvd_id = 1: Column 'dvd_id' in field list is ambiguous'

Please advise.

Code: Select all

<?php

$show_detail = false;

if (isset($_GET['id']))
{
$Host = "localhost"; //you can use IP address instead of localhost
$User = "username";
$Password = "password";
$Database = "databasename";

$Link_ID=mysql_pconnect($Host, $User, $Password);
     if (!$Link_ID)
     {
        echo "Failed to connect to Server=".$Host;
          return 0;
     }
     else
     {
#         echo "<B>Successfully to connected to Server  </B>" .$Host;
     }


     if (!@mysql_select_db($Database,$Link_ID))
     {
#         echo "<br>Cannot use database=  " .$Database;
      }
      else
     {
#         echo "<br> Successfully connected to database= ";
      }


$dvd_id = mysql_real_escape_string($_GET['id']);

$sql = "select dvd_id, dvd_title, dvd_plot from dvd_details, dvd_titles where dvd_titles.dvd_id = $dvd_id";

if (!$result = mysql_query($sql))
    {
        die('Could not get the selected DVD using ' . $sql . ': ' . mysql_error());
    }

$dvd_detail = mysql_fetch_array($result);
if (!empty($dvd_detail))
    {
        $show_detail = true;
    }

}


if ($show_detail)
{
   // $dvd_detail houses the information in the detail
 echo 'Title: ' . $dvd_detail['dvd_title'] . '<br />';
 echo 'Plot: ' . $dvd_detail['dvd_plot'] . '<br />';
}
else
{
  // Will add code for master/main table here
}
?>
phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Post by phpflixnewbie »

Nevermind i found the problem, just edited sql query to:

Code: Select all

$sql = "select dvd_titles.dvd_id, dvd_title, dvd_plot from dvd_details, dvd_titles where dvd_titles.dvd_id = $dvd_id";

So far I only have one title which has detail information, i need to add details to other titles so i can fully test it on any random ID.
Many thanks for all the help guys :D
phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Post by phpflixnewbie »

Just one last thing on this issue guys, how do i echo/print out the coveart images. I have stored their URLs in the database and added them to the sql query, ive attempted the echo statement below, but no joy.
Please advise.


Code: Select all

<?php

$show_detail = false;

if (isset($_GET['id']))
{
$$Host = "localhost"; //you can use IP address instead of localhost
$User = "username";
$Password = "password";
$Database = "databasename";

$Link_ID=mysql_pconnect($Host, $User, $Password);
     if (!$Link_ID)
     {
        echo "Failed to connect to Server=".$Host;
          return 0;
     }
     else
     {
#         echo "<B>Successfully to connected to Server  </B>" .$Host;
     }


     if (!@mysql_select_db($Database,$Link_ID))
     {
#         echo "<br>Cannot use database=  " .$Database;
      }
      else
     {
#         echo "<br> Successfully connected to database= ";
      }


$dvd_id = mysql_real_escape_string($_GET['id']);

$sql = "select dvd_titles.dvd_id, dvd_title, dvd_plot, image_path from dvd_details, dvd_titles, dvd_coverart where       dvd_titles.dvd_id = $dvd_id";

if (!$result = mysql_query($sql))
    {
        die('Could not get the selected DVD using ' . $sql . ': ' . mysql_error());
    }

$dvd_detail = mysql_fetch_array($result);
if (!empty($dvd_detail))
    {
        $show_detail = true;
    }

}


if ($show_detail)
{
   // $dvd_detail houses the information in the detail
 echo '<img src="$dvd_detail['image_path']"';
 echo '<p>'.'Title: ' . $dvd_detail['dvd_title'] . '</p>';
 echo '<p>'.'Plot: ' . $dvd_detail['dvd_plot'] . '</p>';
}
else
{
  // Will add code for master/main table here
}
?>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Follow the lead of the other code...

Code: Select all

if ($show_detail)
{
  // $dvd_detail houses the information in the detail
  // This has a few problems, can you spot the difference between it and
//  echo '<img src="$dvd_detail['image_path']"';
  // This one?
  echo '<img src="' . $dvd_detail['image_path'] . '" />';
  echo '<p>'.'Title: ' . $dvd_detail['dvd_title'] . '</p>';
  echo '<p>'.'Plot: ' . $dvd_detail['dvd_plot'] . '</p>';
}
phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Post by phpflixnewbie »

Hi everah,

I see from your ammendment that i was missing the dots, the closing image tag and quote marks were in the wrong place, however the image is still not displaying.
The URLs in the database are stored as \coverart images\filename.jpg and i also tried localhost\coverart images\filename.jpg.
Please advise.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

View the source of the output html and post a few lines of that so we can see what is being output.
phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Post by phpflixnewbie »

Heres the output HTML for the main part of the page:

Code: Select all

<div id="details">


<img src="\coverart images\kingofthehills04.jpg" /><p><b>Title:</b>Beavis And Butt-Head Do America</p><p><b>Plot:</b>Our two adolescent heroes embark on an epic journey across america to recover their precious stolen television.
On their travels they come across a murderous smuggler of a deadly virus, an FBI agent with a partiality for cavity searches and ex-Motley Crue roadies.
Can the Great Cornholio save the day? Ofcourse they can! (or maybe not).</p>

</div>
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

There is a space in your image path. That is likely to kill it. You should either get rid of the space or run the path variable through urlencode() so that the spaces get transposed to something usable. I would suggest just getting rid of the space however.
phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Post by phpflixnewbie »

Removed the space from the directory and the path in the database, refreshed the page, but image still not displaying :cry:
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Change your path separator to '/' instead of '\'.
phpflixnewbie
Forum Contributor
Posts: 132
Joined: Fri Nov 17, 2006 11:46 am

Post by phpflixnewbie »

That worked! Thankyou again! :D (Hugs Everah)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Glad I could help.
Post Reply