-2

i want to insert both value and content of dropdown select into a database table. i can insert the value with no problem but i have no idea how to insert the content. i've been searching similar questions and probably it can be done by adding javascript. but i haven't learned about that yet so i have no clue how to use it.

<form action="hitung.php" method="POST" id="formid">
        <?php
            $query  = "SELECT nama FROM alternatif";
            $result = mysqli_query($conn, $query);

            for ($i=0; $i < $row = mysqli_fetch_array($result) ; $i++) { 
                
            
            ?>
            <tr>
                <th><?php echo $row['nama'] ?></th>
                <?php
                $select = mysqli_query($conn, "SELECT * FROM kriteria");
                $count = mysqli_num_rows($select);
                for ($j=0; $j < $count ; $j++) {
                ?>
                <td>
                
                        <select class="btn-secondary" name="subkrt[<?php echo $i ?>][<?php echo $j ?>]" required>
                        <option value="" style="display:none;" required>-Pilih-</option>
                        <?php
                        $query = mysqli_query($conn, "SELECT pv_alternatif.pv_subkriteria, subkriteria.nama FROM pv_alternatif INNER JOIN subkriteria ON pv_alternatif.id_alternatif = subkriteria.id WHERE id_kriteria=$j+1");
                        while ($a = mysqli_fetch_array($query)) {
                        ?>
                            <option value="<?php echo $a['pv_subkriteria']; ?>"><?php echo $a['nama']; ?></option>
                        <?php
                        }
                        ?>
                        </select>
                </td>
            
            <?php
                }
                }
            ?>  
                
            </tr>
    </form>

so both value and content are fetched from two different tables of my database. and hitung.php is code to insert into database.

include 'koneksi.php';
include 'fungsi.php';
$jmlsub = array();
$qwery = mysqli_query($conn, "SELECT nilai FROM pv_kriteria");
while ($row = mysqli_fetch_array($qwery)){ 
    $rows[] = $row['nilai'];
    $jmlsub[] = 0;
}

$query  = "SELECT nama FROM alternatif";
$result = mysqli_query($conn, $query);

$a = mysqli_query($conn, "SELECT * FROM nilai_alt");

for ($i=0; $i < $row = mysqli_fetch_array($result) ; $i++) {?>
    <?php
    $select = mysqli_query($conn, "SELECT * FROM kriteria");
    $count = mysqli_num_rows($select);
    for ($j=0; $j < $count ; $j++) {
        if(isset($_POST['subkrt'][$i][$j])){
            $matriks[$i][$j] = $_POST['subkrt'][$i][$j] * $rows[$j];
            $value = $matriks[$i][$j];
            $jmlsub[$i] += $value;

        $id_alternatif = getIDAlternatif($i);
        $id_kriteria = getKriteriaID($j);
        if (mysqli_num_rows($a)==0) {
            $b = "INSERT INTO nilai_alt VALUES('',$id_alternatif,$id_kriteria,$value)";
        } else {
            $b = "UPDATE nilai_alt SET nilai_alternatif=$value WHERE id_alternatif = $id_alternatif AND id_kriteria = $id_kriteria";
        }


        $rsult = mysqli_query($conn,$b);
        if (!$rsult) {
            echo "Gagal memasukkan / mengupdate nilai alternatif";
            exit();
        }
    }
    }
    }
    
$jmlAlternatif  = getJumlahSubKriteria();
for ($i=0; $i < ($jmlAlternatif); $i++) { 
    $id_alternatif = getIDAlternatif($i);
    $query = "INSERT INTO ranking VALUES ($id_alternatif,$jmlsub[$i]) ON DUPLICATE KEY UPDATE nilai=$jmlsub[$i]";
    $result = mysqli_query($conn,$query);
    if (!$result) {
        echo "Gagal mengupdate ranking";
        exit();
    }
}

header('Location: hasil_subkrt.php');
?>

tables:

enter image description here enter image description here

Any help will be greatly appreciated. Thanks.

Mon Freecs
  • 21
  • 4
  • 1
    Since the options are stored in a database, one idea is to record the ID of the option that is selected, rather than directly storing its value or content. That way, you can reference both the value and content in the future. See [relational database](https://www.oracle.com/database/what-is-a-relational-database/). – showdev Dec 15 '22 at 03:05
  • i haven’t looked at that yet. any suggestion how to do that? – Mon Freecs Dec 15 '22 at 03:19
  • Is it possible to share your database structure and some sample data? – showdev Dec 15 '22 at 03:19
  • i made an edit and added both tables, fetching pv_subkriteria and nama to options. – Mon Freecs Dec 15 '22 at 03:29
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). 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](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Dec 15 '22 at 11:17

1 Answers1

0

As you said you "can insert the value with no problem", so it is simple to pass all the value and content to the selected option value as a string using json_encode(...), then in hitung.php you just convert it back to array or object using json_decode(...)

If you have a array, example:

$a = array('pv_subkriteria' => 'the value', 'nama' => 'the name');

Then just

<option value='<?php echo htmlentities(json_encode($a)); ?>'><?php echo $a['nama']; ?></option>

Then in your case in hitung.php,

...
$a = json_decode(html_entity_decode($_POST['subkrt'][$i][$j]), true); 
...

More info, generally your code has nested loop and query database in loop, you need to think more about better query way to reduce number of loops to increase performance.

Edited and added an example using some php functions that might help you:

<?php
$a = array('pv_subkriteria' => 'the value', 'nama' => 'the name');
$str = htmlentities(json_encode($a));
$a2 = json_decode(html_entity_decode($str), true);
print_r($a2);
?>
Kody Fanz
  • 1
  • 4