PHP - How to Create Dynamic URLs?

2019-03-16 23:51发布

I've scoured the web for a tutorial about this simple task, but to no avail. And so I turn to you helpful comrades. Here's what I need to do:

I have a MySQL database with an Events table. I need to create a PHP web page with a list of the Event titles, and each title must be a link to the full details of the Event. But I want to avoid having to create a static page for each event, primarily because I don't want the data entry volunteer to have to create these new pages. (Yes, I realize that static pages are more SEO friendly, but I need to forego that in this case for the sake of efficiency.)

I've seen PHP url syntax with something like this:

pagename.php?id=20

but I don't know how to make it work.

Any and all help greatly appreciated.

Thanks!

Kip

5条回答
老娘就宠你
2楼-- · 2019-03-17 00:06

It's wise to take extra care when you are using $_GETvariables, because them can be easily altered by a malicious user.

Following with the example, you could do:

 $foo = (int)$_GET['id'];

So we are forcing here the cast of the variable to a integer so we are sure about the nature of the data, this is commonly used to avoid SQL injections.

查看更多
可以哭但决不认输i
3楼-- · 2019-03-17 00:10

lets say you have the php file test.php

<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("db", $conn);
$id = $_GET['id'];
$sql = "select * from table where id = $id";
$result = mysql_query($sql, $conn);

if ($result){
    $row = mysql_fetch_row($result);
    $title = $row[0];
    $content = $row[1];
    }
?>

<html>
    <head>
    <title><?php echo $title ?></title>
    </head>
    <body>
        <h1><?php echo $title ?></h1>
        <p><?php echo $content ?></p>
    </body>
</html>

a dynamic page would be something like that..

查看更多
孤傲高冷的网名
4楼-- · 2019-03-17 00:11

Here is the pertinent code for extracting a list of events in November from a table named calendar, with each event having a link to a page called event.php and with the event's id field appended to the end of the url:

$result = mysql_query("SELECT * FROM calendar WHERE sort_month='11'");  
while($row = mysql_fetch_array($result))  
{echo  
"<a href='event.php?id=".$row['id']."'>".$row['event_name']."</a>"  
;}  

And here is the pertinent code on the event.php page. Note the row numbers in brackets depends on the placement of such in your table, remembering that the first row (field) would have the number 0 inside the brackets:

$id = $_GET['id'];  
$sql = "select * from calendar where id = $id";  
$result = mysql_query($sql, $con);  
if ($result){  
$row = mysql_fetch_row($result);  
$title = $row[12];  
$content = $row[7];  
}  
?>  

<html>  
<head>  
<title><?php echo $title ?></title>  
</head>  
<body>  
<h1><?php echo $title ?></h1>  
<p><?php echo $content ?></p>  
</body>  
</html>  

This works for me, thanks to the help from those above.

查看更多
女痞
5楼-- · 2019-03-17 00:12

This is basic php. You would simply query the DB for the event details before the page headers are written and write the html accordingly.

The first thing I would ask you is if you know how to connect to your database. From there, you query based on the $_GET['id'] value and use the results to populate your html.

Not to be rude, but the question itself suggests you're new to PHP, right? So in order to provide a solution that works we might want to know just how far you got.

Also, you can rewrite your dynamic urls to appear like static ones using apache's mod_rewrite. It's probably a novice level thing if you're interested in "pretty" url's.

MODIFIED ANSWER:

In your loop you would use the id from the query result (assuming your primary key is id)...

while($field = mysql_fetch_array($result)) { 
    echo "<p class='date'>";
    echo $field['month']." ".$field['day'].", ".$field['year'];
    echo "</p>";
    echo "<h3>";
    echo '<a href="/somepage.php?id='.$field['id'].'">'.$field['event_name'].'</a>';
    echo "</h3>"; 
}

Then on somepage.php you would use the get var id to pull the relevant info...

$result = mysql_query("SELECT * FROM `calendar` WHERE `id` = '".mysql_real_escape_string($_GET['id'])."');

don't forget to look into mysql_real_escape_string() for cleaning entries.

查看更多
Bombasti
6楼-- · 2019-03-17 00:17
$foo=$_GET['id'];

in your example $foo would = 20

查看更多
登录 后发表回答