douan2907 2013-12-10 09:08
浏览 47
已采纳

从可变数量的表单中将数据插入到mysql中

From input page I have changeable number of forms sending me data. I made so that each new form have next number.

name2
date3
name4
date5
name6
date7
etc...

The problem begins when I tried to capture them in in my Db in table with two columns "name" and "date". This is the code Im using:

$i = 1;
while(isset($_POST["name".$i]))
{
    $cantidad = $_POST["name".$i];
    $value = $_POST["date".($i+1)];
    $query = "INSERT INTO `tickets` SET `name` = $cantidad, `date` = $value";
    mysql_query($query);
    $i += 2;
}

Basically, since the forms come in pares (name and date) I need them to insert in right order in "name" and "date" column. The thing is for some reason the code doesn't inert.

What do I do wrong? Thanks in advance.

  • 写回答

2条回答 默认 最新

  • dongtangze6393 2013-12-10 10:26
    关注

    lznogud,

    I have done a few changes to your code and accordingly you need to do some in the php file i.e testProces.php

    http://jsfiddle.net/mNkub/

    $('#addScnt').live('click', function() {
                $('<label for="ticketCantidad"><input type="text" id="ticketCantidad" size="20" name="ticketCantidad[' + i +']" value="" placeholder="Valor de Tickets" /></label>').appendTo(scntDiv);
                //i++;
                $('<label for="ticketValue"><input type="text" id="ticketValue" size="20" name="ticketValue[' + i +']" value="" placeholder="Cantidad de Tickets" /></label><p></p>').appendTo(scntDiv);
                i++;
                return false;
        });
    

    Now the html structure you get will be

    <input type="text" id="ticketCantidad" size="20" name="ticketCantidad[2]" value="" placeholder="Valor de Tickets">
    <input type="text" id="ticketValue" size="20" name="ticketValue[2]" value="" placeholder="Cantidad de Tickets">
    

    then next one will be :

    <input type="text" id="ticketCantidad" size="20" name="ticketCantidad[3]" value="" placeholder="Valor de Tickets">
    <input type="text" id="ticketValue" size="20" name="ticketValue[3]" value="" placeholder="Cantidad de Tickets">
    

    and so on

    now in the php file you will get two arrays with both fields data in same key in POST. Then you can do the coding as follows :

    if(isset($_POST["ticketCantidad"]) && count($_POST['ticketCantidad'])>0) {
    
        foreach($_POST["ticketCantidad"] as $key => $value) {
            $cantidad = $value;
            $value = $_POST["ticketValue"][$key];
            $query = "INSERT INTO `tickets` SET `name` = $cantidad, `date` = $value";
            mysql_query($query);
        }     
    
    }
    

    Hope this will clears the picture.

    PS : you may need to do some tweaking in code above as i have not tested it, just writing it in one go.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)