Error while retrieving an unexisting data entry - nee help

Hi,

There’s a form where user enters his name to retrieve other info (phone) entered by him earlier. Here’s the html-code:

<form action="retrieve.php" name="secondForm" id="secondForm" method=post>
<input name="name" class="required" type=text ><br>
<input type="submit" >
</form>

the php-code is as follows:

<?php
$username="root";
$password="123456";
$database="phonebook";
//connect to database
mysql_connect(localhost,$username);
@mysql_select_db($database) or die("<b>Unable to specified database</b>");
$name=$_POST['name'];
//perform query
$query = "select * from contact where name='$name'";
$result=mysql_query($query) or die('Error, insert query failed');
//get result
$phone=mysql_result($result,0,"phonenumber");
echo "Your phone number is <b> $phone</b> ";

?>

the problem is, that when a user enters a non-existing name and submits it, he gets mysql warning and error. How do I make it that he get a message that no such entry exits without showing him an underfind error from mysql? I know it’s something simple but I cannot solve it myself.

Try this:


<?php

    $username="root";
    $password="123456";
    $database="phonebook";
    
//connect to database
    mysql_connect(localhost,$username);
    @mysql_select_db($database) or die("<b>Unable to specified database</b>");
    
    $name=$_POST['name'];
    
//perform query

    $query = "select * from contact where name='$name'";
    $result=mysql_query($query) or die('Error, insert query failed');

    //get result
        
        $phone = mysql_result($result,0,"phonenumber");
        
        if ( $phone ) {
        
            echo "Your phone number is <b> $phone</b> "; 
            
        } else {
        
            echo 'nothing';
            
        }
    
    }
    
?>

doni, still no luck. After the above correction of the code I still get this:
Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 3 in Z:\home\localhost\www\ ask\retrieve2.php on line 31
nothing

Line 31 is as follows: $phone = mysql_result($result,0,“phonenumber”);

Try this instead, this is how i usually do things


        $query = 'select * from contact where name="' . $name . '"';
        $result = mysql_query($query);
        
        if ( $result ) {
        
            $phone = mysql_fetch_array( $result );
                    
            echo 'Your phone number is <b>' . $phone['phonenumber'] . '</b> ' . "\
";
                        
        } else {
        
            echo 'nothing';
        
        }

<?php

/*
 * Code for connecting to mysql and selecting database goes here
 */

$name=mysql_escape_string($_POST['name']);

$sql="
    SELECT
        phonenumber
    FROM
        contact
    WHERE
        name = '$name'
";

$result=mysql_query($query) or die('Error, insert query failed');

$matches = mysql_num_rows($result);

if ( $matches === 0 ) {
    echo 'Sorry, there is no phone number stored for the name entered.';
} elseif ( $matches > 1 ) {
    echo 'Sorry, there was a problem finding the phone number, please contact the site administrator.';
} else {
    $row = mysql_fetch_assoc($result);
    echo "Your phone number is <b>{$row['phonenumber']}</b>";
}

?>

SpacePhoenix, with your corrections the whole php-code is as follows:

<?php


    $username="root";
    $password="123456";
    $database="phonebook";

//connect to database
    mysql_connect(localhost,$username);
    @mysql_select_db($database) or die("<b>Unable to specified database</b>");



//perform query

   $name=mysql_escape_string($_POST['name']);

$sql="
    SELECT
        phonenumber
    FROM
        contact
    WHERE
        name = '$name'
";

$result=mysql_query($query) or die('Error, insert query failed');

$matches = mysql_num_rows($result);

if ( $matches === 0 ) {
    echo 'Sorry, there is no phone number stored for the name entered.';
} elseif ( $matches > 1 ) {
    echo 'Sorry, there was a problem finding the phone number, please contact the site administrator.';
} else {
    $row = mysql_fetch_assoc($result);
    echo "Your phone number is <b>{$row['phonenumber']}</b>";
}
?>

and now it shows an error message (Error, insert query failed) even if a valid entry in submitted:(

$result=mysql_queryCOLOR=#007700 or die(‘Error, insert query failed’[COLOR=#007700])

it is because of this, there is no $query maybe try replacing query with sql
[/COLOR][/COLOR]

Doni, I hope I understood you correctly, so I added the following (but I’m wondering if you meant this or something else) $query = “select * from contact where name=‘$name’”; and it seems to work finally. Thank you both, guys, you helped a lot.

I actually meant the to switch the query to sql , but what you did is the same, glad to be of help.

And another question, if I need change the code so that the user doesn’t see his phone number displayed upon submit but gets a message that he will receive an e-mail with the phone, which function do I use to send the retrieved data to the submitted e-mail address?

I added the following to the end of the script to achieve the above but it didn’t work


	$message = "Hello, you phone number is <b>{$row['phone']}</b>;"
     mail($email, "Your phone number", $message, "Content-type:text/plane; Charset=utf-8\\r\
");

returning a syntax mistake on this last line. Please show me my mistake.

It works now.