Reputation: 223
I make 2 forms in 3 files: loginMHS.php
, inputPerwalian.php
, insertMataKuliah.php
.
I have 4 tables in database: mahasiswa
, matakuliah
, kelas
, mahasiswa_kelas
.
mahasiswa
table has these fields:
nrp | nama | pass | jatah_sks | foto_profil
matakuliah
table has these fields:
kode_mk | nama | jumlah_sks | deskripsi
kelas
table has these fields:
kode_kelas | kode_mk | kode_periode | nama_kelas
mahasiswa_kelas
table has these fields:
nrp | kode_kelas
First, loginMHS.php
showed up. It has a form, which has an input stored in var $nrp
(from mahasiswa
table). After login, inputPerwalian.php
showed up. It has a form too, which has inputs stored in var $kodeMK
(from matakuliah
table) & $namaKelas
(from kelas
table). Then, the inputs will be inserted to table mahasiswa_kelas
.
This is my code (the inputs can't be stored to DB, but it shows no error):
$sql = "select kode_kelas from kelas inner join mahasiswa_kelas"
. " on kelas.kode_kelas = mahasiswa_kelas.kode_kelas"
. " where nama_kelas=" . $namaKelas;
$result = mysqli_query($link, $sql);
$sql2 = "insert into mahasiswa_kelas (nrp, kode_kelas)"
. " values ($nrp, $result)";
$result2 = mysqli_query($link, $sql2);
$row2 = mysqli_fetch_array($result2);
I don't know how to insert it, while the data inputed is from another table. Because, from what I know is, I can't use inner join in insert query. How to solve it? Please explain your answer. Thanks
Upvotes: 0
Views: 142
Reputation: 585
First you can get nrp value from loginMHS.php. You have to pass that nrp value to inputPerwalian.php. In inputPerwalian.php you can get value for kodemk. if you submit that form u will get 2 important needed data that are nrp and kodemk. Now u would have nrp and kodemk.
now you have to insert into mahasiswa_kelas with nrp, kode_kelas. Already you have nrp, but u dont have kode_kelas. in order to get kode_kelas value you have to select kode_kelas data from kelas table by select query.
"SELECT kode_kelas FROM kelas where kode_mk = kodemk "
By using this query you can get kode_kelas value.
Now you can insert nrp and kode_kelas values in mahasiswa_kelas.
Hope it will helpful.
Upvotes: 0
Reputation: 369
mysqli_query()
returns an object so you can't insert $result
as a value. Check out how they handle it in the manual with mysqli_fetch_row This should take care of your problem
Update:
Not sure what you mean by 'use' but I think not . . . keep reading. You need to parse your array from mysqli_fetch_row()
. Look at
if ($result = mysqli_query($link, $query)) {
/* fetch associative array */
while ($row = mysqli_fetch_row($result)) {
printf ("%s (%s)\n", $row[0], $row[1]);
}
free result set */ mysqli_free_result($result);
}`
Update 2:
Not sure if I should be writing the code for you but here you go. As described mysqli_query()
returns $result as an object. mysqli_fetch_row()
returns $result as an array. Get your value from the proper array index and assign to a variable $kode_kelas_value
. Insert that variable into the db with your $sql2 query.
$sql = "select kode_kelas from kelas inner join mahasiswa_kelas"
. " on kelas.kode_kelas = mahasiswa_kelas.kode_kelas"
. " where nama_kelas=" . $namaKelas;
if ($result = mysqli_query($link, $sql)) {
while ($row = mysqli_fetch_row($result)) {
$kode_kelas_value = $row[0];
}
/*free result set */
mysqli_free_result($result);
}`
$sql2 = "insert into mahasiswa_kelas (nrp, kode_kelas)"
. " values ($nrp, $kode_kelas_value)";
$result2 = mysqli_query($link, $sql2);
$row2 = mysqli_fetch_array($result2);
Upvotes: 1
Reputation: 585
you should fetch nrp, kode_kelas values by using join query. If you got those values perfectly then you can insert simply know. Any queries ask me.
Upvotes: 0