PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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.
<?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);
?>
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.
$query = "SELECT modelid, fname, lname FROM models WHERE modelid = $modelid";
$result = mysql_query($query);
if(mysql_num_rows($result > 0)
{
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";
}
}
else
{
echo "no such model in database";
}
Parse error: syntax error, unexpected '{' in /homepages/12/d235019564/htdocs/phptest/modelsearch.php on line 32
Can you check the {} and see if they are correct. Should one of the closing {} be below the echo as the whole thing would be a block. The way it is written means the else is a n=block on its own.
<?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('xxxx', 'xxxx', 'xxxx');
if( empty($connect))
{
die ("Error. Could not connect to server");
}
if( !mysql_select_db('xxxx'))
{
die ("Error. Could not connect to specified database");
}
// This is the databse query that returns the model request
$query = "SELECT modelid, fname, lname FROM models WHERE modelid = $modelid";
$result = mysql_query($query);
[color=#FF0000]if(mysql_num_rows($result > 0)
{
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";
}
}
else
{
echo "no such model in database";
}
[/color]
// This is the end of the model request query
mysql_close($connect);
?>