如何用php形式的可变数量的字段更新sql表 [英] how to update sql table with a variable number of fields in the form php

查看:122
本文介绍了如何用php形式的可变数量的字段更新sql表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表单中有可变数量的字段。
文本字段的数量由用户使用jquery中的函数来定义,但是表单(例子)的最终代码是这样的:

 < form id ='form_educ'name ='form_educ'method ='post'action ='form / educ.php'> 
< div id ='educ'>
< input type ='text'name ='date1'id ='date1'/>
< input type ='text'name ='date2'id ='date2'/>
< input type ='text'name ='date3'id ='date3'/>
< input type ='text'name ='date4'id ='date4'/>
....
< / div>
< input type ='submit'name ='form_educ'value ='Refresh'/>
< / form>

用户添加的这些文本字段是创建一个sql INSERT TO (在另一个文件中):

  $ date = clean($ _ GET ['date']); 

INSERT INTO educ(in​​dex_of_form,date,email)VALUES('$ index','','.mysql_real_escape_string($ _ SESSION ['SESS_EMAIL'])。');

$ date 是date1或date2,或date3或date4(示例)。

现在在文件 educ.php 中,我想更新所有文本字段在mysql数据库中。



通常它是一个

  $ example = clean($ _POST ['例']); 

我可以在表格中进行更新并解决。



但在我的情况下,如何获取表单上文本字段的所有值,并设置 $ _ POST var如果字段数是可变的(可以是date1,date2,date3,date4)?

解决方案

是一个未知的变量。除非你正在处理可重复的字段,在这种情况下,你会使用一个数组像 dates [] ,并且你会知道在进程脚本中会发生什么。



有关其他信息,请参阅示例: http://www.web-design-talk.co.uk/58/adding-unlimited-form-fields-with-jquery-mysql/

警告未来的话。当使字段可重复时,还允许用户删除他们可能意外插入的字段。在流程脚本中注意缺少数组键(如果用户在提交之前删除了某些表单域,则0-10中的数字索引可能会丢失一些值)。您可以使用 array_merge 函数重置数组键。缺少密钥是一个问题,如果你有两个数组,你试图添加到数据库作为syncronized。






更新回答评论。



对不起,我不会拒绝你的问题。您不一定非要使用隐藏字段。你需要的是一个数据库结构来匹配你的表单功能:支持一对多的关系。毕竟你插入了多个与一个人相关的日期,或者一些特定的事件类型,或者其他的日期。让我们假设一个用户想要在世界上添加他的三个最喜欢的日期。您的表单的源代码如下所示:

 < input type =textname ='dateLover'id ='dateLover'/ > 

< input type =textname =dates []id =date1/> //你需要一个递增的变量来表示这些id号(或者根本不要放id)
< input type =textname =dates []id =date2/>
< input type =textname =dates []id =date3/>

