Dynamic url using id to match id in mySQL database...

Discussion in 'MySQL' started by jezebel, Jun 17, 2006.

  1. #1
    That's probably not accurate terminology, but its the best I know how to describe what I'm trying to do. I've set up dgssearch and it works beautifully. It brings up the results page, in which each result has a link. Unfortunately, I'm having trouble figuring out how to make the link work.

    The search is at http://www.turnupdown.com/dgssearch/index.php

    If you search for Eric Clapton or Bruce Springsteen, or anyone else that I might have in the database, it will return the results. You will then see the link I am referring to.

    An example would be:

    http://www.turnupdown.com/dgssearch/library.php?id=770

    In this instance, I want the library.php page to pull up certain fields from the table row with an id of 770.

    Does this make sense? Its been so long since I've done anything like this, I've forgotten everything I used to know and its just no clicking for me.

    Thanks in advance for any input.

    jez
     
    jezebel, Jun 17, 2006 IP
  2. soj

    soj Well-Known Member

    Messages:
    233
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    138
    #2
    
    
    SELECT * FROM `table` WHERE `id` =770
    
    Code (markup):
    use the mysql code from above in your php script and change the use the $id instead of 770.
     
    soj, Jun 18, 2006 IP
  3. jezebel

    jezebel Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for the reply. The code for the library.php page is as follows:

    <html>
    <body>
    <?php
    if(isset($_GET['ID'])) {
    $ID = $_GET['ID']; 
    
     $dbh=mysql_connect ("localhost", "*****", "*****") or die ('I cannot connect to the database because: ' . mysql_error());
    mysql_select_db ("turnupdlibrary");
    
     $result = mysql_query("SELECT `Notes` FROM `jezebel` WHERE `ID` = $ID;"); 
    
    if (!$result) {
       echo 'Could not run query: ' . mysql_error();
       exit;
    }
    $row=mysql_fetch_row($result);
        echo $row[0];
        echo $row[1];
    } 
    ?>
    </body>
    </html>
    
    
    Code (markup):
    I'm not getting an error code, but the page comes up blank.

    Thanks,
    jez
     
    jezebel, Jun 18, 2006 IP
  4. soj

    soj Well-Known Member

    Messages:
    233
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    138
    #4
    is your mysql ID or is it actually id, this will make the whole difference and not show up anything
     
    soj, Jun 18, 2006 IP
  5. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #5
    Don't use isset, use if (x != "");

    Clean the input variable or you have a huge injection hole.

    $ID = intval($_GET['ID']);

    Your ULR used lower case and your php uses uppercase to sort the id variable. So $ID will always be empty.
     
    noppid, Jun 18, 2006 IP
  6. jezebel

    jezebel Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I'm sorry...I really have no clue how I"ve gotten as far as I have with this. I now have it coded as below:

    <html>
    <body>
    <?php
    (x != "id");
    $id = $_GET['id']; 
    
     $dbh=mysql_connect ("localhost", "*****", "*****") or die ('I cannot connect to the database because: ' . mysql_error());
    mysql_select_db ("turnupdlibrary");
    
    // Get all the data from the "example" table
     $result = mysql_query("SELECT `Notes` FROM `jezebel` WHERE `id` = $id;"); 
    
    if (!$result) {
       echo 'Could not run query: ' . mysql_error();
       exit;
    }
    $row=mysql_fetch_row($result);
        echo $row[0];
        echo $row[1];
    } 
    ?>
    </body>
    </html>
    
    Code (markup):
    I now get the error message:

    Could not run query: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1.

    In the database, ID is in caps. I'll rename it to lowercase so the url with be correct.

    I appreciate all the help!

    jez
     
    jezebel, Jun 18, 2006 IP
  7. jezebel

    jezebel Peon

    Messages:
    4
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Holy smokes...its works! Thank you SO much :)


    jez
     
    jezebel, Jun 18, 2006 IP
  8. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #8
    Try working with this...

    
    <html>
    <body>
    <?php
    
    if(  $_REQUEST['id'] != '' )
    {
    	$id = intval($_REQUEST['id']);
    	$dbh=mysql_connect ("localhost", "*****", "*****") or die ('I cannot connect to the database because: ' . mysql_error());
    	mysql_select_db ("turnupdlibrary");
    
    	// Get all the data from the "example" table
    	$result = mysql_query("SELECT `Notes` FROM `jezebel` WHERE `id` = $id;"); 
    
    	if (!$result) 
    	{
    		echo 'Could not run query: ' . mysql_error();
    		exit;
    	}
    	$row=mysql_fetch_row($result);
        echo $row[0];
        echo $row[1];
    }
    else
    {
    	echo "No Request.";
    }
    ?>
    </body>
    </html>
    
    PHP:
     
    noppid, Jun 18, 2006 IP