Page 1 of 1

having probem inserting data into db table

Posted: Thu Oct 29, 2009 1:48 am
by doforumda
hi

i have a table with following columns in it

Code: Select all

candidate_id, degree, cgpa, institute
and i have a dynamic form with some javascript in it. the problem in php script i think not in js. this form has two buttons when "need more fields" button and "submit" button. when the user clicks more fields button then three more fields appears. when user fills this form and press submit it it goes to db and insert there. the problem is when user fills as many fields as he wants but only first three fields enters into the db. and remaining of them does not enter. you can pick out what i am doing wrong in the following code.

Code: Select all

<script type="text/javascript">
var counter = 0;
//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('add_phone').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('add_phone');
//Inside the getElementById brackets is the name of the div class you will use.
        insertHere.parentNode.insertBefore(newFields,insertHere);
}
 
</script>
 
 
<form name="add_a_phone" action="qualificationProcess.php" method="post" enctype="multipart/form-data">
<fieldset>
<div id="phone">
    Degree:<input type="text" name="degree_0" value="" /><br>
    cgpa:<input type="text" name="cgpa_0" value="" /><br>
    institute:<input type="text" name="institute_0" value="" /><br>
</div>
<div id="add_phone" style="display: none;">
    Degree:<input type="text" name="degree_" value="" /><br>
    cgpa:<input type="text" name="cgpa_" value="" /><br>
    institute:<input type="text" name="institute_" value="" /><br>
</div>
<input type="hidden" name="lastId" value="<?php echo $lastId; ?>"/>
<input type="button" id="add_phone()" onclick="add_phone()" value="Give me more fields!" /><br>
<input type="submit" name="submit" value="submit" />
</fieldset>
</form>
 
 
<?php
   
   //echo $lastId;
 
    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
               //$id = $_POST['id_'.$i];
               $lastId = $_POST['lastId'];
                    $degree = $_POST['degree_'.$i];
                    $cgpa = $_POST['cgpa_'.$i];
               $institute = $_POST['institute_'.$i];
               
               
               $db = mysql_connect("localhost");
               mysql_select_db("jobolicious", $db);
                    $query = "INSERT INTO qualification (candidate_id, degree, cgpa, institute) VALUES ('$lastId', '$degree', '$cgpa', '$institute')";
                    $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++;
            }
        }
    }
 
?>
and one more thing is in the candidate_id column only one id will be enter. i mean when one user come and fills as many fields as he wants but for those fields this column should have only one id. and also this id is taken from other form through hidden input.

Re: having probem inserting data into db table

Posted: Thu Oct 29, 2009 5:17 am
by it2051229
Hmmm... so if i click on the add more fields a new set of text boxes appears? and if you click on the submit button then only the first set of text boxes' values gets inserted on the database? If i'm not mistaken, when you generate a new set of text boxes (when clicking add more fields), then you have to set also the name of the text box so that when you click on submit, all you have to do is to loop on the textboxes and getting one by one its values and store it into the database.

example of three fields (without javascript)

Code: Select all

 
Contact 1: <input type='text' name='contacts[]' /><br />
Contact 2: <input type='text' name='contacts[]' /><br />
Contact 3: <input type='text' name='contacts[]' /><br />
 
and assuming the user hits the submit button... then on PHP it's something like...

Code: Select all

 
$contacts = $_POST["contacts"];
 
mysql_connect("localhost", "root", "password");
mysql_select_db("mydb");
 
for($i = 0; $i < count($contacts); $i++)
{
     mysql_query("INSERT INTO..... VALUES ('$contacts[$i]')");
}
 

Re: having probem inserting data into db table

Posted: Thu Oct 29, 2009 8:59 pm
by doforumda
how can this be achieved with js

i think the problem is with id field when i give different ids then the data gets inserted but for one id it does not get insert. and this id has to be same for one user. how can this be fixed so 9 or 10 rows gets inserted for one id?

Re: having probem inserting data into db table

Posted: Thu Oct 29, 2009 9:04 pm
by JamaicanMovies
I have the same pro pls help me :)