简体   繁体   中英

Insert HTML Form array into multiple rows in mySQL with PHP

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:

在此处输入图片说明

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.

As far as I know INSERT INTO requires a value for all the columns you specify in the statement. As you specify date, userid, ticket, customer and comments you will need a value for all of them in every "VALUES-bracket".

So a valid INSERT INTO Statement should be:

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

How to create this kind of syntax with php is now the challenge you have to solve ;)

Seems I misunderstood your question.

If your question was about how to create the valid sql syntax then my answer is the following: Try to add the date and userId values in the for each loop in which you create your $valuesTac array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM