Submit Your Article Forum Rules

Page 1 of 3 123 LastLast
Results 1 to 10 of 21

Thread: PHP, check if a record was inserted via MySQL...

  1. #1
    WebProWorld MVP morestar's Avatar
    Join Date
    Jun 2007
    Location
    Toronto, Ontario (Burlington)
    Posts
    4,249

    PHP, check if a record was inserted via MySQL...

    So basically I'm trying to determine of a record was actually inserted into my database with the insert statement in MySQL.

    My code is as follows:

    Code:
    $sQuery = "INSERT INTO `$database`.`Users` 
    (`User` ,`Email` ,`Email_verified`)
    VALUES (NULL , '$sEmail', '0')";
    mysql_query($sQuery);
    From here I would like to send an email to the new member but before I do I need to know for sure that the record was inserted.

    I have already setup checks before this step, that makes sure everything is totally ok before the insert but I would like it as a last precaution.

    If anyone knows the proper syntax to check if a record was inserted into the MySQL database that would be great. I'm still going to search the net and find it...if I do I will post it here...
    Join a free dating site and meet single people in your area.
    Submit your content at my content publishing site and promote your business, services or opinions.

  2. #2
    Senior Member Uncle Dog's Avatar
    Join Date
    Apr 2008
    Posts
    342

    Re: PHP, check if a record was inserted via MySQL...

    if (mysql_query($sQuery))
    {
    echo "Record inserted";
    }
    else
    {
    echo "Error inserting record: " . mysql_error();
    }

  3. #3
    WebProWorld MVP morestar's Avatar
    Join Date
    Jun 2007
    Location
    Toronto, Ontario (Burlington)
    Posts
    4,249

    Re: PHP, check if a record was inserted via MySQL...

    Thank you UD, once I get the fricken mail to send, I'll add in the insert check...

    thanks again man...
    Join a free dating site and meet single people in your area.
    Submit your content at my content publishing site and promote your business, services or opinions.

  4. #4
    WebProWorld MVP morestar's Avatar
    Join Date
    Jun 2007
    Location
    Toronto, Ontario (Burlington)
    Posts
    4,249

    Re: PHP, check if a record was inserted via MySQL...

    Yep it worked! I can't believe how long it actually takes to write a perfectly error proof registration form...

    I added javascript validation and then had to write server side validation in case the javascript is turned off or not working for the user...added a little ajax to check if the username exists - in real time...

    whew...that's done...

    Thanks Uncle Dog...
    Join a free dating site and meet single people in your area.
    Submit your content at my content publishing site and promote your business, services or opinions.

  5. #5
    WebProWorld MVP danlefree's Avatar
    Join Date
    Jun 2005
    Posts
    414

    Re: PHP, check if a record was inserted via MySQL...

    You might want to try variable assignment instead of wrapping the mysql_query() call in an if/else statement (things can get tricky otherwise).

    PHP Code:
    $flag_inserted mysql_query($sQuery);

    if ( 
    $flag_inserted ) {

      
    // Success handling
      // . . .

    } else {

      
    // Error handling
      // . . .


    (Then you can chain multiple conditions)

    In the future you may want to look at the Return Values section of the PHP manual for functions you are using.
    Dan LeFree | Owner/Operator (Web development, marketing)

  6. #6
    WebProWorld MVP morestar's Avatar
    Join Date
    Jun 2007
    Location
    Toronto, Ontario (Burlington)
    Posts
    4,249

    Re: PHP, check if a record was inserted via MySQL...

    Ya I looked at the link you provided before I posted here...I couldn't quite figure out the syntax or way of coding it with those examples...

    Now what do you mean by chaining multiple conditions?
    Join a free dating site and meet single people in your area.
    Submit your content at my content publishing site and promote your business, services or opinions.

  7. #7
    WebProWorld MVP morestar's Avatar
    Join Date
    Jun 2007
    Location
    Toronto, Ontario (Burlington)
    Posts
    4,249

    Re: PHP, check if a record was inserted via MySQL...

    Ya I did as you mentioned..

    Code:
    $result=mysql_query($sQuery);
       if($result){
         /// do this
       } else {
        dothat();
    }
    Join a free dating site and meet single people in your area.
    Submit your content at my content publishing site and promote your business, services or opinions.

  8. #8
    WebProWorld MVP wige's Avatar
    Join Date
    Jun 2006
    Posts
    3,138

    Re: PHP, check if a record was inserted via MySQL...

    Another (tricky) way to do it is to get the insert id of the newly created record, assuming you are entering a new row into the database. This can be used in two ways - to confirm that the row was created successfully, and to perform additional work on the entry or to link other tables to the new entry. The function, run after the insert query, is mysql_insert_id(), which will return 0 if no new id was created. You can get more info and some samples here: PHP: mysql_insert_id - Manual

    Another good method, which I often use, is to simply test if an error has been set. If the error id is 0, the previous command executed correctly, otherwise call the error handler... if (mysql_errno()) echo mysql_error;
    The best way to learn anything, is to question everything.
    WigeDev - Freelance web and software development

  9. #9
    WebProWorld MVP danlefree's Avatar
    Join Date
    Jun 2005
    Posts
    414

    Re: PHP, check if a record was inserted via MySQL...

    Quote Originally Posted by morestar View Post
    Now what do you mean by chaining multiple conditions?
    There may be some cases where multiple queries must succeed (or other conditions must be met) before you perform an action - for example, if your form requires that the user be inserted and a registration e-mail be sent, you might want to cancel the user insert if there is a problem sending the e-mail.

    (The mail() command will return FALSE on error as well)
    Dan LeFree | Owner/Operator (Web development, marketing)

  10. #10
    WebProWorld MVP morestar's Avatar
    Join Date
    Jun 2007
    Location
    Toronto, Ontario (Burlington)
    Posts
    4,249

    Re: PHP, check if a record was inserted via MySQL...

    Very well wige and danlefree, thank you for your advice(s). I'm noticing that my php coding skills aren't nearly as advanced as some of the code I'm seeing out there...after I launch this new site I just might get a more experienced someone to take a peek at the code and optimize it as much as possible.

    the sign up form is done except now I have to code to prevent sql injection...

    wish me luck!
    Join a free dating site and meet single people in your area.
    Submit your content at my content publishing site and promote your business, services or opinions.

Page 1 of 3 123 LastLast

Similar Threads

  1. single quote in mysql record
    By kruser in forum Database Discussion Forum
    Replies: 7
    Last Post: 05-25-2011, 06:59 AM
  2. Current Loan rates dynamicly inserted into site
    By DCnotPC in forum Webmaster Resources Discussion Forum
    Replies: 2
    Last Post: 01-18-2008, 02:44 PM
  3. How a message sent through email can be inserted in database on serverside?
    By sinnan in forum Web Programming Discussion Forum
    Replies: 1
    Last Post: 08-07-2007, 08:24 AM
  4. How do I remove the Dreamweaver Check In/Check Out Feature??
    By WebMasterKrames in forum Graphics & Design Discussion Forum
    Replies: 2
    Last Post: 03-28-2007, 11:22 AM
  5. Why is this line being inserted...?
    By Dragonsi in forum Internet Security Discussion Forum
    Replies: 4
    Last Post: 11-30-2004, 05:11 PM

Posting Permissions

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