Search, Sort data from foreign table Generate fields 1) Input 2) Textarea 3). Cara membuat atau. May 16, 2019 - Using this article one can easily understand how to insert a value from a radio button into a MySQL database in PHP. Tutorial ini membahas mengenai cara membuat fungsi insert data menggunakan Codeigniter. Data yang diinsert menggunakan desain database yang telah dibahas pada tutorial sebelumnya. Sebelum melangkah lebih jauh untuk membuat fungsi insert ini, anda perlu mengetahui bahwa di dalam Codeigniter terdapat tiga buah direktori utama untuk melakukan coding.
i have 2 tables 'pengguna' and 'mahasiswa' and then 1 have a form which is 1 form inserting into 2 tables, so far i manage to insert the data but when it has to do with 'primary key' and 'foreign key' it has problem, as u can see from code below id_pengguna
from table pengguna
is a primary key and id_pengguna
from table mahasiswa
is a foreign key the problem is when i inserting the data, id_pengguna
from pengguna
has it value while in table mahasiswa
it has no value, below are my code, is there any simple way or am i doing something wrong?
PENGGUNA TABLEMAHASISWA TABLE
Controller
MODEL
Mirza ChilmanMirza ChilmanYou need to return last insert id form your model file
In model
In controller
SatySatyBefore inserting data to the 2nd table i.e. mahasiswa
you need to grab the primary id created after insertion of data in table 1 (pengguna
).
This can be done in 2 ways:
Grabbing last insert id using codeigniter's insert_id()
function.
After inserting the data in 1st table, get the id of the row which has the same username
. (For this you must have unique usernames in username
column) This will return the corresponding id.
Note: You can also do this with email column.
Source/Reference: Detailed explanation - Inserting data in 2 tables through 1 Form - CodeIgniter
you can do this (only few changes)
controller.php
model.php
elddenmedioelddenmedio<?php |
/* |
* Code above omitted purposely |
* In your HTML form, your input[type=file] must be named *userfile[]* |
*/ |
/* |
* Uploads multiple files creating a queue to fake multiple upload calls to |
* $_FILE |
*/ |
publicfunctionmultiple_upload() |
{ |
$this->load->library('upload'); |
$number_of_files_uploaded=count($_FILES['upl_files']['name']); |
// Faking upload calls to $_FILE |
for ($i=0; $i<$number_of_files_uploaded; $i++) : |
$_FILES['userfile']['name'] =$_FILES['upl_files']['name'][$i]; |
$_FILES['userfile']['type'] =$_FILES['upl_files']['type'][$i]; |
$_FILES['userfile']['tmp_name'] =$_FILES['upl_files']['tmp_name'][$i]; |
$_FILES['userfile']['error'] =$_FILES['upl_files']['error'][$i]; |
$_FILES['userfile']['size'] =$_FILES['upl_files']['size'][$i]; |
$config=array( |
'file_name'=><yourouwfunctiontogeneraterandomnames>, |
'allowed_types'=>'jpg jpeg png gif', |
'max_size'=>3000, |
'overwrite'=>FALSE, |
/* real path to upload folder ALWAYS */ |
'upload_path' |
=>$_SERVER['DOCUMENT_ROOT'] .'/path/to/upload/folder' |
); |
$this->upload->initialize($config); |
if ( !$this->upload->do_upload()) : |
$error=array('error'=>$this->upload->display_errors()); |
$this->load->view('upload_form', $error); |
else : |
$final_files_data[] =$this->upload->data(); |
// Continue processing the uploaded data |
endif; |
endfor; |
} |
?> |
Thanks for the trick @zitoloco. |
Thanks for share this trick. But I want to Multiple upload, multiple insert to 2 table join and multiple insert data in 1 table. |
$_FILES['userfile']['name'] = $_FILES['upl_files']['name'][$i]; Check It again |
Please I don't understand this line In your HTML form, your input[type=file] must be named *userfile[] Please Explain. Thanks |
your ouw function to generate random names |
please explain the concept for each and for which one is fast for uploading image data files |
#el-mahbub |
Nice trick, dude. |
thank sir zitoloco |
One essential remark in line 38: if ( ! $this->upload->do_upload('userfile')): Otherwise you get error: You did not select file to upload |
exiting solutions. Thanks a lot |
Amazing contribution |
how to upload multiple images with database in codeigniter |