SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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:

    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 Code:
    <?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.

  2. #2
    SitePoint Member doni's Avatar
    Join Date
    Apr 2012
    Location
    TN
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    PHP Code:
    <?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';
                
            }
        
        }
        
    ?>

  3. #3
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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\task\retrieve2.php on line 31
    nothing

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

  4. #4
    SitePoint Member doni's Avatar
    Join Date
    Apr 2012
    Location
    TN
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this instead, this is how i usually do things

    PHP Code:
            $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> ' "\n";
                            
            } else {
            
                echo 
    'nothing';
            
            } 

  5. #5
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,000
    Mentioned
    101 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?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 === ) {
        echo 
    'Sorry, there is no phone number stored for the name entered.';
    } elseif ( 
    $matches ) {
        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>";
    }

    ?>
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  6. #6
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    SpacePhoenix, with your corrections the whole php-code is as follows:
    PHP Code:
    <?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 === ) {
        echo 
    'Sorry, there is no phone number stored for the name entered.';
    } elseif ( 
    $matches ) {
        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

  7. #7
    SitePoint Member doni's Avatar
    Join Date
    Apr 2012
    Location
    TN
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $result=mysql_query($query) or die('Error, insert query failed')

    it is because of this, there is no $query maybe try replacing query with sql

  8. #8
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.

  9. #9
    SitePoint Member doni's Avatar
    Join Date
    Apr 2012
    Location
    TN
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I actually meant the to switch the query to sql , but what you did is the same, glad to be of help.

  10. #10
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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
    PHP Code:
        $message "Hello, you phone number is <b>{$row['phone']}</b>;"
         
    mail($email"Your phone number"$message"Content-type:text/plane; Charset=utf-8\r\n"); 
    returning a syntax mistake on this last line. Please show me my mistake.

  11. #11
    SitePoint Member
    Join Date
    Nov 2008
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It works now.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •