Forum Moderators: coopster
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> <a href='/ShowVenue.php?id=$VenueID'>View</a></td></tr></table>";
?>
All help is most appreciated.
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 :)
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.
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.
<?
$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++;
}
$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.