Forum Moderators: coopster

Message Too Old, No Replies

Pass a $id= value in the URL to PHP query

         

criscokid

2:54 pm on Nov 11, 2007 (gmt 0)

10+ Year Member



I'm not doing very well... I guess I'm not coming up with the correct search term to find what I want on Google so it's time to post to the forums.

I'm new to PHP mySQL. So far I've got my database, an input form that writes data to the database and a webpage that lists the entries in the databses.

What I'm now trying to do is write a page that will retrieve and display a single record based on the URL. ie: /ShowVenue.php?id=6

I'm having dificulty in writing the page / handing the ID in the URL over to the PHP code. This is what I've come up with so far:

<?
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$VenueNumber = $_GET['id'];

$query="SELECT * FROM venues ORDER WHERE VenueNumber=$VenueNumber";

mysql_close();

echo "<b><center>Database Output</center></b><br><br>";
echo "<table><tr><td>$VenueName</td><td>$VenueAddress, $VenueArea, $VenuePostCode</td><td><a href='/EditVenue.php?id=$VenueID'>Edit</a>&nbsp;<a href='/ShowVenue.php?id=$VenueID'>View</a></td></tr></table>";

?>

All help is most appreciated.

henry0

3:06 pm on Nov 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



All you need is
HERE
[webmasterworld.com]

plus this VenueNumber=$VenueNumber"; should be:

VenueNumber='$VenueNumber' ";
your problem: even if connected you are not extracting any data
it's all explained in the above linked pages

give it a try and post back with encountered problem if any :)

criscokid

6:14 pm on Nov 11, 2007 (gmt 0)

10+ Year Member



I've had a look at the thread referred to in the previous posting but it didn't help me to solve my problem.

I want to retrieve a record based on the record ID that get's typed into the web browser's URL input field - apparently I need to make use of $_GET - the other posting is based on selection from a form / selection on a page and makes use of $_POST.

PHP_Chimp

8:08 pm on Nov 11, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If the url is typed in directly then you can use the $_GET array.
This will return an array based on the query string attached to the url.

So if you have page?a=hello&b=another&c=test&test=another%20one

If you print_r($_GET) you will get something like -
Array {
['a'] => 'hello'
['b'] => 'another'
['c'] => 'test'
['test'] => 'another%20one'
}
If you are putting values directly into a query string then make sure that they are url encoded, so dont ask people to put something in with spaces, as you will get the encoded %20 version in the $_GET array.

criscokid

9:23 am on Nov 12, 2007 (gmt 0)

10+ Year Member



This is what I came up with last night - posted here for the benefit of others that may come across this thread in the future:

<?
$id=$_GET['id'];

mysql_connect(localhost,$username,$password);
mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM Venues WHERE VenueNumber=$id";

$result=mysql_query($query);

//Get the number of rows in your array for loop
$num=mysql_numrows($result);

mysql_close();

//Here's the loop that uses $num from above as a boundary. The loop will seperate each data into a unique variable then echos out each however you like before it loops back.
$i=0;
while ($i < $num) {
$VenueNumber=mysql_result($result,$i,"VenueNumber");
$VenueName=mysql_result($result,$i,"VenueName");
$VenueAddress=mysql_result($result,$i,"VenueAddress");

echo "$VenueNumber - $VenueName - $VenueAddress";

$i++;
}

eelixduppy

4:27 pm on Nov 12, 2007 (gmt 0)



Make sure you escape your variables:

$query="SELECT * FROM Venues WHERE VenueNumber='".[url=http://www.php.net/mysql-real-escape-string]mysql_real_escape_string[/url]($id)."'";

This will prevent from SQL injection.