Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Oct 2009
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts

    how can we add data from dynamic fields to mysql db in php

    i have a script which i found on the internet. i modify that script according to my needs. what is in that script is there are three form fields with two buttons. one button is "Give me more fields" clicking on this button will give you more fields. and second button is submit so the data goes to server side and will be added to db. the problem is when i click give me more fields it gives me three more fields which is right but when i fill all these fields and click submit button it adds to the db but the data in the first three fields adds in the one row and the other three fields data adds in separate row which is not fine for me. so how can i do this so all the data will be added to only one row.

    here is js code
    Code:
    var counter = 0;
    //Start a counter. Yes, at 0
    function add_phone() {
        counter++;
    // I find it easier to start the incrementing of the counter here.
        var newFields = document.getElementById('addQualification').cloneNode(true);
        newFields.id = '';
        newFields.style.display = 'block';
        var newField = newFields.childNodes;
        for (var i=0;i<newField.length;i++) {
            var theName = newField[i].name
            if (theName)
                    newField[i].name = theName + counter;
    // This will change the 'name' field by adding an auto incrementing number at the end. This is important.
            }
            var insertHere = document.getElementById('addQualification');
    // Inside the getElementById brackets is the name of the div class you will use.
            insertHere.parentNode.insertBefore(newFields,insertHere);
    }
    here is form
    Code:
    <form name="addAQualification" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
    <fieldset>
    <div id="phone">
    	Degree:
        <input type="text" name="degree_0" value="" /><br>
        CGPA/Grade:
        <input type="text" name="cgpa_0" value="" /><br>
        Institute:
        <input type="text" name="institute_0" value="" /><br>
    </div>
    <div id="addQualification" style="display: none;">
    Degree:
        <input type="text" name="degree_" value="" /><br>
    CGPA/Grade:
        <input type="text" name="cgpa_" value="" /><br>
    Institute:
    	<input type="text" name="institute_" value="" /><br>
    </div>
    <input type="button" id="add_phone()" onclick="add_phone()" value="Give me more fields!" /><br>
    <input type="submit" name="submit" value="submit" />
    </fieldset>
    </form>
    and here is php
    Code:
    <?php
        if(isset($_POST['submit']))
    //This checks to make sure submit was clicked
        {
            echo "You clicked submit!<br>";
            echo "Here is your data<br>";
            echo "<br>";
            if ($_POST['cgpa_0'])
    //This checks that the proper field has data
            {
                    $continue = FALSE;
                    $i = 0;
                    while ($continue == FALSE)
                    {
                        if (isset($_POST['degree_'.$i]))
    //This looks for an entry after 0 and increments
                        {
                        echo $_POST['degree_'.$i] . " = " . $_POST['cgpa_'.$i] . "<br>";
    //Echoing the data
                        $degree1 = $_POST['degree_'.$i];
                        $cgpa1 = $_POST['cgpa_'.$i];
    					$institute1 = $_POST['institute_'.$i];
    					$db = mysql_connect("localhost");
    					mysql_select_db("test", $db);
                        $query = "INSERT INTO cv 
    											(
    											degree1, 
    											cgpa1, 
    											institute1
    											 ) 
    											VALUES 
    											 (
    											 '$degree1', 
    											 '$cgpa1', 
    											 '$institute1'
    											 )";
                        $result = mysql_query($query);
    //The four lines above is the example on how the data would be put into a MySQL database. It's not used here
                    }
                    else
                    {
                        $continue = TRUE;
                    }
                    $i++;
                }
            }
        }
    ?>

  • #2
    New Coder
    Join Date
    Oct 2009
    Posts
    68
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i am still waiting for help

  • #3
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Apparently, your current code's "give me more fields" functionality is designed to allow additional records to be inserted at the same time. The best option would probably be to reformat your form to accept the data for a complete record rather than only a partial record.
    Are you a Help Vampire?

  • #4
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You need a different database design, one that is more dynamic. I'm picturing a child table that holds a foreign key to a parent table, as well as a column that stores some kind of code that identifies the nature of the row, and then a value column. A classic "key-value" setup. Here's a pretty good tutorial of how that all works:

    http://www.devshed.com/c/a/MySQL/Dat...yValue-Tables/


  •  

    Posting Permissions

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