另外,您可以添加更多字段,例如< input type = textname =extra/> 。在提交的 $ _ POST 数组中,将会有如下变量和数组: $ _ POST ['dateLover'] $ _ POST ['date'] [1] $ _ POST ['date'] [2] $ _ POST ['extra'] 。你会直接从 $ _ POST 数组中取出不可重复的值,但需要一个 foreach (或某些

    你的数据库必须包含两个表(结构简化):

      >
    1. person id dateLover

    2. date id dateLover FK给person.dateLover, date

    在您的流程脚本中,您必须:


    1. 将新的 dateLover 插入 person 并使用 last_insert_id 来获取他的 id

    2. 使用 foreach 将新的 date s插入到表 date (使用 dateLover id 作为FK)

    这一切在前面提供的链接中都有很好的表现。现在,我们很难给出一个完整的示例,而不必考虑实际问题。






    更新2。



    您正在序列化表单,而不是 div 的。所以你的(动态生成的)如下所示:

     < form id =form_educname =form_educmethod = postaction =form / educ.php> 
    < div id =educ>
    < div><! - 这仅用于布局 - >
    < input type =textname =dates []id =date0/>
    < input type =textname =names []id =name0/>
    < / div>
    < div>
    < input type =textname =dates []id =date1/>
    < input type =textname =names []id =name1/>
    < / div>
    < div>
    < input type =textname =dates []id =date2/>
    < input type =textname =names []id =name2/>
    < / div>
    < / div>
    < input type =submitname =form_educvalue =Refresh/>

    在你的进程文件中, code> $ _POST 数组并将它们插入到数据库中可能是这样的(当然, pre> //查询的动态部分
    $ qEnd ='';
    $ i = -1;

    //这是查询的静态部分
    $ qBeginning =INSERT INTO`date`(`id`,`date`,`name`)VALUES;

    foreach($ _POST ['dates'] as $ key => $ date){
    $ i ++;
    $ qValues [$ i] =(null,'{$ date}','{$ _POST [names] [$ i]}'); //不要这样做,总是检查值...

    //将值集合逐个连接到$ qEnd
    $ qEnd。= $ qValues。 ,;
    }

    //结合查询部分并从结尾
    $ q = $ qBeginning中删除多余的,。 rtrim($ qEnd,',');

    //现在可以执行(单个)查询($ q),仅仅为了它的乐趣而回显它

    id 应该是自动增量字段,或者这种东西不能即时运行。



    同样,这一切都应该在jQuery链接示例中明确,所以请仔细阅读。


    I have a variable number of fields in a form. The number of text fields are defined by the user with a function in jquery, but the final code of the form (example) is this:

    <form id='form_educ' name='form_educ' method='post' action='form/educ.php'>   
      <div id='educ'>
        <input type='text' name='date1' id='date1'/>
        <input type='text' name='date2' id='date2'/>
        <input type='text' name='date3' id='date3'/>
        <input type='text' name='date4' id='date4'/>
        ....   
      </div> 
      <input type='submit' name='form_educ' value='Refresh'/>
    </form>
    

    These text fields when added by the user is create a sql INSERT TO (in another file):

    $date = clean($_GET['date']);
    
    "INSERT INTO educ (index_of_form, date, email) VALUES('$index', '', '" .mysql_real_escape_string($_SESSION['SESS_EMAIL']). "')";
    

    $date is date1, or date2, or date3 or date4 (example).

    Now in the file educ.php I want to update all text fields in the mysql database.

    Usually it is a

    $example = clean($ _POST ['example']);
    

    I can do an update in the table and is resolved.

    But in my case how can I get all the values ​​of the text field on the form and set the $_POST var if the number of fields is variable (could be date1, date2, date3, date4)?

    解决方案

    I can think of no reason why form field name should be a unknown variable. Unless you're dealing with repeatable fields, in which case you would use an array like dates[], and you'd know what to expect in the process script.

    For additional info see for example: http://www.web-design-talk.co.uk/58/adding-unlimited-form-fields-with-jquery-mysql/

    Word of warning for future. When you make the field repeatable, allow users also to delete the fields they might have accidentally insertet. Watch out in the process script missing array keys (numerical index from 0–10 might be missing some values if the user deleted some form fields before submitting). You can reset the array keys with the array_merge function. Missing keys is an issue if you have two arrays you are trying to add into database as syncronized.


    Updated to answer the comment.

    Sorry, I don't undestand your question. You don't necessarily have to use hidden field. What you need is a database structure to match your forms function: to support one to many relationship. After all you are inserting multiple dates that relate to one person, or some specific event type, or what ever. Lets assume one user wants to add his three favorite dates in the world. Your form's source code looks like:

    <input type="text" name='dateLover' id='dateLover'/>
    
    <input type="text" name="dates[]" id="date1" /> //you need a increasing variable for these id numbers (or dont't put the id at all)
    <input type="text" name="dates[]" id="date2" />
    <input type="text" name="dates[]" id="date3" />
    

    In addition you could have more fields such as <input type="text" name="extra" />. In submitted $_POST array there would be variables and arrays like: $_POST['dateLover'], $_POST['date'][0], $_POST['date'][1], $_POST['date'][2], $_POST['extra']. You'd take the non-repeatable values straight out of the $_POST array but you need a foreach (or some else loop) to handle the dates array.

    Your database has to contain two tables (structure simplified):

    1. person: id, dateLover
    2. date: id, dateLover FK to person.dateLover, date

    In your process script you have to:

    1. insert a new dateLover to person and use last_insert_id to get his id
    2. use a foreach to insert new dates to table date (with a dateLover's id as FK)

    This all is pretty well demonstrated in the link I supplied earlier. For now, it's hard to give an complete example without undestanding the actual problem.


    Update 2.

    You are serializing the form, not the div's. So your (dynamically generated) could look like this:

    <form id="form_educ" name="form_educ" method="post" action="form/educ.php">
    <div id="educ">   
        <div><!--This is for layout only-->
            <input type="text" name="dates[]" id="date0" />
            <input type="text" name="names[]" id="name0" />
        </div>
        <div>
            <input type="text" name="dates[]" id="date1" />
            <input type="text" name="names[]" id="name1" />
        </div>
        <div>
            <input type="text" name="dates[]" id="date2" />
            <input type="text" name="names[]" id="name2" />
        </div>
      </div> 
      <input type="submit" name="form_educ" value="Refresh" />
    </form>​
    

    And in your process file you take these arrays from $_POST array and insert them into database maybe like this (with properly escaped and checked values of course...):

    //dynamic part of the query
    $qEnd = '';
    $i = -1;
    
    //this is static part of the query
    $qBeginning = "INSERT INTO `date` (`id`, `date`, `name`) VALUES ";
    
    foreach ($_POST['dates'] as $key => $date){
      $i++;
      $qValues[$i] = "(null, '{$date}', '{$_POST[names][$i]}')"; //never do this, always check values...
    
      //value sets are concatenated one after another to the $qEnd
      $qEnd .= $qValues . ',';
    }
    
    //combine the query parts and remove extra "," from the end
    $q = $qBeginning . rtrim($qEnd, ',');
    
    //now the (single) query ($q) is ready to be executed, echo it just for the fun of it
    

    id should be auto increment field, or this kind of stuff doesn't work on the fly.

    Again, this all should be clear in the jQuery link example so please read it carefully.

    这篇关于如何用php形式的可变数量的字段更新sql表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