首页 \ 问答 \ 使用PHP在mySQL中将HTML Form数组插入多行(Insert HTML Form array into multiple rows in mySQL with PHP)

使用PHP在mySQL中将HTML Form数组插入多行(Insert HTML Form array into multiple rows in mySQL with PHP)

在编码时我是一个相对新手,但现在已经在一个侧面项目上工作了一段时间。 我的目的是使用HTML表单创建一个报表工具,通过PHP将信息输入MySQL。 对于单场输入,该过程没有问题。 我遇到问题的地方是当我尝试获取一个Form输入表(带有3个字段)并通过在html表单中添加另一行字段来创建数组时(如果有必要),然后将数组插入到多个MySQL中行。 我知道这个主题已经在StackOverflow中出现了几次,我已经阅读了大多数(如果不是全部)这些主题,但由于某些原因,所提供的答案对我不起作用。

这是我正在使用的代码示例。 这是表格部分:

<form name="weekly" action="process_form.php" method="post">

...

售后 - 票证和错误数据

  <div class="form-group">

  <table id="tac" class="form-group" border="1">

    <tbody>

      <tr>

        <p>

          <td class="ticket">

            <label>Customer</label><input type="text" name="customerTac[]" ng-model="customerTac[]" id="customerTac[]" />

          </td>

          <td class="ticket">

          <label>Ticket ID</label> <input type="text" name="tacTicket[]" ng-model="tacTicket[]" id="tacTicket[]">

        </td>

        <td class="comment">

          <label for="tacComments">Ticket Comments</label>

          <input type="text" name="tacComments[]" ng-model="tacComments[]" id="tacComments[]" cols="100">

        </td>

      </p>

      </tr>

    </tbody>

  </table>

  <p><input type="button" value="Add MOS Ticket" onclick="addRow('tac')" />

  </p>

将此信息插入MySQL的process_form.php的PHP文件如下所示:

$sql .= "INSERT INTO tac (date, userid, ticket, customer, comments) ";



foreach (array('tacTicket', 'customerTac', 'tacComments') as $tac) {

    foreach ($_POST[$tac] as $tacTix => $rowTac) {

    $_POST[$tac][$tacTix] = mysqli_real_escape_string($mysqli, $rowTac);



    }

}



    $tacTicket = ($_POST['tacTicket']);

    $customerTac = ($_POST['customerTac']);

    $tacComments = ($_POST['tacComments']);



    $itemsTac = array();

    $sizeTac = count($tacTicket);



   for($iTac = 0 ; $iTac < $sizeTac ; $iTac++) {

  // Check for ticket

    if (empty($tacTicket[$iTac]) || empty($customerTac[$iTac]) || empty($tacComments[$iTac])) {

    continue;

}

    $itemsTac[] = array(

        "tacTicket"     => $tacTicket[$iTac], 

        "customerTac"    => $customerTac[$iTac],

        "tacComments"       => $tacComments[$iTac]

    );

}



    if (!empty($itemsTac)) {

        $valuesTac = array();



        foreach($itemsTac as $itemTac){

            $valuesTac[] = "('{$itemTac['tacTicket']}', '{$itemTac['customerTac']}', '{$itemTac['tacComments']}')";

        }



        $valuesTac = implode(", ", $valuesTac);



        $sql .= "VALUES  (now(), '".$username."', ".$valuesTac.");"; 



    }

我遇到的是当我将字段发布到表单中时:

在此处输入图像描述

我得到的MySQL命令如下:

INSERT INTO tac (date, userid, ticket, customer, comments) 
VALUES (now(), 'Test', ('1206982096', 'test1', 'first test'), ('292526022', 'test2', 'second test'), ('3026909620', 'test3', 'third test')); 

我尝试了多种不同的方式,有时我得到第一组值,有时我会得到所有值。 我最近将INSERT INTO部分移到foreach命令之上,这可能不是一个好主意,但在一天结束时,我希望看到以下情况发生:

INSERT INTO tac (date, userid, ticket, customer, comments) 
VALUES (now(), 'Test', '1206982096', 'test1', 'first test'); 
INSERT INTO tac (date, userid, ticket, customer, comments) 
VALUES (now(), 'Test', '292526022', 'test2', 'second test');
INSERT INTO tac (date, userid, ticket, customer, comments) 
VALUES (now(), 'Test', '3026909620', 'test3', 'third test');

如果有人对如何实现这一点有任何想法,我将不胜感激。


I am a relative novice when it comes to coding, but have been working on a side project for a little while now. My intent is to create a reporting tool using HTML forms that input the information into MySQL via PHP. For single field inputs, the process has been no problem. Where I am running into issues is when I attempt to take a Form input table (with 3 fields) and create an array by adding another row of fields in the html form, if they are necessary, and then inserting the array into MySQL in multiple rows. I know this topic has come up a few times in StackOverflow and I have read through most if not all of these topics, but for some reason the answers provided are not working for me.

