duanhongqiong9460 2016-11-03 15:58
浏览 25

将多个HTML表数据添加到MySQL DB中

Good day I have on my form 3 html tables that get populated by users. I am trying to save all the data into MySQL database, but only the data on the third table is getting saved while the other tables data are coming out as null.

Please find below what I had tried so far:

<?php
error_reporting(~E_NOTICE); // avoid notice
require_once 'dbconfig.php'; //database connection file
if (isset($_POST['btnsave'])) {
$companyName = $_POST['companyName'];
$address = $_POST['address'];

foreach ($_POST['network'] as $rec => $value){
    foreach ((array) $_POST['network'] as $rec => $value) {
        $network = $_POST['network'][$rec];
        $size = $_POST['size'][$rec];
        $assignedBy = $_POST['assignedby'][$rec];
    }
} 
    foreach ($_POST['network4'] as $rec4 => $value){
    foreach ((array) $_POST['network4'] as $rec4 => $value) {
        $network4 = $_POST['network4'][$rec4];
        $subnetsize = $_POST['subnetsize'][$rec4];
        $max4 = $_POST['max4'][$rec4];
    }
    }    

 foreach ($_POST['network6'] as $key => $value){
    foreach ((array) $_POST['network6'] as $key => $value) {
        $network6 = $_POST['network6'][$key];
        $prefix = $_POST['prefix'][$key];
        $max6 = $_POST['max6'][$key];
    }
 }    

  $stmt = $DB_con->prepare('INSERT INTO         mytable(companyName,address,network,size,assignedby,network4,subnetsize,max4,net     work6,prefix,max6) '
            . 'VALUES(:compName,     :uaddr,:tblntwk,:tblassignedBy,:tblnetwk4,:tblsubsz4,:tblmx4,:tblnetwk6,:tblprfx      6,:tblmx6)');

    $stmt->bindParam(':compName', $companyName);
    $stmt->bindParam(':uaddr', $address);

    //First Table Entries
    $stmt->bindParam(':tblntwk', $network);
    $stmt->bindParam(':tblsze', $size);
    $stmt->bindParam(':tblassignedBy', $assignedBy);

    //Second Table Entries
    $stmt->bindParam(':tblnetwk4', $network4);
    $stmt->bindParam(':tblsubsz4', $subnetsize);
    $stmt->bindParam(':tblmx4', $max4);

    //Third Table Entries: Only this data gets saved
    $stmt->bindParam(':tblnetwk6', $network6);
    $stmt->bindParam(':tblprfx6', $prefix);
    $stmt->bindParam(':tblmx6', $max6);

    if ($stmt->execute()) {
        $successMSG = "new record succesfully inserted ...";
        //header("refresh:60;index.php"); // redirects image view page
    } else {
        $errMSG = "error while inserting....";
    }
}
?>


