SitePoint Sponsor

User Tag List

Results 1 to 12 of 12
  1. #1
    SitePoint Guru
    Join Date
    Jun 2001
    Location
    Australia
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Error: -- in PHP/MySQL tutorial

    Hi,

    I'm learning PHP & MySQL from a book called "Sam's Teach yourself PHP, in 24 hours". The book is great but I have been doing one of the tutorials and I keep getting "Error:", thought someone could take a look and see what is wrong.

    ------
    <html>
    <head>
    <title>Listing 12.3</title>
    </head>
    <body>
    <?php
    if ( isset( $domain ) && isset( $sex ) && isset( $domain ) )
    {
    // check user input here!
    $dberror = "";
    $ret = add_to_database( $domain, $sex, $mail, $dberror );
    if ( ! $ret )
    print "Error: $dberror<BR>";
    else
    print "Thank you very much";
    }
    else {
    write_form();
    }

    function add_to_database( $domain, $sex, $mail, $dberror )
    {
    $user = "users";
    $pass = "password";
    $db = "databasename";
    $link = mysql_pconnect( "localhost", $user, $pass );
    if ( ! $link )
    {
    $dberror = "Couldn't connect to MySQL server";
    return false;
    }
    if ( ! mysql_select_db( $db, $link ) )
    {
    $dberror = mysql_error();
    return false;
    }
    $query = "INSERT INTO domains ( domain, sex, mail )
    values ( '$domain', $sex', '$mail' )";
    if ( ! mysql_query($query, $link ) )
    {
    $dberror = mysql_error();
    return false;
    }
    return true;
    }

    function write_form()
    {
    global $PHP_SELF;
    print "<form action=\"$PHP_SELF\" method=\"POST\">\n";
    print "<input type=\"text\" name=\"domain\"> ";
    print "The domain you would like<p>\n";
    print "<input TYPE=\"text\" name=\"mail\"> ";
    print "Your mail address<p>\n";
    print "<select name=\"sex\">\n";
    print "\t<option value=\"F\"> Female\n";
    print "\t<option value=\"M\"> Male\n";
    print "</select>\n";
    print "<input type=\"submit\" value=\"submit!\">\n</form>\n";
    }
    ?>
    </body>
    </html>
    ------------

  2. #2
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Could you include the error message?

  3. #3
    SitePoint Guru
    Join Date
    Jun 2001
    Location
    Australia
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is what it prints
    Error:
    print "Error: $dberror<BR>";

  4. #4
    SitePoint Guru
    Join Date
    Jun 2001
    Location
    Australia
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is the website im testing it on

    http://www.ausforum.com/testphp/index2.php

  5. #5
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I understand. In the function add_to_database the $dberror must be passed by reference (because you want to assign it a value from inside the function), so just replace the declaration by

    PHP Code:
    function add_to_database$domain$sex$mail, &$dberror 
    Paul

  6. #6
    SitePoint Guru
    Join Date
    Jun 2001
    Location
    Australia
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Paul,

    Thanks for repling. I'm still new to this, i can't see any difference to my line above except the "&" in &$dberror.
    I have upload it again and I have another error.
    Error: You have an error in your SQL syntax near '', 'myname@myname.com' )' at line 2

  7. #7
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    First of all, Sams books are worthless and repleat with omissions and errors. Secondly, this statement is ridiculous:

    if ( isset( $domain ) && isset( $sex ) && isset( $domain ) )

    but I don't think it will give you an error. I guess my only suprise is that Sams did not have this as the statement:

    if(isset($domain) && isset( $domain ) && isset( $sex ) && isset( $domain )isset( $domain ) && isset( $sex ) && isset( $domain)

    As for your error, you are missing a single quote before:

    $sex'
    Last edited by 7stud; Jun 10, 2001 at 21:06.

  8. #8
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, now is working

    I found that you have a sintax error (now with MYSQL). On function add_to_database, look for the line

    PHP Code:
    $query "INSERT INTO domains ( domain, sex, mail ) 
    values ( '
    $domain', $sex', '$mail' )"
    And add the missing quote to $sex variable, must be

    PHP Code:
    $query "INSERT INTO domains ( domain, sex, mail ) 
    values ( '
    $domain', '$sex', '$mail' )"
    Paul
    Last edited by Paul S; Jun 10, 2001 at 21:10.

  9. #9
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Errors like that are pretty aggravating, but if you get a text editor, I use EditPlus but there are 6 or 7 on the market, with syntax highlighting those errors are fairly easy to spot once you know the line number. For instance, I couldn't see the error in your post, but when I cut and pasted the line into my text editor, it was readily apparent.

  10. #10
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by 7stud
    Hi,

    First of all, Sams books are worthless and repleat with omissions and errors. Secondly, this statement is ridiculous:
    Hey 7stud thanks for the tip

  11. #11
    SitePoint Guru
    Join Date
    Jun 2001
    Location
    Australia
    Posts
    676
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Paul & 7stud,

    You have been a great help. I tryed it on the another database and it works great.

    Im still learning alot from the book and Im only on chap 12, so I will finish it, but thanks for the insite on the books.

    I'm downloading EditPlus now. Thanks guys.

    I'll be back

  12. #12
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    24 hours - bah! I learnt everything I know about PHP in 12!


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
  •