-3

Here, i'am had some case that i thought that are uniq for me. this the problem, i want to looping some tag 3 times. The loop is work, but when it was input to database... just 1 loop that worked.

Here the Screenshot,

The value of loop

Result of loop

<td>
    <label for="baris">Baris</label>
    <select name="baris">
        <option value="">--</option>
        <?php 
            for ($i2=A; $i2 < F; $i2++) { 
                echo "<option name='".$i2."'>".$i2."</option>";
            }
         ?>
    </select>
</td>
<td>
    <label for="kolom">Kolom</label>
    <select name="kolom">
        <?php 
            for ($i3=1; $i3 <= 10; $i3++) { 
                echo '<option name="'.$i3.'">'.$i3.'</option>';
            }
         ?>
    </select>
</td>

also this code for input to database

<?php     
                if(isset($_POST['masukan'])){
                    $nama = htmlspecialchars($_POST['nama']);
                    $email = htmlspecialchars($_POST['email']);
                    $jk = htmlspecialchars($_POST['jk']);
                    $notlp = htmlspecialchars($_POST['notlp']);

                    $queryinput = mysqli_query($link, "UPDATE tb_customer 
                                                        SET nama='$nama',
                                                            email='$email',
                                                            notlp='$notlp',
                                                            jk='$jk'

                                                        WHERE id_cust='$id_cust1'    
                                                        ");

                    $querytampil = mysqli_query($link, "SELECT * FROM tb_customer WHERE id_cust ORDER by id_cust desc limit 1");
                    $dcus = mysqli_fetch_assoc($querytampil);
                    $id_cust = $dcus['id_cust'];

                    $querytampil2 = mysqli_query($link, "SELECT max(num_ticket)AS num  FROM tb_ticket");
                    $dnumti = mysqli_fetch_assoc($querytampil2);
                    $num_ticket = $dnumti['num'];
                    $hasil = $num_ticket + 1;

                    for ($i5= 1; $i5 <= $p; $i5++) { 

                    $baris = htmlspecialchars($_POST['baris']);
                    $kolom = htmlspecialchars($_POST['kolom']);
                        if($queryinput){
                            $id_flight = $data['id_flight'];
                            $queryinput2 = mysqli_query($link, "INSERT INTO tb_ticket VALUES('','$id_cust','$id_flight','$hasil','','','','','$id_dest','$id_ori', '$baris', '$kolom')");

                            if ($queryinput2) {
                                $querytampil3 = mysqli_query($link, "SELECT * FROM tb_ticket order by num_ticket desc limit 1");
                                $dtick = mysqli_fetch_assoc($querytampil3);
                                $nt = $dtick['num_ticket'];
                                echo "<script>alert('Succes.')</script>";
                                echo '<script>window.location="pembayaran.php?num_ticket='.$nt.'&&id_cust='.$id_cust.'"</script>';
                            }else{
                            echo "<script>alert('Your data cannot send.')</script>";
                            }
                        }else{
                            echo "<script>alert('Your data cannot send, please check your input data.')</script>";
                        }
                    }
                }
            ?>

Bad engslish sorry*

1 Answers1

0

If I got this correct; it's because you are not setting your html variables as an array.

The first changes you need to make are within your view page..

<select name="baris"> -> <select name="baris[]">

<select name="kolom"> -> <select name="kolom[]">

This sets your $_POST['baris'] and $_POST['kolom'] as arrays.

Since you're already looping...

for ($i5= 1; $i5 <= $p; $i5++) { 
    $baris = htmlspecialchars($_POST['baris']);
    $kolom = htmlspecialchars($_POST['kolom']);    

This stuff now needs to access the relevant array... And becomes...

for ($i5= 1; $i5 <= $p; $i5++) { 
    $baris = htmlspecialchars($_POST['baris'][$i5]);
    $kolom = htmlspecialchars($_POST['kolom'][$i5]);      
IsThisJavascript
  • 1,726
  • 2
  • 16
  • 25
  • Awesome to hear please press the tick under the upvote/downvote buttons so this thread can be solved && closed :) – IsThisJavascript Jun 04 '18 at 15:56
  • Just a friendly notice; `htmlspecialchars` shouldn't be used for database sanitation. Please give this topic a read [on how to prevent mysqli injection](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – IsThisJavascript Jun 04 '18 at 15:59