Membuat Insert Data Banyak Di Codeigniter 7,9/10 9942 votes

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.

  1. Insert Multiple Rows In Mysql Using Codeigniter

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

Insert multiple rows in mysql using codeigniter

Controller

MODEL

Mirza ChilmanMirza Chilman
1521 gold badge2 silver badges15 bronze badges
Insert

3 Answers

You need to return last insert id form your model file

Nexusmods mount and blade

In model

In controller

SatySaty
20.5k6 gold badges24 silver badges44 bronze badges

Before 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:

  1. Grabbing last insert id using codeigniter's insert_id() function.

  2. 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

Aman DhandaAman Dhanda

you can do this (only few changes)

controller.php

model.php

elddenmedioelddenmedio
9921 gold badge5 silver badges15 bronze badges

Not the answer you're looking for? Browse other questions tagged phpcodeigniter or ask your own question.

Trick to upload multiple files using CodeIgniter's Upload Library
codeigniter.multiple.file.upload.php
<?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;
}
?>

commented May 3, 2017

Thanks for the trick @zitoloco.

commented May 25, 2017

Thanks for share this trick.

But I want to Multiple upload, multiple insert to 2 table join and multiple insert data in 1 table.
How to implement this case, Please guide me, thanks

commented Jun 14, 2017

$_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];

Check It again

commented Jul 22, 2017

Please I don't understand this line
$number_of_files_uploaded = count($_FILES['upl_files']['name']);
Why did you use 'upl_files' as the file name when you said:

In your HTML form, your input[type=file] must be named *userfile[]

Please Explain. Thanks

commented Aug 5, 2017
edited

your ouw function to generate random names
What is this ?
Can you explain us ?

commented Oct 2, 2017

please explain the concept for each and for which one is fast for uploading image data files
and how to fetch that images by code ignitor

commented Nov 2, 2017

#el-mahbub
generate random names means we are generating the random(unique) names for each files bcz uploaded files must have different names, for that u can use for eg let say 'uniqid()' function, it will generate a 16 digit unique names i think.

commented Dec 13, 2017

Nice trick, dude.
Explanation of the code in comments would be great for newbie coders.

commented Dec 30, 2017

How to insert multiple records into database using php codeigniter

thank sir zitoloco

commented Jan 11, 2018

One essential remark in line 38:

if ( ! $this->upload->do_upload('userfile')):

Otherwise you get error: You did not select file to upload

commented Oct 11, 2018

exiting solutions. Thanks a lot

commented Oct 12, 2018

Insert Multiple Rows In Mysql Using Codeigniter

Amazing contribution

commented May 28, 2019

how to upload multiple images with database in codeigniter

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment