插入数据库

时间:2014-12-01 11:10:41

标签: php mysql cakephp

我有一个小问题。我有这个代码,但这个插入到数据库只是最后一个输入值。我的函数做了一个新的输入,如果你点击+然后再来新的输入,但我想把所有这些输入值都输入到数据库中,但是这个代码只将最后一个输入值插入到数据库中。 示例:我插入53424345然后插入56546546,只有56546546进入数据库。但我想进入数据库53424345,56546546。这可能吗?以及如何做这样的事情?

<table id="mytable">
        <tr id="number0" style="display:none;">
            <td><?php echo $this->Form->button('&nbsp;-&nbsp;',array('type'=>'button','title'=>'Click Here to remove this number')); ?></td>
            <td><?php echo $this->Form->input('lisanumbrid'); ?></td>
        </tr>
        <tr id="trAdd"><td> <?php echo $this->Form->button('+',array('type'=>'button','title'=>'Click Here to add another number','onclick'=>'addNumber()')); ?> </td><td></td><td></td><td></td><td></td></tr>
    </table>



    <?php echo $this->Html->script(array('jquery-2.1.1.min.js'));?>
<script type='text/javascript'>
    var lastRow=0;

    function addNumber() {
        lastRow++;
        $("#mytable tbody>tr#number0").clone(true).attr('id','lisanumbrid'+lastRow).removeAttr('style').insertBefore("#mytable tbody>tr#trAdd");
        $("#lisanumbrid"+lastRow+" button").attr('onclick','removeNumber('+lastRow+')');
        $("#lisanumbrid"+lastRow+" input:first").attr('numbrid','data[Lisanumbrid]['+lastRow+'][lisanumbrid]').attr('id','numbridlisaNumber'+lastRow);
    }

    function removeNumber(x) {
        $("#lisanumbrid"+x).remove();
    }
</script>

谢谢,我接受所有提示和解决方案。

1 个答案:

答案 0 :(得分:1)

考虑到您将数据保存到模型表中,您需要进行2次更改:

1)将输入修改为

<?php echo $this->Form->input('lisanumbrid',array('name'=>'data[Model][0][lisanumbrid]')); ?>

2)将addNumber()修改为

function addNumber() {
        lastRow++;
        $("#mytable tbody>tr#number0").clone(true).attr('id','lisanumbrid'+lastRow).removeAttr('style').insertBefore("#mytable tbody>tr#trAdd");
        $("#lisanumbrid"+lastRow+" button").attr('onclick','removeNumber('+lastRow+')');
        $("#lisanumbrid"+lastRow+" input:first").attr('numbrid','data[Lisanumbrid]['+lastRow+'][lisanumbrid]').attr({'id':'numbridlisaNumber'+lastRow,'name': 'data[Model]['+ lastRow  +'][lisanumbrid]'});
}