SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot MediaOhgr's Avatar
    Join Date
    Jul 2001
    Location
    Calgary, Alberta, Canada
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    if - else problem

    I'm having a problem with a PHP form handler. I've got an HTML form set up to recieve data for a mysql databse. I put an if - else statement in there to check if the query was successful or not. Here's the problem ...

    When I click the 'Create Entry' button, it returns that the query was un-successful yet when I check my database, all is well and the new data inputed properly and in the right spot.

    Here's the handler:

    // create entry code
    $Array["entry_code"] = $Array[flfn] . $Array[fflln];

    // mysql link
    $link = mysql_connect("localhost", "root");

    // mysql create entry query
    $query = "INSERT INTO entries (ID, fname, lname, player01, player02, player03, player04, player05, player06, player07, player08, player09, player10, player11, player12, player13, player14, player15, goalie) VALUES ('$Array[entry_code]', '$fname', '$lname', '$player01', '$player02', '$player03', '$player04', '$player05', '$player06', '$player07', '$player08', '$player09', '$player10', '$player11', '$player12', '$player13', '$player14', '$player15', '$goalie')";

    // create entry query
    mysql_db_query("hockeypool", $query, $link);

    // if else statement to check if successful
    if(mysql_db_query("hockeypool", $query, $link)){
    print ("The entry was created successfully!");
    } else {
    print ("An error occured and the entry was not created.");
    }

    // close mysql connection
    mysql_close($link);
    I don't know! I know it must be a problem with the if - else statement as the query works just fine.

    P.S ~ I'm a screemin' PHP newbie so don't scrutinize my code too hard.
    MediaOhgr - But then again, maybe it's just me ...

  2. #2
    SitePoint Zealot theFinn's Avatar
    Join Date
    Oct 2000
    Location
    Edmonton
    Posts
    102
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1) Why are you using mysql_db_query? If you're going to be using the same DB all the time then you would save yourself alot of typing by just doing a mysql_select_db first then using mysql_query() from then on.

    2) The if/else is faling because you're doing the query twice. So the first query works fine, then when you try and insert the second time it has the same ID (probably a primary key in your DB) so the query failes. Simply remove the first mysql_db_query call and all should be well.
    - theFinn
    Creator and Project Manager, phpBB
    http://www.theFinn.net
    http://www.tarotclass.com

  3. #3
    SitePoint Zealot MediaOhgr's Avatar
    Join Date
    Jul 2001
    Location
    Calgary, Alberta, Canada
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Blame it on the Book! :)

    My book told me to do that!

    Okay, I understand the problem and the remedy but have another question if you don't mind.

    What exactly is that if - else statement doing? By the looks of it, it is simply checking to see if the connection to the database was successful. Or, is it actually making sure that the SQL statement was excuted with out errors?

    As I said, I'm a newbie. I'm trying to learn PHP while making this program for a Hockey Pool. I'll be asking a lot of questions over the coming weeks!

    Thanks a lot for you input! Much appreciated.
    MediaOhgr - But then again, maybe it's just me ...

  4. #4
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay. This code below should work better than the one above (faster and less redundant). You'll see that instead of inserting the fields then values, I've simply made it insert them by the actual values; I find that much easier to work with and you probably will too. However, if you want to keep it your way, you should; they both result in the same insertion.
    PHP Code:
    <?php
    // create entry code
    $Array["entry_code"] = $Array[flfn] . $Array[fflln];

    // connect to db or fail
    $link = @mysql_connect("localhost""root") or exit("<p>An error was encountered: " mysql_error() . "</p>");

    // select the db or fail
    $select = @mysql_select_db("hockeypool",$link) or exit("<p>An error was encountered: " mysql_error() . "</p>");

    // define the query
    $query "insert into entries set " .
    "ID='$Array[entry_code]', fname='$fname', lname='$lname', player01='$player01', player02='$player02', " .
    "player03='$player03', player04='$player04', player05='$player05', player06='$player06', player07='$player07', " .
    "player08='$player08', player09='$player09', player10='$player10', player11='$player11', player12='$player12', " .
    "player13='$player13', player14='$player14', player15='$player15', goalie='$goalie'";

    // perform the query or fail
    $sql = @mysql_query($query) or exit("<p>An error was encountered: " mysql_error() . "</p>");

    // if script executed this far, no errors occured
    echo("The entry was created succesfully!");
    mysql_close($link);
    exit();
    ?>
    As for your if-else statement near the end, it basically looks at the code and says either "yes, it worked" or "no, it did not." As Finn said, how you had it in the statement, it tries to insert the data again. You would be better off using something like:
    PHP Code:
    <?php
    $sql 
    = (mysql_db_query("hockeypool"$query$link);
    if(!
    $sql)
    {
    echo(
    "There was an error");
    }
    else
    {
    echo(
    "Entry successful");
    }
    ?>
    But I still think you should use my code at the top -- it has the or statements build right into the code after the db connection, selection, and query so you don't even have to worry about the if-else statement. Good luck!
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  5. #5
    SitePoint Zealot MediaOhgr's Avatar
    Join Date
    Jul 2001
    Location
    Calgary, Alberta, Canada
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Zoinks!

    HOLY!!! Left me in the dust there ya did!

    Actually, I can see as how your method is nice and slick and will probably use it sans the if/else. But! I'm not going to learn anything simply cutting and pasting your code. So, may I ask a few questions?

    1: What's with the @ in front of the mysql_connect?
    2: Is or exit the same thing as or die?
    3: mysql_error()? What is that there for?

    Other than that, I understand what's going on in your revision of the code.

    Thanks for your help!
    Last edited by MediaOhgr; Oct 19, 2001 at 18:27.
    MediaOhgr - But then again, maybe it's just me ...

  6. #6
    Probably eating pie mitsubishi's Avatar
    Join Date
    Sep 2001
    Location
    England, UK
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    1: It supress errors, so it doesn't spit out nonsense before you want it to.

    2: Yeah die() and exit() are eactly the same, forces an end to the script.

    3: It is the error message generated by mySQL, so you know what went wrong (hopefully nothing did).

  7. #7
    SitePoint Zealot MediaOhgr's Avatar
    Join Date
    Jul 2001
    Location
    Calgary, Alberta, Canada
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks!

    Cool! Thanks! Now I'm in the know.

    I've also been scouring the PHP manual ... what a eyesore that monster can get to be!
    MediaOhgr - But then again, maybe it's just me ...

  8. #8
    Probably eating pie mitsubishi's Avatar
    Join Date
    Sep 2001
    Location
    England, UK
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Over the pasy few weeks, the php online-manual has become one of my best friends. If you know nothing it is like it is real beast, but once you get going, invaluable.

  9. #9
    SitePoint Zealot MediaOhgr's Avatar
    Join Date
    Jul 2001
    Location
    Calgary, Alberta, Canada
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Manual

    Agreed. When I first decided that I'd like to learn PHP I downloaded the PDF. Needless to say, a few minutes of looking at that and I found myself discourged. But now that I've got my foot in the door a bit it's becoming more usable to me.

    Anyway, thanks for all your help everyone! I've now got a smokin' handler that I understand and can use in other aspects of the over all app.

    Cheers!

    P.S ~ One hurdle past me ... more await no doubt. I'll be back!
    MediaOhgr - But then again, maybe it's just me ...


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
  •