Page 1 of 1

mysql_query validation question

Posted: Tue Mar 04, 2008 6:30 am
by RichG
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.

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);   
 
?>
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

Re: mysql_query validation question

Posted: Tue Mar 04, 2008 6:46 am
by N1gel
I would just test the number of rows returned in your result. using mysql_num_rows

Something like this

Code: Select all

 
 
$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";
}
 
Hope this is what you mean. Hope it helps

Nigel :D

Re: mysql_query validation question

Posted: Tue Mar 04, 2008 7:08 am
by RichG
Hi

Thanks

I think that could be the way but i got

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.

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('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);   
 
?>
 
Cheers

RichG

Re: mysql_query validation question

Posted: Tue Mar 04, 2008 7:42 am
by N1gel
Sorry i missed of a )

change line 31 to be

if(mysql_num_rows($result) > 0)