PHP script for inserting a form data to a database

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

    PHP script for inserting a form data to a database

    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.
    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...?
  • Ankit Diwan
    New Member
    • Dec 2013
    • 17

    #2
    The above was the insert.php file which is linked to the html form to be linked to the database.The form code is given below:
    Code:
    <html>
    <body>
    
    <form action="insert.php" method="post">
    Firstname: <input type="text" name="Firstname">
    Lastname: <input type="text" name="Lastname">
    Age: <input type="text" name="Age">
    <input type="submit">
    </form>
    
    </body>
    </html>
    Please suggest possible solution?
    Last edited by Ankit Diwan; Jan 2 '14, 10:10 PM. Reason: ( Link to a Previous Query : Insert Data From a Form Into a Database)

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      do you have any idea what the error "Undefined Index" could mean?

      Comment

      • Ankit Diwan
        New Member
        • Dec 2013
        • 17

        #4
        No..Idea. Please suggest possible solution...?

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          what do you think could it mean?

          Comment

          • Luuk
            Recognized Expert Top Contributor
            • Mar 2012
            • 1043

            #6
            If i migh suggest a possible solution?
            try: http://lmgtfy.com/?q=php+undefined+index

            Comment

            • Ankit Diwan
              New Member
              • Dec 2013
              • 17

              #7
              I tried the link as suggested but I am getting the error as:

              ( ! ) Parse error: syntax error, unexpected '(', expecting ']' in C:\wamp\www\ins ert.php on line 31

              The code implemented is as :
              $sql= "INSERT INTO Persons (FirstName(30), LastName(30),Ag e(11))

              // line 31//VALUES(if(isset ('$_POST[FirstName(30)],[LastName(30)],[Age(11)]')){$notify ='$_POST([FirstName(30)],[LastName(30)],[Age(11)])'})";

              if (!mysqli_query( $con,$sql))
              {
              die('Error: ' . mysqli_error($c on));
              }
              echo "1 record added";

              mysqli_close($c on);
              ?>
              Please suggest?

              Comment

              • Ankit Diwan
                New Member
                • Dec 2013
                • 17

                #8
                Code:
                $sql= "INSERT INTO Persons (FirstName(30),LastName(30),Age(11))
                
                // line 31//VALUES(if(isset('$_POST[FirstName(30)],[LastName(30)],[Age(11)]')){$notify ='$_POST([FirstName(30)],[LastName(30)],[Age(11)])'})";
                
                if (!mysqli_query($con,$sql))
                {
                die('Error: ' . mysqli_error($con));
                }
                echo "1 record added";
                
                mysqli_close($con);
                ?>

                Comment

                • Ankit Diwan
                  New Member
                  • Dec 2013
                  • 17

                  #9
                  Actually i have used isset() function in PHP which determines whether a variable is set and is not NULL. It returns a Boolean value, that is, if the variable is set it will return true and if the variable value is null it will return false.

                  But due to the above error the database is also not being created and the inserting of form data to a database is also not possible.

                  So could someone please suggest ways to execute the entire code without any error or warning?

                  Comment

                  • Luuk
                    Recognized Expert Top Contributor
                    • Mar 2012
                    • 1043

                    #10
                    You are asking a PHP question, but when reading your code, you should first learn about (My)SQL.

                    The proper syntax to add a record should not start with:
                    Code:
                    INSERT INTO Persons (FirstName(30),LastName(30),Age(11))
                    because your database does not know 'FirstName(30)' , 'LastName(30)' and 'Age(11)'.

                    where did you find that syntax?

                    try (as an example):
                    Code:
                    INSERT INTO Persons (FirstName,LastName,Age)
                    VALUES ('Ankit','Diwan',0);
                    Also the PHP-code
                    Code:
                    $_POST[LastName]
                    is wrong.

                    It should look like
                    Code:
                    $_POST['Lastname']
                    becuase in you HTML-form your wrote:
                    Code:
                    Lastname: <input type="text" name="Lastname">

                    Comment

                    • Ankit Diwan
                      New Member
                      • Dec 2013
                      • 17

                      #11
                      Can you please suggest me a site for learning MySQL and PHP that has proper syntax?

                      Because I am new to PHP/MySQL...and i am facing various syntax related issues.

                      Comment

                      Working...