0

I need to insert dynamic text box values into database table on same id,now I can possible for inserting values on different user id only.

when i am trying to insert dynamic text boxes values into database table on id 1,the dynamic text boxes values going to different id like 2,3,4 and so on.And also becoming zero field values of id 1.https://i.sstatic.net/MPWTt.png

here's my html and javascript code:

file name:index.php

<!DOCTYPE html>
<html>

    <head>
    <SCRIPT src="http://code.jquery.com/jquery-2.1.1.js"></SCRIPT>
        <style>
        table, th, td {
        border: 1px solid black;
        border-collapse: collapse;
        }
        th, td {
        padding: 5px;
        }
        table{
        background-image:  #eee;
        }

        </style>

    </head>

<body>

    <form name="registration" id="regis" method="post" enctype="multipart/formdata">
        <table>

                <th colspan="1">
                <th>Details Mahall members</th>
                </th>

            <tr>
                <td>Name of the person:</td>
                <td>
                    <input type="text" name="first_name1" id="fname1" placeholder="Enter name" value=""/>
                </td>
            </tr>


            <tr>
                <td>Name of the father:</td>
                <td>
                    <input type="text" name="first_name2" id="fname2" placeholder="Enter name" value=""/>
                </td>
            </tr>

            <tr>
                <td>Name of the mother:</td>
                <td>
                    <input type="text" name="first_name3" id="fname3" placeholder="Enter name" value=""/>
                </td>
            </tr>



            <tr>
                <td>Details of family members:</td><td></td>

            </tr>

            <tr>
                <td></td>
                <td>
                    <table width="100%">
                        <tr>
                            <th><center>No:</center></th><th><center>Name</center></th><th><center>Age</center></th><th><center>Relation<strong></th><th><center>Occupation<strong></th>
                        </tr>
                    </table>
                </td>
            </tr>

            <tr>
                <td></td>

                <td>
                <DIV id="product">
                        <DIV class="product-item float-clear" style="clear:both;">

                            <?php require_once("sub.php") ?>

                        </DIV>
                    </DIV></td></tr>
                <tr><td></td><td> <DIV class="btn-action float-clear">
                    <input type="button" name="add_item" value="Add More" onClick="addMore();" />
                    <input type="button" name="del_item" value="Delete" onClick="deleteRow();" />
                    <span class="success"><?php if(isset($message)) { echo $message; }?></span>
                </DIV>
                </td>


            </tr>

            <tr>
            <td>

                <input type="submit" name="save" value="Submit"/>
                <input type="reset" value="cancel"/>

            </td><td></td>
            </tr>
        </table>


    </form>
    <SCRIPT>
function addMore() {
    $("<DIV>").load("sub.php", function() {
            $("#product").append($(this).html());
    }); 
}
function deleteRow() {
    $('DIV.product-item').each(function(index, item){
        jQuery(':checkbox', this).each(function () {
            if ($(this).is(':checked')) {
                $(item).remove();
            }
        });
    });
}
</SCRIPT>
</body>
</html>

file name:sub.php

<DIV class="product-item float-clear" style="clear:both;">
<DIV class="float-left"><input type="checkbox" name="item_index[]" /></DIV>
<table cellspacing="2"> 
 <tr>

<td><DIV class="float-left"><input type="text" name="name1[]" style="width:60px"/></DIV></td>
<td><DIV class="float-left"><input type="text" name="name2[]" style="width:90px"/></DIV></td>
<td><DIV class="float-left"><input type="text" name="name3[]" style="width:62px"/></DIV></td>
<td><DIV class="float-left"><input type="text" name="name4[]" style="width:130px"/></DIV></td>
<td><DIV class="float-left"><input type="text" name="name5[]" style="width:178px"/></DIV></td>
</DIV>
</tr>
</table>

here's my php code for inserting into database

<?php
$username="root";
$password="sha12345";
$hostname="localhost";

$dbhandle = mysql_connect($hostname,$username,$password) or die("Could not connect to database");

$select = mysql_select_db("sha", $dbhandle);


    if(isset($_POST['save']))
{
    $personname = $_POST['first_name1'];
    $fathername = $_POST['first_name2'];
    $mothernname = $_POST['first_name3'];


    mysql_query("INSERT INTO sha(Name_person,Name_father,Name_mother)   VALUES ('$personname','$fathername','$mothernname')");  



        if(!empty($_POST["save"])) {


        $itemCount = count($_POST["name1"]);
        $itemValues=0;
        $query = "INSERT INTO sha (no_members,name_members,age_members,relation_members,occupation_members) VALUES ";
        $queryValue = "";
        for($i=0;$i<$itemCount;$i++) {
            if(!empty($_POST["name1"][$i]) || !empty($_POST["name2"][$i]) || !empty($_POST["name3"][$i]) || !empty($_POST["name4"][$i]) || !empty($_POST["name5"][$i])) {
                $itemValues++;
                if($queryValue!="") {
                    $queryValue .= ",";
                }
                $queryValue .= "('" . $_POST["name1"][$i] . "', '" . $_POST["name2"][$i] . "', '" . $_POST["name3"][$i] . "', '" . $_POST["name4"][$i] . "', '" . $_POST["name5"][$i] . "')";
            }
        }
        $sql = $query.$queryValue;
        if($itemValues!=0) {
            $result = mysql_query($sql);
            if(!empty($result)) $message = "Added Successfully.";
        }
    }
    }
    mysql_close();

