doumizhi0809 2014-03-19 10:20
浏览 55
已采纳

如何使用动态文本字段在我的数据库中插入多个文本框值

I'm building form with dynamic textboxes, if you click at the button "add", it appears a new textbox, i'm using php and javascript.

But i just can add into my database the value from the first textbox.

How can i insert multiple values into my database?

<html>
   <head>
      <title></title>
   </head>
   <body>
      <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
      <script type="text/javascript"><!--
         $(document).ready(function(){

         var counter = 2;
         $("#add").click(function () {
         if(counter==11){
             alert("Too many boxes");
             return false;
         }   
             $("#textBoxes").append("<div id='d"+counter+"' ><label for='t2'> Textbox "+counter+"</label><input     type='textbox' id='t"+counter+"' > </div>
");
             ++counter;
         });

         $("#remove").click(function () {
         if(counter==1){
             alert("No boxes");
             return false;
         }   
             --counter;
             $("#d"+counter).remove();
         });
         });
         // --></script>
      </head>
      <body>


            <form method="POST">
        <div id='textBoxes'>
            <label for="t1"> Textbox 1</label>
            <input name="nome" type='textbox' id='t1' ></div>
         </div>

         <input type='button' value='add' id='add'>
         <input type='button' value='remove' id='remove'>
         <input type='submit' name="adicionar" value='adicionar'>
         </form>
         <?php
            $dbHost = 'localhost';
                       $dbUsername = 'hr';
                       $dbPassword = '';
                       $dbDatabase = 'r_inserir';

                       $db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die ("Unable to connect to Database Server.");
                       mysql_select_db ($dbDatabase, $db) or die ("Could not select database.");

            if(isSet($_POST['adicionar']))
            {

            $nome=mysql_real_escape_string($_POST['nome']);

            $sql=  mysql_query("INSERT INTO registos(nome) values ('".$nome."')");
            }
            ?>
   </body>
</html>
  • 写回答

3条回答 默认 最新

  • dpnfjx755573 2014-03-19 10:25
    关注

    Make the name like this in html:

    <input name="nome[]" type='textbox' id='t1' ></div>
    

    then in you javascript:

    $("#textBoxes").append("<div id='d"+counter+"' ><label for='t2'> Textbox "+counter+"</label><input name='nome[]' type='textbox' id='t"+counter+"' > </div>
    ");
    

    these code return the array data into your php file. then insert it you your database.

    if(isset($_POST['adicionar']))
            {
    
            //$nome=mysql_real_escape_string($_POST['nome']);
            foreach($_POST['nome'] as $nome){
                  mysql_query("INSERT INTO registos(nome) values ('".mysql_real_escape_string($nome)."')");
            }
    
            //$sql=  mysql_query("INSERT INTO registos(nome) values ('".$nome."')");
            }
    

    Important:

    This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.

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

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