javascript - 如何使用 php 将动态文本框值插入到同一 ID 上的数据库表中?

标签 javascript php jquery mysql database

我需要将动态文本框值插入到同一 ID 上的数据库表中,现在我可以只在不同的用户 ID 上插入值。

当我尝试将动态文本框值插入到 id 1 上的数据库表中时,动态文本框值将转到不同的 id,如 2、3、4 等。并且也会变成 id 1 的零字段值。http://i.stack.imgur.com/MPWTt.png

这是我的 html 和 javascript 代码:

文件名: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>

文件名: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>

这是我的用于插入数据库的 php 代码

<?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();

?>

最佳答案

您可以在 HTML 中获取输入数组,例如 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=""/>

然后在php中获取时,你可以写

print_r($_POST['first_name']);

这将包含数组中的所有值

关于javascript - 如何使用 php 将动态文本框值插入到同一 ID 上的数据库表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29032797/

相关文章:

javascript - 没有从对象中获取数组

javascript - PHP 无法使用 AJAX POST 接收数据

php - 我的数据库的高级组织,需要提示

php - Doctrine ORM : use interface as relation for different entities?

php - SQL查询查找表名

php - 调用成员函数 ERROR PHP

javascript - 根据复选框检查删除文本框值

javascript - DOM 查找 ID 返回未定义

javascript - Node.js 从代码记录到外部网站。 (渗透,欢呼)

javascript - Protractor E2E 和 Angular