?>

2 Answers 2

1

You can take array of inputs in HTML like name="first_name[]"

<input type="text" name="first_name[]" id="fname1" placeholder="Enter name" value=""/>
<input type="text" name="first_name[]" id="fname2" placeholder="Enter name" value=""/>
<input type="text" name="first_name[]" id="fname3" placeholder="Enter name" value=""/>

Then while fetching in php, You can just write

print_r($_POST['first_name']);

That will contain all values in array

Sign up to request clarification or add additional context in comments.

1 Comment

Really thanks for helped me bheem raj,i followed like u edited,but values not getting insert now,should i create text box in html for inserting into database table.
0

Ok, so here are the steps you can follow

Create Table users

CREATE TABLE users(
   id INT NOT NULL AUTO_INCREMENT,
   Name_person VARCHAR(50) NOT NULL,
   Name_father VARCHAR(50) NOT NULL,
   Name_mother VARCHAR(50) NOT NULL,
   PRIMARY KEY ( id )
);

Create Table family_members

CREATE TABLE family_members(
   id INT NOT NULL AUTO_INCREMENT,
   user_id INT NOT NULL,
   no_members TINYINT(2) NOT NULL DEFAULT 1,
   name_members VARCHAR(50) NOT NULL,
   age_members VARCHAR(50) NOT NULL,
   relation_members VARCHAR(50) NOT NULL,
   occupation_members VARCHAR(100) NOT NULL,
   PRIMARY KEY ( id )
);

Then in your PHP Code

mysql_query("INSERT INTO users(Name_person,Name_father,Name_mother) VALUES ('$personname','$fathername','$mothernname')");  


$user_id = mysql_insert_id();

$query = "INSERT INTO family_members (user_id,no_members,name_members,age_members,relation_members,occupation_members) VALUES ";
$queryValue = "";
for($i=0;$i<$itemCount;$i++) {
    if(!empty($_POST["name1"][$i]) || !empty($_POST["name2"][$i]) || !empty($_POST["name3"][$i]) || !empty($_POST["name4"][$i]) || !empty($_POST["name5"][$i])) {
        $itemValues++;
        if($queryValue!="") {
            $queryValue .= ",";
        }
        $queryValue .= "('".$user_id. "', '" . $_POST["name1"][$i] . "', '" . $_POST["name2"][$i] . "', '" . $_POST["name3"][$i] . "', '" . $_POST["name4"][$i] . "', '" . $_POST["name5"][$i] . "')";
    }
}
$sql = $query.$queryValue;

And further you can easily get records with JOIN or just simply

Let say you want get record of user 1

$get_user = mysql_query("SELECT * FROM users WHERE id = 1");
$user = mysql_fetch_assoc($get_user);
$user_id = $user['user_id'];


echo '<table>';
echo '<tr><th colspan="5">Recod of User '.$user['Name_person'].'</th></tr>';
$get_user_family = mysql_query("SELECT * FROM family_members WHERE user_id = '$user_id'");
while ($family = mysql_fetch_assoc($get_user_family))
{
    echo"<tr><td>".$family['no_members']."</td><td>".$family['name_members']."</td><td>".$family['age_members']."</td><td>".$family['relation_members']."</td><td>".$family['occupation_members']."</td></tr>";
}
echo '<table>';

4 Comments

Thank you,actually i can insert all values of dynamic text box already into table,but i can't able to insert on same id.
Thanks bheem raj,I can insert second query correctly,but the problem is on values goes to different id's.you can checkout my attached snapshot now,than you will get understand easily what i am trying to ask.
Oh, You should have two tables for this, let say 1. users, 2. fmembers, and should have one-to-many relation between them, LIKE: create new field user_id in fmembers. Then store Name_person,Name_father,Name_mother in users table, get user_id = mysql_insert_id(). Store other info like no_members etc in fmembers table with this user_id.
-Really thanks for helped me,now i can insert second query into family_members table,i got an error like undefined user_id,but now i cleared that error.also listed users and family_members details from both tables.really appreciated bheem raj.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.