Here is an example of the code that I am using. This is the form portion:

<form name="weekly" action="process_form.php" method="post">

...

Post Sales - Ticket and Bug Data

  <div class="form-group">

  <table id="tac" class="form-group" border="1">

    <tbody>

      <tr>

        <p>

          <td class="ticket">

            <label>Customer</label><input type="text" name="customerTac[]" ng-model="customerTac[]" id="customerTac[]" />

          </td>

          <td class="ticket">

          <label>Ticket ID</label> <input type="text" name="tacTicket[]" ng-model="tacTicket[]" id="tacTicket[]">

        </td>

        <td class="comment">

          <label for="tacComments">Ticket Comments</label>

          <input type="text" name="tacComments[]" ng-model="tacComments[]" id="tacComments[]" cols="100">

        </td>

      </p>

      </tr>

    </tbody>

  </table>

  <p><input type="button" value="Add MOS Ticket" onclick="addRow('tac')" />

  </p>

The PHP file of process_form.php to insert this info into MySQL looks like this:

$sql .= "INSERT INTO tac (date, userid, ticket, customer, comments) ";



foreach (array('tacTicket', 'customerTac', 'tacComments') as $tac) {

    foreach ($_POST[$tac] as $tacTix => $rowTac) {

    $_POST[$tac][$tacTix] = mysqli_real_escape_string($mysqli, $rowTac);



    }

}



    $tacTicket = ($_POST['tacTicket']);

    $customerTac = ($_POST['customerTac']);

    $tacComments = ($_POST['tacComments']);



    $itemsTac = array();

    $sizeTac = count($tacTicket);



   for($iTac = 0 ; $iTac < $sizeTac ; $iTac++) {

  // Check for ticket

    if (empty($tacTicket[$iTac]) || empty($customerTac[$iTac]) || empty($tacComments[$iTac])) {

    continue;

}

    $itemsTac[] = array(

        "tacTicket"     => $tacTicket[$iTac], 

        "customerTac"    => $customerTac[$iTac],

        "tacComments"       => $tacComments[$iTac]

    );

}



    if (!empty($itemsTac)) {

        $valuesTac = array();



        foreach($itemsTac as $itemTac){

            $valuesTac[] = "('{$itemTac['tacTicket']}', '{$itemTac['customerTac']}', '{$itemTac['tacComments']}')";

        }



        $valuesTac = implode(", ", $valuesTac);



        $sql .= "VALUES  (now(), '".$username."', ".$valuesTac.");"; 



    }

What I am running into is when I post the fields into the form:

enter image description here

The MySQL command I get from this is as follows:

INSERT INTO tac (date, userid, ticket, customer, comments) 
VALUES (now(), 'Test', ('1206982096', 'test1', 'first test'), ('292526022', 'test2', 'second test'), ('3026909620', 'test3', 'third test')); 

I have tried multiple different ways and some times I get the first set of values and other times I will get all values. I recently moved the INSERT INTO portion up above the foreach commands which probably is not a good idea, but at the end of the day, I would like to see the following happen:

INSERT INTO tac (date, userid, ticket, customer, comments) 
VALUES (now(), 'Test', '1206982096', 'test1', 'first test'); 
INSERT INTO tac (date, userid, ticket, customer, comments) 
VALUES (now(), 'Test', '292526022', 'test2', 'second test');
INSERT INTO tac (date, userid, ticket, customer, comments) 
VALUES (now(), 'Test', '3026909620', 'test3', 'third test');

If anyone has any thoughts on how I can accomplish this, I would greatly appreciate it.


原文:https://stackoverflow.com/questions/35633637
更新时间:2022-01-12 10:01

最满意答案

对模拟/深层/ CSS选择器(阴影穿孔后代组合器>>>)的支持已被弃用,以与浏览器实现和Chrome的删除意图相匹配。 :: ng-deep已被添加,为当前使用此功能的开发人员提供临时解决方法。

从这里: http : //angularjs.blogspot.com/2017/07/angular-43-now-available.html


I've done some digging and it is possible to overwrite the ng-bootstrap styles if you turn off the view encapsulation for a component, like so:

@Component({
  selector: 'app-example',
  templateUrl: './example.component.html',
  styleUrls: ['./example.component.scss'],
  encapsulation: ViewEncapsulation.None
})
export class ExampleComponent {

    constructor(public activeModal: NgbActiveModal) { }

}

For example, here I am customizing the styles for a modal dialog, because I assumed that my example component should be shown as a modal dialog and I want its width to be at maximum 70% of the view port in small screen sizes:

.modal-dialog {
  @include media-breakpoint-down(xs) {
    max-width: 70vw;
    margin: 10px auto;
  }
}

Please note that turning off the view encapsulation means that the styles for this particular component will be available throughout the application, therefore other components will have access to them and will eventually use them if you are not careful.

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)