mysql_query validation question
Posted: Tue Mar 04, 2008 6:30 am
Hi
I am querying a database and displaying return result based on an id number entered on a prior page. I have retrieved the data and displayed it but I now want to validate the request by checking whether the data exists. Meaning if the search is for a number not in the database it will return "no such model in database" I have tried a few things but just get a blank page.
This is the code that works ok I am just unsure of where my new code will go to perform the task.
I am searching the archives for this but as I am new I thought I would post it anyway to get used to posting a topic with code.
Cheers
RichG
I am querying a database and displaying return result based on an id number entered on a prior page. I have retrieved the data and displayed it but I now want to validate the request by checking whether the data exists. Meaning if the search is for a number not in the database it will return "no such model in database" I have tried a few things but just get a blank page.
This is the code that works ok I am just unsure of where my new code will go to perform the task.
Code: Select all
<?php
// Create short variable names
$modelid = $_POST['modid'];
//This check for empty fields
if ( empty($_POST['modid']))
{
echo 'You have not filled in all of the fields. Please go back and try again.<br><br>';
echo "<a href=\"admin.html\">Back</a>";
exit;
}
//This connects to the datatbase and checks connection
$connect = mysql_connect('xxx', 'xxx', 'xxx');
if( empty($connect))
{
die ("Error. Could not connect to server");
}
if( !mysql_select_db('xxx'))
{
die ("Error. Could not connect to specified database");
}
[color=#FF0000] // This is the databse query that returns the model request
$query = "SELECT modelid, fname, lname FROM models WHERE modelid = $modelid";
$result = mysql_query($query);
while ($row = mysql_fetch_object($result)){
echo "<h2>Here is your model:</h2><br>\n Model ID: $row->modelid<br>\n First Name:
$row->fname<br>\n Second Name: $row->lname<br>\n";
}
[/color]
// This is the end of the model request query
mysql_close($connect);
?>Cheers
RichG