Jump to content

[SOLVED] Dynamic URL with Php / MySQL


nita

Recommended Posts

HI. I.m rather new to php and mysql.

I would like to get some help with this basic script below.

Script is printing names of the movies as a links with specified id.

Once is clicked on - it meant to refresh index.php and display full  info about choosen movie.

But is not, instead is printing entire list of movies again.

you can this script in action at:

 

www.nita-on-line.com/dynurl/

 

<?
include "connectdb.php";

if (isset($id)) // looking at a record with speified id
  {
    $S1="SELECT * from movies where id='$id'";
    $S2=mysql_query($S1);
    $S3=mysql_fetch_array($S2);
    print "$S3[name]";
    print "$S3[plot]";

  }

else if(!isset($id)) // looking at the root
  {
     $D1="SELECT * from movies";
     $D2=mysql_query($D1);
     while($D3=mysql_fetch_array($D2))

     {
        print "<A href='index.php?ID=$D3[id]'>$D3[name]</a><br>";
 }
  }
?> 

 

 

I'm not sure what is wrong, i will be very happy if someone can help with this code.

Im aware how basic it is, but im stuck and would like to move on.

Thank you for your solution in advance.

 

Nita nita.travel@ekit.com

Link to comment
Share on other sites

<?
include "connectdb.php";

if(is_numeric($_GET[id])){
$S2=mysql_query("SELECT * from movies where id='$_GET[id]' LIMIT 1");
$S3=mysql_fetch_array($S2);
print "$S3[name]";
print "$S3[plot]";
}else{
$D2=mysql_query("SELECT * from movies");
while($D3=mysql_fetch_array($D2)){
  print "<A href='index.php?ID=$D3[id]'>$D3[name]</a><br>";
}
}
?>

Link to comment
Share on other sites

Thx for your replay.

 

but the problem is still not solved, exactly the same think is happening. Browser will not show details of specified movie / id. Print the entire list again, instead.

 

I dont know if it matters much what is in connectdb.php file.

Listing below:

 

<?
//connect to database
       $conn = mysql_connect("localhost", "login", "password") or die(mysql_error());
       mysql_select_db("nita",$conn) or die(mysql_error());
?>

 

Well, maybe there is a problem with GET ??

 

Thanks a lot for further help !!!

Nita

Link to comment
Share on other sites

I did modarate my code a bit (after looking up in some other sources)

 


<?
include "connectdb.php";

if(isset($_GET['id']))
{
$id=$_GET['id'];
$S2=mysql_query("SELECT * from movies where id='$id' LIMIT 1");
$S3=mysql_fetch_array($S2);
print "$S3[name]";
print "$S3[plot]";
}
else
{
$D2=mysql_query("SELECT * from movies");
while($D3=mysql_fetch_array($D2))
{
  print "<A href='index.php?id=$D3[id]'>$D3[name]</a><br>";
}
}
?>

 

There is no problem of geting the names of the movies as a links with specified id.

Problem is with display a single record (within same index.php file).

Look at:

www.nita-on-line.com/dynurl/

Browser do listing from the database with or without specified id. (!!worng if - else statment ??)

 

It's really confusing me. i tried many options, but non of them seems to work.

I know i cant be to difficult couse this is very beginning of php/mysql run websites.

 

if someone can give me some tip, please.

 

Link to comment
Share on other sites

Problem has been solved !!! Thx to Teith

 

Corect code:

 


<?
include "connectdb.php";

if(isset($_GET['id']))
{
$id=$_GET['id'];
$S2=mysql_query("SELECT * from movies where id='$id' LIMIT 1");
$S3=mysql_fetch_array($S2);
print "$S3[name]";
print "$S3[plot]";
}
else
{
$D2=mysql_query("SELECT * from movies");
while($D3=mysql_fetch_array($D2))
{
  print "<A href='index.php?id=$D3[id]'>$D3[name]</a><br>";
}
}
?>

Link to comment
Share on other sites

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.