如何将html表多行数据插入mysqli数据库

时间:2017-11-09 00:36:05

标签: javascript php mysqli

这是我的javascript表格代码

<script type="text/javascript">
    var row = "";
    $(function() {
        $('select').change(function() {
            $('#myTable').html('');
            row ='<tr>\
            <th>\Room</th>\
            <th>\Floor</th>\
            <th>\Rent</th>\
            </td>\
            </tr>';
            row = $(row);
            $('#myTable').append(row);
            for (var i = 0; i<parseInt($(this).val()); i++) {
              var a = i+1;
              row =  '<tr>\
                    <td>'+a+'</td>\
                    <td>\
                    <select name="floor">\
                     <option value="1">0</option>\
                     <option value="2">1</option>\
                     <option value="3">2</option>\
                     <option value="3">3</option>\
                     </select>\
                    </td>\


                     <td>\
                        <input id="rent" name="rent" class="form-control required" required="required" placeholder="$" min="0" data-bind="value:replyNumber" type="text">       \
                    </td>\
                </tr>';
                row = $(row);
                $('#myTable').append(row);
            }
        });
    });
</script>

这是我的HTML代码

<form action="ad-post.process.php" method="post" enctype="multipart/form-data" class="form-horizontal">
    <fieldset>

        <div class="form-group" runat="server">
            <label class="col-md-3 control-label" for="addetails">No of Rooms <sup>*</sup></label>
            <div class="col-md-8">

                <select name ="rooms">
                    <option disabled="disabled" selected="selected" ></option>
                    <option value="1">1</option>
                    <option value="2">2</option>
                    <option value="3">3</option>
                    <option value="4">4</option>
                </select>
            </div>
        </div>

        <div class="form-group" >

            <label class="col-md-3 control-label" for="addetails"></label>

            <table id="myTable"></table>
            <div class="col-md-8" ></div>
        </div>
    </div>
</div>

        <div class="form-group">
            <label class="col-md-3 control-label"></label>
            <div class="col-md-8"><button id="btn-signup" type="submit" class="btn btn-primary"><i class="icon-hand-right"></i> &nbsp Submit</button>
            </div>
        </div>
    </fieldset>
</form>

这是我的ad-post.process.php代码

require_once './class.ads.php';
$rent = floatval(getFormValues('rent'));
$floor =   getFormValues('floor');
$rooms = getFormValues('rooms');
$ad = new Ads($mysql_connection);
if ($ad->postAd($floor,$rent,$rooms)) {
    header("location:./ad-success.php?" . $ts);
    exit;
}

这是我的class.ads.php

的代码
public function postAd( $rent,$floor, $rooms) {
    try {
        for($i=0; $i < $rooms; $i++) { 
            $sql_insert1 = "INSERT INTO `room` (`Floor`, `rent`) 
                            VALUES "
                          . " ('{$floor}','{$rent}' )";
            $sql_insert2 = mysqli_query($this->_db, $sql_insert1);
        }

        if ($sql_insert2) {
            return true;
        }
    } catch (Exception $ex) {
        return false;
    }
}

我的代码问题是每当我将数据插入到最后一行的数据库数据时多次插入数据库。

1 个答案:

答案 0 :(得分:0)

所以我花了一点时间才抓住它。

广告-post.process.php

当您调用函数if($ ad-&gt; postAd($ floor,$ rent,$ rooms))时,您传递了3个变量。

require_once './class.ads.php';
$rent = floatval(getFormValues('rent'));
$floor =   getFormValues('floor');
$rooms = getFormValues('rooms');
$ad = new Ads($mysql_connection);
if ($ad->postAd($floor,$rent,$rooms)) {
    header("location:./ad-success.php?" . $ts);
    exit;
}

<强> class.ads.php

当您收到函数调用时,您正在接受4个变量,而不是正确的顺序。

function postAd($rent, , $floor, $rooms)
{
    try {
        for ($i = 0; $i < $rooms; $i++) {
            $sql_insert1 = "INSERT INTO `room` (`Floor`, `rent`) VALUES "
                . "       ('{$floor}','{$rent}' )";
            $sql_insert2 = mysqli_query($this->_db, $sql_insert1);
        }

        if ($sql_insert2) {
            return true;
        }
    } catch (Exception $ex) {
        return false;
    }
}

我会将class.ads.php函数更改为:

function postAd($floor,$rent,$rooms)
{
    try {
        for ($i = 0; $i < $rooms; $i++) {
            $sql_insert1 = "INSERT INTO `room` (`Floor`, `rent`) VALUES "
                . "       ('{$floor}','{$rent}' )";
            $sql_insert2 = mysqli_query($this->_db, $sql_insert1);
        }

        if ($sql_insert2) {
            return true;
        }
    } catch (Exception $ex) {
        return false;
    }
}