<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
       <title>Table Form</title>
        <SCRIPT language="javascript">
            function addRow(tableID1) {
                var table = document.getElementById(tableID1);
                var rowCount = table.rows.length;
                var row = table.insertRow(rowCount);
                var cell1 = row.insertCell(0);
                var element1 = document.createElement("input");
                element1.type = "checkbox";
                element1.name = "chkbox[]";
                cell1.appendChild(element1);
                var cell2 = row.insertCell(1);
            cell2.innerHTML = "<input type='text' name='network[]'>";
            var cell3 = row.insertCell(2);
            cell3.innerHTML = "<input type='text'  name='size[]' />";
            var cell4 = row.insertCell(3);
            cell4.innerHTML = "<input type='text'  name='assignedby[]' />";
        }

        function deleteRow(tableID1) {
            try {
                var table = document.getElementById(tableID1);
                var rowCount = table.rows.length;

                for (var i = 0; i < rowCount; i++) {
                    var row = table.rows[i];
                    var chkbox = row.cells[0].childNodes[0];
                    if (null != chkbox && true == chkbox.checked) {
                        table.deleteRow(i);
                        rowCount--;
                        i--;
                    }
                }
            } catch (e) {
                alert(e);
            }
        }

        function addRow(tableID2) {
            var table = document.getElementById(tableID2);
            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);
            var cell1 = row.insertCell(0);
            var element1 = document.createElement("input");
            element1.type = "checkbox";
            element1.name = "chkbox[]";
            cell1.appendChild(element1);
            var cell2 = row.insertCell(1);
            cell2.innerHTML = "<input type='text' name='network4[]'>";
            var cell3 = row.insertCell(2);
            cell3.innerHTML = "<input type='text'  name='subnetsize[]' />";
            var cell4 = row.insertCell(3);
            cell4.innerHTML = "<input type='text'  name='max4[]' />";
        }

        function deleteRow(tableID2) {
            try {
                var table = document.getElementById(tableID2);
                var rowCount = table.rows.length;

                for (var i = 0; i < rowCount; i++) {
                    var row = table.rows[i];
                    var chkbox = row.cells[0].childNodes[0];
                    if (null != chkbox && true == chkbox.checked) {
                        table.deleteRow(i);
                        rowCount--;
                        i--;
                    }
                }
            } catch (e) {
                alert(e);
            }
        }

        function addRow(tableID3) {
            var table = document.getElementById(tableID3);
            var rowCount = table.rows.length;
            var row = table.insertRow(rowCount);
            var cell1 = row.insertCell(0);
            var element1 = document.createElement("input");
            element1.type = "checkbox";
            element1.name = "chkbox[]";
            cell1.appendChild(element1);
            var cell2 = row.insertCell(1);
            cell2.innerHTML = "<input type='text' name='network6[]'>";
            var cell3 = row.insertCell(2);
            cell3.innerHTML = "<input type='text'  name='prefix[]' />";
            var cell4 = row.insertCell(3);
            cell4.innerHTML = "<input type='text'  name='max6[]' />";
        }

        function deleteRow(tableID3) {
            try {
                var table = document.getElementById(tableID3);
                var rowCount = table.rows.length;

                for (var i = 0; i < rowCount; i++) {
                    var row = table.rows[i];
                    var chkbox = row.cells[0].childNodes[0];
                    if (null != chkbox && true == chkbox.checked) {
                        table.deleteRow(i);
                        rowCount--;
                        i--;
                    }
                }
            } catch (e) {
                alert(e);
            }
        }

    </SCRIPT>

    <link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">

        <!-- Optional theme -->
        <link rel="stylesheet" href="bootstrap/css/bootstrap-theme.min.css">

            </head>
            <body>
                <?php
                if (isset($errMSG)) {
                    ?>
                    <div class="alert alert-danger">
                        <span class="glyphicon glyphicon-info-sign"></span> <strong><?php echo $errMSG; ?></strong>
                    </div>
                    <?php
                } else if (isset($successMSG)) {
                    ?>
                    <div class="alert alert-success">
                        <strong><span class="glyphicon glyphicon-info-sign"></span> <?php echo $successMSG; ?></strong>
                    </div>
                    <?php
                }
                ?>  
                <form method="post" enctype="multipart/form-data" class="form-horizontal"> 

                    <h>Customer Information</h></p>

                    <table class="table table-bordered table-responsive">
                        <tr>
                            <td><label class="control-label">Company/Client Name:.</label></td>
                            <td><input class="form-control" type="text" name="companyName" placeholder="Company/Client Name" value="<?php echo $companyName; ?>" /></td>
                        </tr>
                        <tr>
                            <td><label class="control-label">Address.</label></td>
                            <td><input class="form-control" type="text" name="address" placeholder="Address" value="<?php echo $address; ?>" /></td>
                        </tr>

                    </table>

                        <INPUT type="button" value="Add Row" onClick="addRow('ipv4addressspacetable')" />
                        <INPUT type="button" value="Delete Row" onClick="deleteRow('ipv4addressspacetable')" />
                        <form action="" method="post" name="f">  
                            <TABLE id="firsttable" width="425" border="1">
                                <thead>
                                    <tr>
                                        <th width="98"></th>
                                        <th width="94">Network</th>
                                        <th width="121">Size</th>
                                        <th width="84">Assigned By</th>
                                        <th width="121">Assigned Date</th>
                                        <th width="84">Percentage in Use</th>

                                    </tr>
                                </thead>
                                <tbody id="ipv4addressspacetable">
                                </tbody>
                            </TABLE>


                    <INPUT type="button" value="Add Row" onClick="addRow('ipv4addressspaceusage')" />
                    <INPUT type="button" value="Delete Row" onClick="deleteRow('ipv4addressspaceusage')" />
                    <form action="" method="post" name="f">  
                        <TABLE width="425" border="1">
                            <thead>
                                <tr>
                                    <th width="98"></th>
                                    <th width="94">Network</th>
                                    <th width="121">Subnet Size</th>
                                    <th width="84">(Hosts) Max</th>
                                </tr>
                            </thead>
                            <tbody id="ipv4addressspaceusage">
                            </tbody>
                        </TABLE>

                        <INPUT type="button" value="Add Row" onClick="addRow('ipv6addressspaceusage')" />
                        <INPUT type="button" value="Delete Row" onClick="deleteRow('ipv6addressspaceusage')" />
                        <form action="" method="post" name="f">  
                            <TABLE width="425" border="1">
                                <thead>
                                    <tr>
                                        <th width="98"></th>
                                        <th width="94">Network</th>
                                        <th width="121">Prefix</th>
                                        <th width="84">Max</th>
                                    </tr>
                                </thead>
                                <tbody id="ipv6addressspaceusage">
                                </tbody>
                            </TABLE>
                        <button type="submit" name="btnsave" class="btn btn-default"><span class="glyphicon glyphicon-save"></span> &nbsp; save</button>

                        </form>
                        </div>
                        <!-- Latest compiled and minified JavaScript -->
                        <script src="bootstrap/js/bootstrap.min.js"></script>
                        </body>
                        </html>

Thank you in advance.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 Revit2020下载问题
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
    • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数
    • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
    • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
    • ¥15 如何在炒股软件中,爬到我想看的日k线