inserting data from form to database help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Vesuvius2001
    New Member
    • Nov 2006
    • 6

    inserting data from form to database help

    hi im wondering is anyone can help me with this peice of code

    what i am trying todo is to create a register page where users type information which goes into a database heres the code

    [B]FORM CODE [B/]

    [HTML]
    <html>
    <head>
    <title>Signup Form</title>
    </head>
    <body>
    <form name=form1 method=post action=register .php>
    <table width=100% border=0 cellpadding=4 cellspacing=0>
    <tr>
    <td width=24% align=left valign=top>Firs t Name</td>
    <td width=76%><inpu t name=first_name type=text id=first_name2> </td>
    </tr>
    <tr>
    <td align=left valign=top>Last Name</td>
    <td><input name=last_name type=text id=last_name></td>
    </tr>
    <tr>
    <td align=left valign=top>Emai l Address</td>
    <td><input name=email_addr ess type=text id=email_addres s></td>
    </tr>
    <tr>
    <td align=left valign=top>Desi red Username</td>
    <td><input name=username type=text id=username></td>
    </tr>
    <tr>
    <td align=left valign=top>Info rmation about you:</td>
    <td><textarea name=password id=password></textarea></td> </tr>
    <tr>
    <td align=left valign=top> </td>
    <td><input type=submit name=Submit value=Join Now!></td>
    </tr>
    </table>
    </form>
    </body>
    </html>
    [/HTML]

    DATABASE CODE register.php

    [PHP]
    <?php

    // creates a new connection object
    $adoCon = new COM("ADODB.Conn ection");

    // opens the connection using a standard Access connection string
    $adoCon->Open("Provider =Microsoft.Jet. OLEDB.4.0; Data Source=\wamp\ww w\PRU.mdb");

    { $adoCon->Execute
    ( "INSERT INTO tblUsers
    (firstname,last Name,email,user name,password)
    VALUES
    ('$_POST[first_name]','$_POST[last_name]','$_POST[email_address]','$_POST[username]','$_POST[password]');"
    );
    }

    // closes the connection, frees up resources, kills all traces
    $adoCon->Close();
    $adoCon = null;

    { adocon

    ?>
    [/PHP]

    and heres the error message after pressing the submit button

    Parse error: parse error, unexpected $end in C:\wamp\www\pru \register.php on line 23
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    You specify an opening curly brace at [php]{ adocon[/php] but where is the closing curly brace?

    Ronald :cool:

    Comment

    • Vesuvius2001
      New Member
      • Nov 2006
      • 6

      #3
      Originally posted by ronverdonk
      You specify an opening curly brace at [php]{ adocon[/php] but where is the closing curly brace?

      Ronald :cool:
      ive taken this off and and it still says the same error

      line 23 is the end of th php code i unsure whtas happening nothing is been added to the databse and i assume if the conection code was wrong it would throw up cant connect to database etc

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        And what is the function of the other curly braces as in [php]{ $adoCon->Execute[/php] and at the end of the insert? Those do not belong to any PHP statement in your code.

        Ronald :cool:

        Comment

        • Vesuvius2001
          New Member
          • Nov 2006
          • 6

          #5
          Originally posted by ronverdonk
          And what is the function of the other curly braces as in [php]{ $adoCon->Execute[/php] and at the end of the insert? Those do not belong to any PHP statement in your code.

          Ronald :cool:
          i was taught to put those in anyways ive taken them out can anyone see anyother fundamental problems on why this code isnt working

          my thoughts think its todo with not having a session but surley the $post gives the information from the form to register.php

          or if anyone knows another way of adding information into a database from a form using php could give an example i would apricate it greatly thnx guys

          Comment

          • soeter04
            New Member
            • Nov 2006
            • 12

            #6
            How about this? (removed the last line with adocon... that made little sense)
            [PHP]
            <?php

            // creates a new connection object
            $adoCon = new COM("ADODB.Conn ection");

            // opens the connection using a standard Access connection string
            $adoCon->Open("Provider =Microsoft.Jet. OLEDB.4.0; Data Source=\wamp\ww w\PRU.mdb");

            $adoCon->Execute(
            "INSERT INTO tblUsers(firstn ame,lastName,em ail,username,pa ssword) VALUES
            ('$_POST[first_name]','$_POST[last_name]','$_POST[email_address]',
            '$_POST[username]','$_POST[password]');"
            );

            // closes the connection, frees up resources, kills all traces
            $adoCon->Close();
            $adoCon = null;

            ?> [/PHP]

            Comment

            • Vesuvius2001
              New Member
              • Nov 2006
              • 6

              #7
              ive corected and tidyed up my code to

              [PHP]
              <?php
              $first = $_POST['first_name'];
              $last = $_POST['last_name'];
              $email = $_POST['email_address'];
              $user = $_POST['username'];
              $password = $_POST['password'];

              // creates a new connection object
              $adoCon = new COM("ADODB.Conn ection");

              // opens the connection using a standard Access connection string
              $adoCon->Open("Provider =Microsoft.Jet. OLEDB.4.0; Data Source=\wamp\ww w\pru\PRU.mdb") ;

              $adoCon->Execute
              ( "INSERT INTO tblusers
              (firstname,last Name,email,user name,password)
              VALUES
              ('$first','$las t','$email','$u ser','$password ');"

              );


              // closes the recordset, frees up resources, kills all traces
              $adocon->Close();
              $adocon->Release();

              ?>

              [/PHP]

              now i am getting this error message whihc i assume means there is something wrong with the insert statment

              Fatal error: Uncaught exception 'com_exception' with message '<b>Source:</b> Microsoft JET Database Engine<br/><b>Description :</b> Syntax error in INSERT INTO statement.' in C:\wamp\www\pru \register2.php: 20 Stack trace: #0 C:\wamp\www\pru \register2.php( 20): com->Execute('INSER T INTO tbl...') #1 {main} thrown in C:\wamp\www\pru \register2.php on line 20

              Comment

              • soeter04
                New Member
                • Nov 2006
                • 12

                #8
                The problem is that you do some tasks, which one might call risky or unstable... Like database connections: the connection might be refused, the credentials might be wrong, whatever...
                Instead of letting an application just crash when something goes wrong, we want to "catch" the error, and act accordingly (displaying an error message, log the error, whatever...). Usually that is achieved by something called a try/catch block.

                Haven't been using PHP since some pre PHP5 version, so im not that aware of the exact syntax used for try/catch in php...

                Read up on that here:
                http://www.phpfreaks.c om/phpmanual/page/language.except ions.html

                Comment

                • soeter04
                  New Member
                  • Nov 2006
                  • 12

                  #9
                  Maybe i should have read all of your error :)

                  Something's wrong in your query too...

                  Comment

                  • Vesuvius2001
                    New Member
                    • Nov 2006
                    • 6

                    #10
                    ive added a catch on the insert and have got this message shooting back at me lol ive spent hours looking at this code it has to be soemthing simple

                    [PHP]
                    <?php
                    $id = 90;
                    $first = $_POST['first_name'];
                    $last = $_POST['last_name'];
                    $email = $_POST['email_address'];
                    $user = $_POST['username'];
                    $password = $_POST['password'];

                    // creates a new connection object
                    $adoCon = new COM("ADODB.Conn ection");

                    // opens the connection using a standard Access connection string
                    $adoCon->Open("Provider =Microsoft.Jet. OLEDB.4.0; Data Source=\wamp\ww w\pru\PRU.mdb") ;

                    {$adoCon->Execute
                    ( "INSERT INTO tblusers
                    (id,firstname,l astName,email,u sername,passwor d)
                    VALUES
                    ($id,'$first',' $last','$email' ,'$user','$pass word');"

                    );
                    }
                    catch(exception $e)
                    { echo 'sorry - there was a problem with adding data to the databse.<br />';
                    }

                    // closes the recordset, frees up resources, kills all traces
                    $adocon->Close();
                    $adocon->Release();

                    ?>


                    [/PHP]

                    Parse error: parse error, unexpected T_CATCH in C:\wamp\www\pru \register.php on line 23

                    Comment

                    • ronverdonk
                      Recognized Expert Specialist
                      • Jul 2006
                      • 4259

                      #11
                      Where is the TRY block in your code gone to??

                      Ronald :cool:

                      Comment

                      • Vesuvius2001
                        New Member
                        • Nov 2006
                        • 6

                        #12
                        lol what does that mean :)

                        i think i gonna move onto coding the next part and see if i cant figure out whats oing on tomorow

                        Comment

                        • soeter04
                          New Member
                          • Nov 2006
                          • 12

                          #13
                          Change
                          {$adoCon->Execute
                          To

                          try {

                          $adoCon->Execute


                          a try cant exist without a catch and vice versa.

                          Comment

                          • steven
                            New Member
                            • Sep 2006
                            • 143

                            #14
                            Firstly, I would start by reading some of the basics on php.net

                            The online manual is unbelievably useful and will contain most of the answers to questions like these.

                            For example, you can't just catch an error. You have to "try" something first. So you would do

                            Code:
                            try {
                              //code to try
                            } catch (Exception $e)
                              //catch error, display $e or just carry on with another try/catch block
                            }
                            The best thing to do is echo your SQL statement during runtime, see if it looks okay, then copy and paste it intop the database prompt and see if it works directly with your Database engine.

                            Also, before trying to do your INSERT statements, attempt to see if the connection was successful. See if you have a resource id in the connection variable.

                            Comment

                            Working...