Create data loop received by post and insert into mysql

Asked

Viewed 118 times

0

good morning. I’m trying to make a shopping ordering system. I would like to know how to receive $_POST values and send them to a bank. The problem is that this $_POST information is dynamic. I would like to create a special array and insert this information into a mysql table.

Example: inserir a descrição da imagem aqui

Add the include items button:

$('#addmore').click(function(){

                // Get last id 
                var lastname_id = $('.tr_input input[type=text]:nth-child(1)').last().attr('id');
                var split_id = lastname_id.split('_');

                // New index
                var index = Number(split_id[1]) + 1;

                // Create row with input elements
                var html = "<tr class='tr_input'>\n\
                            <td><center><input type='text' class='username' id='username_"+index+"' name='username_"+index+"' placeholder='Informe o produto'></center></td>\n\
                            <td><center><input type='text' class='id_' id='id_"+index+"' name='id_"+index+"' readonly></center></td>\n\
                            <td><center><input type='text' class='name' id='name_"+index+"' name='name_"+index+"' ></center></td>\n\
                            <td><center><input type='text' class='salary' id='salary_"+index+"' name='salary_"+index+"' onKeyUp='moeda(this);'  ></center></td>\n\
                            <td><center><button type='button' class='btn btn-warning' onclick='remove(this)'>Excluir</button></td></center></tr>";

                // Append data
                $('tbody').append(html);

            });

TR for data entry:

<tr class='tr_input'>

                    <td><center><input type='text' class='username' id='username_1' name="username_1" placeholder='Informe o produto'></center></td>
                    <td><center><input type='text' class='id' id='id_1' name="id_1" readonly></center></td>
                    <td><center><input type='text' class='name' id='name_1'  name="name_1" required></center></td>
                    <td><center><input type='text' class='salary' id='salary_1' name="salary_1" onKeyUp="moeda(this);" required></center></td>

                <td><center><button type="button" class="btn btn-warning" onclick="remove(this)">Excluir</button></center></td>

            </tr>

form to send via post:

<form method="post" action="cad-novo-pdc.php">

2 answers

0

$_POST is an array that contains a series of key/value pairs with the field name and its value, passed by POST in the request HTTP. So just walk the same with the foreach (as done in a normal array). Here is an example below, where the list of keys/values:

<?php
$query_string = "";
if ($_POST) {
  $kv = array();
  foreach ($_POST as $key => $value) {
    $kv[] = "$key=$value";
  }
  $query_string = join("&", $kv);
}
else {
  $query_string = $_SERVER["QUERY_STRING"];
}
echo $query_string;
?>

Taken from: https://odesenvolvedor.com.br/percorrendo-todos-os-registros-do-post_1208.html

0

Oh my friend, all right ? Man to do this is quite simple. You only need to use an element of your HTML as a " size " reference for PHP to go through all the data. In second step, in your HTML you need to transform your elements in a form of "Array", for that just put [] in front of each nameof these. It would look something like this:

Your HTML:

<tr class='tr_input'>

                    <td><center><input type='text' class='username' id='username_1' name="username_1[]" placeholder='Informe o produto'></center></td>
                    <td><center><input type='text' class='id' id='id_1' name="id_1[]" readonly></center></td>
                    <td><center><input type='text' class='name' id='name_1'  name="name_1[]" required></center></td>
                    <td><center><input type='text' class='salary' id='salary_1' name="salary_1[]" onKeyUp="moeda(this);" required></center></td>

                <td><center><button type="button" class="btn btn-warning" onclick="remove(this)">Excluir</button></center></td>

            </tr>

PHP for data insertion:

$id_insert = $connect->insert_id;
$qtd_itens = sizeof($_POST['username_1']);

for($i = 0 ; $i < $qtd_itens; $i++) {

        $produto = $_POST['username_1'][$i];
        $codigo = $_POST['id_1'][$i];
        $qtd = $_POST['name_1'][$i];
        $preco = $_POST['salary_1'][$i];

        $query = "INSERT INTO table_name VALUES ('{$id_insert}', '{$produto}', '{$codigo}', '{$qtd}', '{$preco}';)";

        if(!$connect->query($query) === true)
            die("Erro na inserção dos dados <br>" . $connect->error);
}

With this code, PHP checks the amount of existing INPUT’s with the name passed and with this it knows the amount of times it will loop taking value from each other input passed on for. The variable [$i] does this job of separating each value of each input for its proper line, and with it, each row integer way is inserted into its DB. I put this $id_insert in the code, but I don’t know if you work with a PK in your BD where it contains the "Request" ID, if you don’t have it, you can delete it.

  • thanks friend, but it did not work, he is only taking the first item I include, the others do not print on screen.

  • The names of the inputs that you showed the print, are the same of the HTML code ?

  • Buddy, I figured out why he’s only getting the first line. In your JS, you’re doing the adding of the products the wrong way. You are also changing the ID value of the items. You should make this addition using as parameter the name Array-like as I mentioned to you. Since you’re changing the element ID, it’s only picking up the first line anyway. Take the part of changing the Ids that you’ll see will work.

  • I forgot to say that applies also to the name, in the same you do not need to put a different index in JS. It would be better if you do a clone() from your table and then give a apend() than to do in the present way. Leave the name as Array instead of adding index’s to it, because it is through the name that the data is sent to the Database. So if you clone(), it works much better, as it will already be cloned as Array.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.