PHP多条记录插入 [英] PHP multiple records insert

查看:67
本文介绍了PHP多条记录插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图改写我的问题.

I am attempting to reword my issue.

我有一个数据表,可以返回数千条记录,每条记录都有多列.第一列中有一个复选框,一旦用户选中它,然后单击一个按钮,与该行关联的CONTAINER_NUMBER将发送到模式窗口以供表单使用.

I have a datatable that can return thousands of records, each with multiple columns. There is a checkbox in the first column that, once the user checks it, they then click a button, and the CONTAINER_NUMBER that is associated with the row is sent to a modal window to be used in a form.

以下是该复选框的代码:

Here is the code for the checkbox:

 echo "<tr><td><input type=\"checkbox\" id=\"{$Row[CONTAINER_NUMBER]}\" name=\"checkMr[]\" /></td>";

这是检索CONTAINER_NUMBER并将其发送到模式窗口的javascript:

This is the javascript that retrieves the CONTAINER_NUMBER and sends it to the modal window:

   <script type="text/javascript">
   $(function()
   {
    $('a').click(function()
    {
        var selectedID = [];
        $(':checkbox[name="checkMr[]"]:checked').each(function()
        {               
            selectedID.push($(this).attr('id'))         
        });                                             
        $(".modal-body .containerNumber").val( selectedID );
       });
    });
    </script>

这是模式窗口中显示CONTAINER_NUMBER的部分:

This is the section of the modal window that displays the CONTAINER_NUMBER:

 <div class="modal-body">
   <form action="" method="POST" id="serviceModalForm" name="serviceModalForm">
   <input type="text" name="containerNumber" id="containerNumber" class="containerNumber">

这是PHP的一部分,它采用id ="containerNumber"并将其转换为PHP变量.之后,有一条INSERT语句将containerNumber插入数据库表中:

Here is the section of PHP that takes the id="containerNumber" and converts it to a PHP variable. After that, there is an INSERT statement that inserts the containerNumber into a database table:

 <?php
   $container = $_POST['containerNumber'];

   if(isset($_POST['submit'])){
     $container = mysql_real_escapse_string(stripslashes($container));

     $sql = "INSERT INTO myTable (container_num) VALUES ('$container')";

     if(mysql_query($sql)){
       echo "Insert complete";
     }
     else {
       echo "Insert was not completed";
     }
 ?>

此代码很好.效果很好.当用户选中一个"复选框时,它会执行应做的工作.当用户选中多个复选框时,它不起作用.

This code is fine. It works good. It does what it's supposed to do...for when the user checks ONE checkbox. It DOES NOT work when the user checks multiple checkboxes.

基本上,根据我的研究,我需要将记录与变量$ container分开,因为该变量中可以有多个容器,这就是为什么当有多个容器时查询不起作用的原因选择的货柜编号.

Basically, from what I've been researching is that I need to separate the records from the variable $container, as there can be multiple containers in that variable, which is why the query does not work when there are more than one container numbers selected.

我需要能够分隔容器编号并将它们存储在数组或其他内容中.该查询将分别读取每条记录,并为每条记录生成多个INSERT语句.

I need to be able to separate the container numbers and store them in an array or something. The query will read each record separately and generate multiple INSERT statements for each record.

我已经尝试过几次创建一个数组并获得sql语句来识别它,但是没有成功.我不确定是否将阵列放置在正确的位置.我不确定在将容器发送到模式窗口之前是否必须在javascript中完成此操作.

I've tried several times to create an array and get the sql statement to recognize it, but have been unsuccessful. I'm not sure if I'm placing the array in the right place. I'm not sure if this has to be done in the javascript before the container gets sent to the modal window.

我知道我需要利用一个FOREACH循环来遍历数组,但是就像我说的那样,我不确定在我的代码中该数组需要去哪里.

I know I need to utilize a FOREACH loop to go through the array, but like I said, I'm not sure where the array needs to go in my code.

请帮助.我知道我需要学习PDO或MYSQLI.我一定会在下一个应用程序中使用PDO或MYSQLI.在此之前,请帮助我解决这个问题.

Please help. I know I need to learn PDO or MYSQLI. I will be sure to utilize PDO or MYSQLI on my next application. Until then, please help me with this issue.

谢谢,非常抱歉.

推荐答案

您的containerNumber将作为js数组中转换后的字符串发布.像id1,id2,id3 [...]

Your containerNumber will be posted as a converted string from a js array. Something like id1, id2, id3[...]

在您的php代码中,将$ container转换回数组( $ containerArray = explode(,",$ container))并动态构造sql以将所有行添加到单个查询,使语句变成类似

In your php code, convert the $container back to an array ($containerArray = explode(",", $container)) and construct the sql dynamically to add all the rows in a single query so that the statment becomes something like

INSERT INTO myTable (container_num) VALUES ('$containerArray[0]'), ('$containerArray[1]')[...]

这篇关于PHP多条记录插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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