Newbi

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mickey

    #1

    Newbi

    I am getting the following error at the line
    mysql_free_resu lt($result);


    Warning: Supplied argument is not a valid MySQL result resource in
    /var/www/html/php/phoneadd.php on line 27

    My code is as follows:

    <html>

    <body>
    <?php

    if ($submit) {

    // process form

    $db = mysql_connect(" localhost", "", "")
    or die("Could not connect : " . mysql_error());





    mysql_select_db ("test",$db) ;

    $sql = "INSERT INTO phone (fname,lname,st reet,h_phone) VALUES
    ('$fname','$lna me','$street',' $h_phone')";

    $result = mysql_query($sq l);

    echo "Thank you! Information entered.\n";


    /* Free resultset */
    mysql_free_resu lt($result);


    /* Closing connection */
    mysql_close($db );


    } else{
    // display form

    ?>


    <form method="post" action="<?php echo $PHP_SELF?>">
    <table>
    <tr>
    <td>First name: </td><td><input type="Text" name="fname"></td>
    </tr>
    <tr>
    <td>Last name: </td><td> <input type="Text" name="lname"></td>
    </tr>
    <tr>

    <td>Address: </td><td><input type="Text" name="street"></td>
    </tr>
    <tr>

    <td>Home Phone:</td><td> <input type="Text" name="h_phone"> </td>
    </tr>
    <tr>

    <td></td><td> <input type="Submit" name="submit" value="Enter information">
    </td>
    </tr>
    </table>
    </form>

    <?php
    } // end if



    ?>

    </body>

    </html>

  • Justin Koivisto

    #2
    Re: Newbi

    mickey wrote:[color=blue]
    > I am getting the following error at the line
    > mysql_free_resu lt($result);
    >
    > Warning: Supplied argument is not a valid MySQL result resource in
    > /var/www/html/php/phoneadd.php on line 27
    >
    > My code is as follows:[/color]

    <snip>
    [color=blue]
    > $sql = "INSERT INTO phone (fname,lname,st reet,h_phone) VALUES
    > ('$fname','$lna me','$street',' $h_phone')";
    >
    > $result = mysql_query($sq l);
    > echo "Thank you! Information entered.\n";
    >
    > /* Free resultset */
    > mysql_free_resu lt($result);[/color]

    Quote from http://us2.php.net/mysql_query :

    "Only for SELECT,SHOW,EXP LAIN or DESCRIBE statements mysql_query()
    returns a resource identifier or FALSE if the query was not executed
    correctly. For other type of SQL statements, mysql_query() returns TRUE
    on success and FALSE on error. A non-FALSE return value means that the
    query was legal and could be executed by the server. It does not
    indicate anything about the number of rows affected or returned. It is
    perfectly possible for a query to succeed but affect no rows or return
    no rows."

    You are trying to do an INSERT query, therefore, $result with be
    *either* TRUE or FALSE, not a resource identifier as expected by
    mysql_free_resu lt...
    --
    Justin Koivisto - spam@koivi.com
    PHP POSTERS: Please use comp.lang.php for PHP related questions,
    alt.php* groups are not recommended.

    Comment

    • Matthias Esken

      #3
      Re: Newbi

      mickey wrote:
      [color=blue]
      > if ($submit) {[/color]

      Your code won't run if PHP is configured with the default values. This
      should be:

      if (isset($_POST['submit'])) {

      [color=blue]
      > $sql = "INSERT INTO phone (fname,lname,st reet,h_phone) VALUES
      > ('$fname','$lna me','$street',' $h_phone')";
      >
      > $result = mysql_query($sq l);
      >
      > echo "Thank you! Information entered.\n";
      >
      > /* Free resultset */
      > mysql_free_resu lt($result);[/color]

      You don't have a result set here. You did an INSERT, not a SELECT.
      [color=blue]
      > <form method="post" action="<?php echo $PHP_SELF?>">[/color]

      Same as my first comment. This won't work with a default installation of
      PHP. Use

      <form method="post" action="<?php echo($_SERVER['PHP_SELF']); ?>">


      I know that you didn't ask for the additional comments about PHP
      defaults. But what will you do if your project has to run on a machine
      with PHP defaults? So read the documentation, change your code now and
      you won't have problems concerning compatibility between different
      installations in the future.


      Regards,
      Matthias

      Comment

      • Andy Hassall

        #4
        Re: Newbi

        On Thu, 29 Jul 2004 21:03:32 GMT, mickey <meshulamtemp@y ahoo.com> wrote:
        [color=blue]
        >if ($submit) {[/color]

        Not defined anywhere, assumes register_global s is on, which is deprecated.
        [color=blue]
        > $db = mysql_connect(" localhost", "", "")
        > or die("Could not connect : " . mysql_error());[/color]

        Here you check for errors.
        [color=blue]
        > mysql_select_db ("test",$db) ;[/color]

        Here you don't.
        [color=blue]
        > $sql = "INSERT INTO phone (fname,lname,st reet,h_phone) VALUES
        >('$fname','$ln ame','$street', '$h_phone')";[/color]

        Wide open for SQL injection attacks - you're trusting user input. Never trust
        user input, treat it as evil data.
        [color=blue]
        > $result = mysql_query($sq l);[/color]

        Here you don't check for errors.
        [color=blue]
        > echo "Thank you! Information entered.\n";[/color]

        That's not necessarily true, you haven't checked whether it was entered.
        [color=blue]
        > mysql_free_resu lt($result);[/color]

        Not needed; insert statements don't return a result set resource identifier
        that needs closing in the first place, and even if they did, would fail if
        there was an error - you can't close FALSE.

        --
        Andy Hassall <andy@andyh.co. uk> / Space: disk usage analysis tool
        http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space

        Comment

        Working...