Insert Data From a Form Into a Database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ankit Diwan
    New Member
    • Dec 2013
    • 17

    Insert Data From a Form Into a Database

    While inserting data from a form into a database i am getting error in the below code only. The error displayed is :( ! ) Parse error: syntax error, unexpected '' (T_ENCAPSED_AND _WHITESPACE), expecting ']'
    Code:
    $sql= "INSERT INTO Persons (FirstName,LastName,Age) VALUES('$_POST[FirstName ]','$_POST[LastName]','$_POST[Age]')";
    
       if (!mysqli_query($con,$sql))
       {
       die('Error: ' . mysqli_error($con));
       }
       echo "1 record added";
    
       mysqli_close($con); 
       ?>
    what does the displayed error mean? what is the possible solution?
  • zmbd
    Recognized Expert Moderator Expert
    • Mar 2012
    • 5501

    #2
    check this $_POST[FirstName ]'
    should it be this $_POST[FirstName]' ?

    Comment

    • Ankit Diwan
      New Member
      • Dec 2013
      • 17

      #3
      The proposed solution does not work. The following error was displayed:
      Database mi_db created successfullyTab le persons created successfully
      ( ! ) Notice: Undefined index: FirstName in C:\wamp\www\ins ert.php on line 29
      Call Stack
      # Time Memory Function Location
      1 0.0027 252088 {main}( ) ..\insert.php:0

      ( ! ) Notice: Undefined index: LastName in C:\wamp\www\ins ert.php on line 29
      Call Stack
      # Time Memory Function Location
      1 0.0027 252088 {main}( ) ..\insert.php:0

      ( ! ) Notice: Undefined index: Age in C:\wamp\www\ins ert.php on line 29
      Call Stack
      # Time Memory Function Location
      1 0.0027 252088 {main}( ) ..\insert.php:0
      1 record added

      Please suggest the possible solution...?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        The proposed solution does not work.
        it did work. you just hit the next problem the previous error was hiding (because it triggered a script exit first).

        Comment

        • Rabbit
          Recognized Expert MVP
          • Jan 2007
          • 12517

          #5
          The next error being that the variable with those names were not passed to the PHP page.

          Comment

          • Ankit Diwan
            New Member
            • Dec 2013
            • 17

            #6
            So what is the solution to the above error?

            Comment

            • Ankit Diwan
              New Member
              • Dec 2013
              • 17

              #7
              The code is:
              Code:
               
              <?php 
              // Connects to your Database 
              $con = mysqli_connect("localhost", "root", "mysql@777") or die(mysqli_error());
              $db_selected = mysqli_select_db($con, 'mi_db');
              if (!$db_selected) 
              {
              die ('Can\'t use mi_db : ' . mysqli_error());
              }
              $sql= "INSERT INTO Persons (FirstName,LastName,Age) VALUES('$_POST[FirstName]','$_POST[LastName]','$_POST[Age]')";
              
              if (!mysqli_query($con,$sql))
              {
              die('Error: ' . mysqli_error($con));
              }
              echo "1 record added";
              
              mysqli_close($con); 
              ?>
              enclosed herewith the code of the above program.Please suggest in view of the above code possible solution of the same.

              Comment

              • zmbd
                Recognized Expert Moderator Expert
                • Mar 2012
                • 5501

                #8
                New Question, New Thread.
                Respectfully, we ask that each thread contain only one question.
                Please start a new thread and, if needed, you can include a link to this thread for context.

                Thnx

                Comment

                • Ankit Diwan
                  New Member
                  • Dec 2013
                  • 17

                  #9
                  Ok & Thanks

                  I have posted a new thread linked to this as :PHP script for inserting a form data to a database. Suggest Possible solution of the problem?

                  Comment

                  Working...