dounan9070 2013-08-30 09:46
浏览 11
已采纳

循环仅插入最后的值

i am using the php to get all the values from textbox, inside a loop i am getting the values.

inside a loop i also have the insert statement and the count.

The flow is: i will select the value from drop-down list, when i select the number from the drop down list, the corresponding number of text-boxes will be created.

for ex:- i have selected the number 3 from drop-down list. so it will create 3 text-boxes.

now i will enter data into those 3 text-boxes and click submit. when i click submit only the value of last text-box is getting inserted into database.

i.e. when i select 3 and enter data to 3 text-box it is inserting only 3rd text-box value 3 times, instead of inserting 1st,2nd and 3rd value..

how can i solve it..?

here is code i have used..

php:

<?php
  mysql_connect("localhost", "tiger", "tiger") or die(mysql_error());
  mysql_select_db("theaterdb") or die(mysql_error());
  for ($i=0; $i < $_POST["range"] ; $i++)
  {
    $query = mysql_query("INSERT INTO movie (movie_name,language) VALUES('$_POST[Fname]','$_POST[language]') ") or die(mysql_error());
  }
?>

range is a value what i have selected in dropdown.

javascript code:

for(i = 0; i < param; i += 1) {
       target.innerHTML +='</br>';
       target.innerHTML +='New Movie '+i+'  ';
       target.innerHTML += '<input type="text" name="Fname">';
}

this is generating the textbox dynamically

  • 写回答

5条回答 默认 最新

  • dpd3447 2013-08-30 09:58
    关注

    the problem is that you are creating new input with the same name as previously created ones, php's $_POST[] goes on the inputs name to get arround this you will need to create inputs with unique names something like

    for(i = 0; i < param; i += 1) {
    
           target.innerHTML +='</br>';
           target.innerHTML +='New Movie '+i+'  ';
           target.innerHTML += '<input type="text" name="Fname_' + i + '">';
    }
    
    <?php
    mysql_connect("localhost", "tiger", "tiger") or die(mysql_error());
    mysql_select_db("theaterdb") or die(mysql_error());
    for ($i=0; $i < $_POST["range"] ; $i++)
    {
    $query = mysql_query("INSERT INTO movie (movie_name,language) VALUES('" . $_POST['Fname_' . $i] . "','" . $_POST['language'] . "') ") or die(mysql_error());
    }
    ?>
    

    this will however only fork for adding inputs once or you will create inputs with identicle names again, to get arround this i would reccomend storing the total count in a hidden input called count <input type="hidden" value="0" name="count" /> the basing the names off of that count

    var count = $('input[name=count]');
    
    for(i = 0+count; i < param+count; i += 1) {
    
           target.innerHTML +='</br>';
           target.innerHTML +='New Movie '+i+'  ';
           target.innerHTML += '<input type="text" name="Fname">';
    }
    
    <?php
    mysql_connect("localhost", "tiger", "tiger") or die(mysql_error());
    mysql_select_db("theaterdb") or die(mysql_error());
    for ($i=0; $i < $_POST["range"] ; $i++)
    {
    $query = mysql_query("INSERT INTO movie (movie_name,language) VALUES('" . $_POST['Fname_' . $i] . "','" . $_POST['language'] . "') ") or die(mysql_error());
    }
    ?>
    

    i havnt tested this code so you might need to tinker a bit to make it work but the concept is there

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

报告相同问题?

悬赏问题

  • ¥20 steam下载游戏占用内存
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系