1
votes

I'm having trouble inserting data. When more than one row is entered into the database, the array splits the words into letters.

In general, I'm trying to choose a couple of names, after which an individual setting appears for each

The first value is added without problems, but if I try to make the rest entered in the same way, they are simply separated into letters

And if I try to change for first value id "add_idtest_1" to another id val, this one completely not work and added spell by letter

Submit Code file.js

    $(document).on("submit", "#addUser", function (e) {
    e.preventDefault();
    var division_st = $("#division_st").val();  
    var schiff_st = $("#schiff_st").val();  
    
    var var_idtest_1 = $("#add_idtest_1").val();  // this one works
    var var_idtest_2 = $("#add_idtest_2").val();
    var var_idtest_3 = $("#add_idtest_3").val();
    var var_idtest_4 = $("#add_idtest_4").val();
    var var_idtest_5 = $("#add_idtest_5").val();
    var var_idtest_6 = $("#add_idtest_6").val();
    var var_idtest_7 = $("#add_idtest_7").val();
    var var_idtest_8 = $("#add_idtest_8").val();
    var var_idtest_9 = $("#add_idtest_9").val();
    var var_idtest_10 = $("#add_idtest_10").val();
    var deck_fz = $("#deck_fz").val();
    var kontroll = $("#kontroll").val();
  

    if ( var_idtest_1 != "" && var_idtest_2 != "" ) {
        $.ajax({
            url: "offconvas3/add_user.php",
            type: "post",
            data: { 
                division_st: division_st,
                schiff_st: schiff_st,
                var_idtest_1: var_idtest_1, // this one works
                var_idtest_2: var_idtest_2,
                var_idtest_3: var_idtest_3, 
                var_idtest_4: var_idtest_4,
                var_idtest_5: var_idtest_5,
                var_idtest_6: var_idtest_6,
                var_idtest_7: var_idtest_7,
                var_idtest_8: var_idtest_8,
                var_idtest_9: var_idtest_9,
                var_idtest_10: var_idtest_10,
                deck_fz: deck_fz,
                kontroll: kontroll
            },
            success: function (data) {
                var json = JSON.parse(data);
                var status = json.status;



                setTimeout(function(){$('#stundenerfassung').DataTable().ajax.reload();}, 250);
                setTimeout(function(){$('#gesamt_statistik_server').DataTable().ajax.reload();}, 250);
                setTimeout(function(){$('.br_tk_statistik').DataTable().ajax.reload();}, 250);


                if (status == "true") {
                    mytable = $("#stundenerfassung").DataTable();
                    mytable.draw();
                    $("#addUserModal").modal("hide");
                } else {
                    alert("failed");
                }
            },
        });
    } else {
        alert("Fill all the required fields");
    }
});

Form Code

<form id="addUser" action="offconvas3/add_user.php" autocomplete="off"> 

  <input type="hidden" class="form-control dsh69" id="division_st" value="<?php echo $pg_div ?>" />

  <input type="hidden" class="form-control dsh69" id="schiff_st" value="<?php echo $pg_sch ?>" />

  <p class="label_pcontent mt-2">Name</p> 
  <div id="show_name"></div>  **<!-- Here comes the one that works -->**

  <div style='background-color:lightgray; margin:10px 0px;' id="selValShow"></div>

  <p class="label_pcontent mt-2">Stunden</p>
  <input placeholder="Stunden:" type="text" class="form-control dsh69 add_idtest_2" value="8" onchange="onFormChange()"/>

  <p class="label_pcontent mt-2">Bereich</p>
  <select class="form-select dsh69 add_idtest_5" onchange="onFormChange()">
     <option selected value="">Leere Zeile</option>
       <?= getBereichForm( 'let_txt_3' ) ?>
  </select>

  <p class="label_pcontent mt-2">Taetigkeit</p>
    <select class="form-select dsh69 add_idtest_6" onchange="onFormChange()">
      <option selected value="">Leere Zeile</option>
        <?= getTaetigkeitForm( 'const_txt_3' )?>
    </select>

  <p class="label_pcontent mt-2">Datum</p>
    <input type="date" placeholder="Datum" class="form-control dsh69" id="add_idtest_4" value="<?php echo date('Y-m-d') ?>" />

  <p class="label_pcontent mt-2 <?php echo ( $pg_div == 'EL' ) ? 'text' : 'hidden' ?>">ABGR</p>
  <input type="<?php echo ( $pg_div == 'EL' ) ? 'text' : 'hidden'; ?>" placeholder="ABGR" class="form-control dsh69" id="add_idtest_7" />

  <p class="label_pcontent mt-2">Notiz</p>
  <textarea placeholder="Notiz:" type="text" class="form-control dsh69" id="add_idtest_8"></textarea>

