search by id

Discussion in 'PHP' started by zodiac, Nov 7, 2006.

  1. #1
    <?php
    
    include("../config.php");
    include("common.php");
    
    
    
    $result = mysql_query("Select * from url",$link);
    $num = mysql_num_rows($result);
    $n = 0;
    ?>
    <?php while($row = mysql_fetch_array($result, MYSQL_BOTH)){
    $n++;
    ?>
        <tr> 
    
    
    
    <td>&nbsp;<a href="<?php echo $row['url'];?>"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><?php echo $row['url'];?></font></a>&nbsp;&nbsp;</td>
    <td>&nbsp;has had &nbsp;&nbsp;</td>
    <td>&nbsp;<?php echo $row['count'];?>&nbsp;&nbsp;hits</td>
    
        </tr>
        <?php
    
     }?>
    PHP:
    in the database there are urls and each has an id.
    that code displays all the urls.

    what i want is a search box to search for an id and just display that url.
     
    zodiac, Nov 7, 2006 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Untested, but this should work.

    
    <?php
    
    include("../config.php");
    include("common.php");
    
    
    if ($_POST['id'])
    {
    	$query = "SELECT * FROM url WHERE id = ". intval($_POST['id']);
    }
    else
    {
    	$query = "SELECT * FROM url";
    }
    
    $result = mysql_query($query);
    $num = mysql_num_rows($result);
    $n = 0;
    
    while($row = mysql_fetch_array($result, MYSQL_BOTH))
    {
    	$n++;
    	?>
        <tr>
    		<td>&nbsp;<a href="<?php echo $row['url'];?>"><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><?php echo $row['url'];?></font></a>&nbsp;&nbsp;</td>
    		<td>&nbsp;has had &nbsp;&nbsp;</td>
    		<td>&nbsp;<?php echo $row['count'];?>&nbsp;&nbsp;hits</td>
        </tr>
      <?php
    }
    
    ?>
    
    <form action="" method="post">
    ID: <input type="text" name="id" /> <input type="submit" value="Search" />
    </form>
    
    PHP:
     
    nico_swd, Nov 8, 2006 IP
  3. zodiac

    zodiac Peon

    Messages:
    2,661
    Likes Received:
    82
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks,it worked.
     
    zodiac, Nov 8, 2006 IP