一次在数据库中添加多行数据


Add Multiple row of data in databse at once

我有几个数据字段,如产品、数量和条形码。系统只显示 1 行包含 3 字段的数据插入表单.当我完成插入第一行时,文本框的第二行将自动生成,我可以通过 Microsoft Access 来做,我可以为 php 这样做吗?

<?php $i=0; ?>
    <form method="post" action="">
        <input type="text" name="<?php echo $i; ?>" />
    </form>

<?php 
    if(isset($_POST[$i])){
    $i++;
?>
    <form method="post" action="">
        <input type="text" name="<?php echo $i; ?>" />
    </form>
<?php }?>

它适用于第一个和第二个文本框,但是如何继续相应地创建更多文本框?

假设您的文件名是 1.php您将表单提交给 1a.php

1.php

<html>
    <head>
        <script>
            a=0;
            function generate()
            {
                if(document.getElementById(a).value!="")
                {
                    var new_input=document.getElementById(a).cloneNode(true);
                    //document.getElementById(a).type="hidden";//uncomment this statement to hide the previous input box.
                    new_input.id=++a;
                    new_input.title='product_no:'+(a+1);
                    new_input.value="";
                    document.getElementById('input_section').appendChild(new_input);
                }
                else
                {
                    alert("Give some value first.");
                    document.getElementById(a).focus();
                }
            }
        </script>
    </head>
    <body>
        <form method="post" action="1a.php">
            <span id="input_section">
                <input type="text" onFocus="this.name='prod[]'; this.id=a; this.title='product_no:'+(a+1);"/>
            </span>
            <input type="button" value="Insert&Generate" onClick="try{generate()} catch(e){alert(e)}">
            <input type="submit" value="INSERT ALL">
        </form>
    </body>
</html>

1一.php

<html>
    <?php
        if(count($_POST)==0)
        {
    ?>
            <script>
                alert('No record to insert.')
                location.href="1.php";
            </script>
    <?php   
        }
        $con=mysqli_connect(...);// connect to database
        foreach($_POST['prod'] as $prod_info)
        {
            $prod_info_arr=explode(",",$prod_info);
            $sql="insert into tab1(product, amount, barcode) values('".$prod_info_arr[0]."', ".$prod_info_arr[1].", '".$prod_info_arr[2]."')";
            mysqli_query($sql);
        }
    ?>
</html>

这就是你想要的。检查一下。

编辑:刚刚在语句document.getElementById(a).type="hidden";上添加了注释,以使填充的文本框不会消失。

如果要具有动态输入字段,以便仅在填充第一个输入框时显示第二个和第三个输入框,请使用 AJAX 代码。

这一定是你想要的。由于您没有清楚地提到在什么条件下应该显示第二个输入字段,我假设如果在第一个输入字段中插入"try",则会显示第二个输入字段。

 <?php 
 echo "<script>
    function showUser(str)
    {
    if (str=='')
      {
      document.getElementById('txtHint').innerHTML='';
      return;
      } 
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');
      }
    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById('txtHint').innerHTML=xmlhttp.responseText;
        }
      }
    xmlhttp.open('GET','get.php?q='+str,true);
    xmlhttp.send();
    }
  </script>
  </head>
  <body>
   <form  method='post' action=''>
   First input<input type='text' name='name'  onclick='showUser(this.value)' /></br>
   <div id='txtHint'></div>
   </form>
   </body>
   </html>"; 
    ?>

并在get.php文件中提交以下代码;

  <?php
  $q = $_GET['q'];
  if($q == 'try'){
  echo "Second input<input type='text' />";
  }
  ?>

希望这会有所帮助。并根据您希望显示第二个输入字段的条件更改 get.php 代码。此代码有效。在第一个输入字段中再次输入 try 后,在文本字段中单击。然后将显示第二个输入字段。