Here is code for row settings

   const container_name = document.querySelector('#show_name');
    sel_name = document.createElement("select");
    sel_name.setAttribute('multiple', 'multiple');
    sel_name.setAttribute('name', 'add_idtest_1[]'); // this one works
    sel_name.setAttribute('id', 'add_idtest_1');
    sel_name.setAttribute('onchange', 'onFormChange()');
    sel_name.classList.add("form-control");

    container_name.appendChild(sel_name);


    function addOptionName(){
        $("#add_idtest_1").append("<?= getNameForm('var_txt_3') ?>");
    }

    addOptionName()

  function onFormChange() {
    const container = document.querySelector('#selValShow');

    let text = "";
    for( let i = 0; i < selected.length; i++ ){



    inp_name = document.createElement('input');
    inp_name.setAttribute('type', 'text');
    inp_name.setAttribute('value', selected[i]);
    inp_name.setAttribute('placeholder', 'Name');

    container.appendChild(inp_name);

    // stunden input
    inp_std = document.createElement('input');
    inp_std.setAttribute('type', 'text');
    inp_std.setAttribute('name', 'add_idtest_2[]');
    inp_std.setAttribute('id', 'add_idtest_2');
    inp_std.setAttribute('value', stunden);
    inp_std.setAttribute('placeholder', 'Stunden');

    container.appendChild(inp_std);

    // meter input
    inp_mtr = document.createElement('input');
    inp_mtr.setAttribute('type', 'text');
    inp_mtr.setAttribute('name', 'add_idtest_3[]');
    inp_mtr.setAttribute('id', 'add_idtest_3');
    inp_mtr.setAttribute('value', '0');
    inp_mtr.setAttribute('placeholder', 'Meter');

    container.appendChild(inp_mtr);

    // Bereich Input
    inp_ber = document.createElement('input');
    inp_ber.setAttribute('type', 'text');
    inp_ber.setAttribute('name', 'add_idtest_5[]');
    inp_ber.setAttribute('id', 'add_idtest_5');
    inp_ber.setAttribute('multiple', 'multiple');
    inp_ber.setAttribute('value', bereich);

    container.appendChild(inp_ber)
    // Taetigkeit input
    inp_tk = document.createElement('input');
    inp_tk.setAttribute('type', 'text');
    inp_tk.setAttribute('name', 'add_idtest_6[]');
    inp_tk.setAttribute('id', 'add_idtest_6');
    inp_tk.setAttribute('value', taetigkeit);

    container.appendChild(inp_tk)
   }
  }

PHP Code

  <?php 
  include('../db.php');

   $division_st = $_POST['division_st'];
   $schiff_st = $_POST['schiff_st'];
   $var_idtest_1 = $_POST['var_idtest_1'];  // this one works
   $var_idtest_2 = $_POST['var_idtest_2']; 
   $var_idtest_3 = $_POST['var_idtest_3']; 
   $var_idtest_4 = $_POST['var_idtest_4'];
   $var_idtest_5 = $_POST['var_idtest_5'];
   $var_idtest_6 = $_POST['var_idtest_6'];
   $var_idtest_7 = $_POST['var_idtest_7'];
   $var_idtest_8 = $_POST['var_idtest_8'];
   $var_idtest_9 = $_POST['var_idtest_9'];
   $var_idtest_10 = $_POST['var_idtest_10'];
   $deck_fz = $_POST['deck_fz'];
   $kontroll = $_POST['kontroll'];

   $name = $var_idtest_1;

   $count = count($name);

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

    $name_clean = mysqli_real_escape_string($con, $var_idtest_1[$i]);
    $std_clean = mysqli_real_escape_string($con, $var_idtest_2[$i]);
    $mtr_clean = mysqli_real_escape_string($con, $var_idtest_3[$i]);
     // $ber_clean = mysqli_real_escape_string($con, $var_idtest_5[$i]);

    $sql = "INSERT INTO `offcanvasfour` (
            `division`, 
            `schiff`,
            `var_idtest_1`,
            `var_idtest_2`,
            `var_idtest_3`,  
            `var_idtest_4`, 
            `var_idtest_5`, 
            `var_idtest_6`, 
            `var_idtest_7`, 
            `var_idtest_8`,
            `var_idtest_9`,
            `var_idtest_10`, 
            `deck_fz`, 
            `kontroll`
        ) 
        VALUES (
            '$division_st', 
            '$schiff_st', 
            '$name_clean', 
            '$std_clean', 
            '$mtr_clean',  
            '$var_idtest_4', 
            '$var_idtest_5', 
            '$var_idtest_6',
            '$var_idtest_7', 
            '$var_idtest_8',
            '$var_idtest_9',
            '$var_idtest_10',
            '$deck_fz',
            '$kontroll'
        )";

        $query= mysqli_query($con,$sql);
        }

        $lastId = mysqli_insert_id($con);

        if( $query == true )
        {
         $data = array(
         'status'=>'true',
        );
        echo json_encode($data);
        }
        else
        {
        $data = array(
        'status'=>'false',
        );
        echo json_encode($data);
        } 
  ?>

PS: Thanks for your time

Warning: You are wide open to SQL Injections and should use parameterized prepared statements instead of manually building your queries. They are provided by PDO or by MySQLi. Never trust any kind of input! Even when your queries are executed only by trusted users, you are still in risk of corrupting your data. Escaping is not enough! - Dharman
Please visit the help center, take the tour to see what and How to Ask. If you get stuck, post a minimal reproducible example of your attempt, noting input and expected output using the [<>] snippet editor. - mplungjan
Can you please limit your question to either PHP or jQuery? Which one are you having trouble with? - Dharman
$("#add_idtest_2").val() means the element with the id add_idtest_2. I can see that you have an element with the class name add_idtest_2, but none with that id. The same for the other ones as well. If you want to fetch an element based on the class name, then it's $(".add_idtest_2") (. instead of #) - M. Eriksson