从表中获取值并将该值提交到另一个表mysql php

时间:2019-03-16 11:15:18

标签: php mysql

所以我想从表单(而不是填充表单)向数据库表中输入数据。之后,我想将输入匹配到某个表(表A)以获取输入数据的ID。然后我想将该ID插入另一个表中...

<?php    
//submit attendance mahasiswa
    if(isset($_POST['buttonSubmitAbsenMahasiswa'])){

        foreach($_POST['inputStatusAbsen'] as $id_absensi=>$status_absensi){
            $id_mahasiswa = $_POST['inputHiddenIDMahasiswa'][$id_absensi];
            $nim_mahasiswa = $_POST['inputHiddenNIMMahasiswa'][$id_absensi];
            $nama_mahasiswa = $_POST['inputHiddenNamaMahasiswa'][$id_absensi];
            $nama_matakuliah = $_POST['inputHiddenMataKuliahMahasiswa'][$id_absensi];
            $tanggal_absensi = date("Y-m-d");       

            //get matakuliah id
            $class_query = "SELECT * FROM matakuliah WHERE nama_matakuliah='".$nama_matakuliah."'";
            $class_id = mysqli_query($connection, $class_query);

            if($class_id){
                while($class_id1 = $class_id->fetch_assoc()){
                    $class_id_fin = $class_id1['id_matakuliah'];
                }
            }

            //get mahasiswa id
            $student_query = "SELECT * FROM data_mahasiswa WHERE nama_mahasiswa='".$nama_mahasiswa."'";
            $student_id = mysqli_query($connection, $student_query);

            if($student_id) {
                while ($student_id1 = $student_id->fetch_assoc()) {
                    $student_id_fin = $student_id1['id_mahasiswa'];
                }
            }

            //insert or update the record
            $query_insert_data_absensi = "INSERT INTO data_absensi VALUES ('$class_id_fin' , '$student_id_fin' , '$tanggal_absensi' , '$status_absensi')";

            mysqli_query($connection, $query_insert_data_absensi);
        }
    }
?>

我想将输入的数据匹配到:

CREATE TABLE data_mahasiswa(
id_mahasiswa int(255) PRIMARY KEY AUTO_INCREMENT,
nim_mahasiswa varchar(255),
nama_mahasiswa varchar(255),
matakuliah_mahasiswa varchar(255));

或此处:

CREATE TABLE matakuliah(
id_matakuliah int(255) PRIMARY KEY AUTO_INCREMENT,
nama_matakuliah varchar(255));

以便我可以获取对应表的ID

然后我要将ID插入到:

CREATE TABLE data_absensi(
id_absensi int(255) PRIMARY KEY AUTO_INCREMENT,
id_mahasiswa int(255),
id_matakuliah int(255),
tanggal_absensi date,
status_absensi varchar(255));

输入如下:

<tr>
  <td>
    <?php echo $class['id_mahasiswa']; ?>
    <input type="text" hidden value="<?php echo $class['id_mahasiswa']; ?>" id="inputHiddenIDMahasiswa" name="inputHiddenIDMahasiswa[<?php echo $i; ?>]">
  </td>
  <td>
    <?php echo $class['nim_mahasiswa']; ?>
    <input type="text" hidden value="<?php echo $class['nim_mahasiswa']; ?>" id="inputHiddenNIMMahasiswa" name="inputHiddenNIMMahasiswa[<?php echo $i; ?>]">
  </td>
  <td>
    <?php echo $class['nama_mahasiswa']; ?>
    <input type="text" hidden value="<?php echo $class['nama_mahasiswa']; ?>" id="inputHiddenNamaMahasiswa" name="inputHiddenNamaMahasiswa[<?php echo $i; ?>]">
  </td>
  <td>
    <?php echo $class['matakuliah_mahasiswa']; ?>
    <input type="text" hidden value="<?php echo $class['matakuliah_mahasiswa']; ?>" id="inputHiddenMataKuliahMahasiswa" name="inputHiddenMataKuliahMahasiswa[<?php echo $i; ?>]">
  </td>
  <td>
    <div class="form-check form-check-inline">
      <input class="form-check-input" type="radio" name="inputStatusAbsen[<?php echo $i; ?>]" value="Hadir">
      <label class="form-check-label" for="inlineRadio1">Hadir</label>
    </div>
    <div class="form-check form-check-inline">
      <input class="form-check-input" type="radio" name="inputStatusAbsen[<?php echo $i; ?>]" value="Tidak hadir">
      <label class="form-check-label" for="inlineRadio2">Tidak Hadir</label>
    </div>
  </td>
</tr>

我哪里出错了? p.s对非英语变量感到抱歉:|

1 个答案:

答案 0 :(得分:0)

所以在看到类似的问题right here

之后,我终于找到了解决我问题的方法

我将输入更改为:

<?php
    $i = 0;
        while($row = mysqli_fetch_array($result_pencarian)){
        ?>
            <tr>
                <td>
                    <?php echo $row['id_mahasiswa']; ?>
                    <input type="text" hidden value="<?php echo $row['id_mahasiswa']; ?>" id="inputHiddenIDMahasiswa" name="inputHiddenIDMahasiswa[<?php echo $i; ?>]"/>
                </td>
                <td>
                    <?php echo $row['nim_mahasiswa']; ?>
                    <input type="text" hidden value="<?php echo $row['nim_mahasiswa']; ?>" id="inputHiddenNIMMahasiswa" name="inputHiddenNIMMahasiswa[<?php echo $i; ?>]"/>
                </td>
                <td>
                    <?php echo $row['nama_mahasiswa']; ?>
                    <input type="text" hidden value="<?php echo $row['nama_mahasiswa']; ?>" id="inputHiddenNamaMahasiswa" name="inputHiddenNamaMahasiswa[<?php echo $i; ?>]"/>
                </td>
                <td>
                    <?php echo $row['matakuliah_mahasiswa']; ?>
                    <input type="text" hidden value="<?php echo $row['matakuliah_mahasiswa']; ?>" id="inputHiddenMataKuliahMahasiswa" name="inputHiddenMataKuliahMahasiswa[<?php echo $i; ?>]"/>
                </td>
                <td>
                    <div class="form-check form-check-inline">
                        <input class="form-check-input" type="radio" checked="checked" name="inputStatusAbsen[<?php echo $i; ?>]" value="Hadir"/>
                        <label class="form-check-label" for="inlineRadio1">Hadir</label>
                    </div>
                    <div class="form-check form-check-inline">
                        <input class="form-check-input" type="radio" name="inputStatusAbsen[<?php echo $i; ?>]" value="Tidak hadir"/>
                        <label class="form-check-label" for="inlineRadio2">Tidak Hadir</label>
                    </div>
                </td>
            </tr>
        <?php
            $i++;
        }?>

其余的php相同