Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. Managing a database insert failure..

Managing a database insert failure..

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
databasehelpphpmysqlsysadmin
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    MacIntyre
    wrote on last edited by
    #1

    I am building a web membership site. when a new member signs up he is able the choose a username that will be used for tracking purposes, such as mike or chuck. The user name is the primary key in a MySql database. If there is already a 'chuck' in the system and a new user requests 'chuck' as a username the insert will fail, and that is OK. The php script catches the failure, the problem is that it puts an ugly message on the screen and break the process. I could change the message but that does not solve the problem. What I want is a nice little dialog box that says that another name or name not available and redisplay the original signup screen? This is my first php program (I've been writing code for more than 30 years). So I need to know how to return to the base application so the user and enter a different username. Here is a code snippet of the error location where I would like to make the appropiate change: if(!mysql_query('INSERT INTO `' . CC_FB_DB_TABLE . '` SET ' . $query . "`created_at` = NOW()", $link)) { printMessage('Unable to Insert Into Database Table.', "We're sorry but we were unable to insert the form results " . 'into your database table. Please be sure that you have ' . 'the proper permissions to insert data into the ' . CC_FB_DB_TABLE . ' table. If you are still experiencing ' . 'trouble, please contact your server administrator.'); } ***************** Thanks for your assistance in advance... Chuck

    D 1 Reply Last reply
    0
    • M MacIntyre

      I am building a web membership site. when a new member signs up he is able the choose a username that will be used for tracking purposes, such as mike or chuck. The user name is the primary key in a MySql database. If there is already a 'chuck' in the system and a new user requests 'chuck' as a username the insert will fail, and that is OK. The php script catches the failure, the problem is that it puts an ugly message on the screen and break the process. I could change the message but that does not solve the problem. What I want is a nice little dialog box that says that another name or name not available and redisplay the original signup screen? This is my first php program (I've been writing code for more than 30 years). So I need to know how to return to the base application so the user and enter a different username. Here is a code snippet of the error location where I would like to make the appropiate change: if(!mysql_query('INSERT INTO `' . CC_FB_DB_TABLE . '` SET ' . $query . "`created_at` = NOW()", $link)) { printMessage('Unable to Insert Into Database Table.', "We're sorry but we were unable to insert the form results " . 'into your database table. Please be sure that you have ' . 'the proper permissions to insert data into the ' . CC_FB_DB_TABLE . ' table. If you are still experiencing ' . 'trouble, please contact your server administrator.'); } ***************** Thanks for your assistance in advance... Chuck

      D Offline
      D Offline
      Dr Walt Fair PE
      wrote on last edited by
      #2

      You can use a try ... catch to handle the error elegantly or use @ with the function name to suppress the error message, but in my opinion, allowing a known error to happen in the first place is a poor design. I would do a simple SELECT query before to see if the name exists, then act accordingly.

      CQ de W5ALT

      Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software

      modified on Wednesday, May 19, 2010 11:32 AM

      M 1 Reply Last reply
      0
      • D Dr Walt Fair PE

        You can use a try ... catch to handle the error elegantly or use @ with the function name to suppress the error message, but in my opinion, allowing a known error to happen in the first place is a poor design. I would do a simple SELECT query before to see if the name exists, then act accordingly.

        CQ de W5ALT

        Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software

        modified on Wednesday, May 19, 2010 11:32 AM

        M Offline
        M Offline
        MacIntyre
        wrote on last edited by
        #3

        Don't like the catch an error either. My first choice is to do as you suggested. I guess what I should be asking is does php work like the code behind asp.net where once the enter key is pressed it runs the code behind, in this case the php. If so how do I get a little message box on the screen indicating that the username has alreadu been taken and to try another one?

        D 1 Reply Last reply
        0
        • M MacIntyre

          Don't like the catch an error either. My first choice is to do as you suggested. I guess what I should be asking is does php work like the code behind asp.net where once the enter key is pressed it runs the code behind, in this case the php. If so how do I get a little message box on the screen indicating that the username has alreadu been taken and to try another one?

          D Offline
          D Offline
          Dr Walt Fair PE
          wrote on last edited by
          #4

          I'm not an expert in asp.net, but PHP is just server side, so if you need to get the info back to the client browser, you'll have to do something else, like use AJAX. I have a user registration web page embedded in a PHP script (or PHP in a web page -- depends on your viewpoint) with a parameter indicating an error message. The first time you call the web page, the error message is blank so nothing is displayed. If the registration succeeds, the PHP script redirects to a login or a secure page. If it fails it reloads the registration page again with a message that the user name is already taken (or database in unavailable, or password was blank, etc.).

          CQ de W5ALT

          Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software

          M 1 Reply Last reply
          0
          • D Dr Walt Fair PE

            I'm not an expert in asp.net, but PHP is just server side, so if you need to get the info back to the client browser, you'll have to do something else, like use AJAX. I have a user registration web page embedded in a PHP script (or PHP in a web page -- depends on your viewpoint) with a parameter indicating an error message. The first time you call the web page, the error message is blank so nothing is displayed. If the registration succeeds, the PHP script redirects to a login or a secure page. If it fails it reloads the registration page again with a message that the user name is already taken (or database in unavailable, or password was blank, etc.).

            CQ de W5ALT

            Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software

            M Offline
            M Offline
            MacIntyre
            wrote on last edited by
            #5

            Walt this is great.. I really appreciate your assistance. I will look to implement this later today.. Could you answer a resource question..? I noticed a site called PHPLabs, they claim to have lots of goodies and great support for PH issues. Do you know anything about them? Thanks.. Chuck..

            D L 2 Replies Last reply
            0
            • M MacIntyre

              Walt this is great.. I really appreciate your assistance. I will look to implement this later today.. Could you answer a resource question..? I noticed a site called PHPLabs, they claim to have lots of goodies and great support for PH issues. Do you know anything about them? Thanks.. Chuck..

              D Offline
              D Offline
              Dr Walt Fair PE
              wrote on last edited by
              #6

              I'm glad my suggestions may have helped. To me, the registration and login is just the first hurdle -- after that you probably need to maintain session info, and doing that securely offers other challenges! I've never heard of PHPLabs before. I see they want to charge to be a member, so I don't think I'm interested. So far I've always found answers to my PHP questions using Bing (or Google). If you decide to delve into PHPLabs, I'd be happy to hear what you think.

              CQ de W5ALT

              Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software

              M 1 Reply Last reply
              0
              • M MacIntyre

                Walt this is great.. I really appreciate your assistance. I will look to implement this later today.. Could you answer a resource question..? I noticed a site called PHPLabs, they claim to have lots of goodies and great support for PH issues. Do you know anything about them? Thanks.. Chuck..

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Not heard of PHPLabs until today. However, here are some websites you might find of use ... http://ajax.phpmagazine.net/[^] http://phpsense.com/[^] http://www.phpandstuff.com/[^] http://www.hotscripts.com/category/php/tutorials-tips/[^]

                M 1 Reply Last reply
                0
                • D Dr Walt Fair PE

                  I'm glad my suggestions may have helped. To me, the registration and login is just the first hurdle -- after that you probably need to maintain session info, and doing that securely offers other challenges! I've never heard of PHPLabs before. I see they want to charge to be a member, so I don't think I'm interested. So far I've always found answers to my PHP questions using Bing (or Google). If you decide to delve into PHPLabs, I'd be happy to hear what you think.

                  CQ de W5ALT

                  Walt Fair, Jr., P. E. Comport Computing Specializing in Technical Engineering Software

                  M Offline
                  M Offline
                  MacIntyre
                  wrote on last edited by
                  #8

                  Thanks Walt..I'll probably save my money for now...

                  1 Reply Last reply
                  0
                  • L Lost User

                    Not heard of PHPLabs until today. However, here are some websites you might find of use ... http://ajax.phpmagazine.net/[^] http://phpsense.com/[^] http://www.phpandstuff.com/[^] http://www.hotscripts.com/category/php/tutorials-tips/[^]

                    M Offline
                    M Offline
                    MacIntyre
                    wrote on last edited by
                    #9

                    Thanks Richard.. I'll look into these sites.. Cheers.. Chuck..

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups