Inserting NOW() into Database with CodeIgniter's Active Record
Unless I am greatly mistaken, the answer is, "No, there is no way."
The basic problem in situations like that is the fact that you are calling a MySQL function and you're not actually setting a value. CI escapes values so that you can do a clean insert but it does not test to see if those values happen to be calling functions like aes_encrypt
, md5
, or (in this case) now()
. While in most situations this is wonderful, for those situations raw sql is the only recourse.
On a side, date('Y-m-d');
should work as a PHP version of NOW()
for MySQL. (It won't work for all versions of SQL though).
Using something like NOW() in a Codeigniter insert array
Use the codeigniter date helper and keep the same notation:
$this->load->helper('date');
$data = array('node1id' => $leader_id,
'node2id' => $idgen,
'friendsSince' => now(),
$this->db->insert('users', $data);
More info on date helper in CI docs here: http://ellislab.com/codeigniter/user_guide/helpers/date_helper.html
Insert data with CodeIgniter Active Records
You may use set()
method.
According to CI documentation, you may use following syntax:
$this->db->set('name', $name);
$this->db->insert('mytable');
which will produce following query:
INSERT INTO mytable (name) VALUES ('{$name}')
Hopefully it's what you're looking for.
Insert Current DateTime in Codeigniter 3.0.6
this works for me in Codeigniter 3.0.6
$this->db->set('user_registered', 'NOW()', FALSE);
How to insert records using select active records in codeigniter
Try this:
If you select('*')
it will return many rows, so it is not advisable to use insert()
since insert
will only process ONE ROW
try using insert_batch()
$this->db->insert_batch("table2", $query->result());
//or
$this->db->insert_batch("table2", $query->result_array());
If you need to use insert()
you must put a limit(1)
to your first query.
Or you have to catch your query if it returns many rows
$numrows = $query->num_rows();
if($numrows == 1)
{
$this->db->insert("table2", $query->row());
}elseif($numrows > 1){
$this->db->insert_batch("table2", $query->result_array());
}
Codeigniter Active Record: Inserting empty records into multiple tables based on an id
Instead of using $this->db->set()
, you can store the required value in an array and then do the insertion. This may help
// Store the select statement in cache
$data = array("user_id"=>$user_id);
// Insert empty record in tables related to user
$this->db->insert('T1',$data);
$this->db->insert('T2',$data);
$this->db->insert('T3',$data);
how to get the result of active record insert in CodeIgniter
To not get the DB error message make sure that you have $db['default']['db_debug'] = FALSE
in the file /application/config/database.php
.
Then after you've preformed your (attempted) insert you can run:
$num_inserts = $this->db->affected_rows();
If the result is 0, your insert failed and you can present an error message of your own choosing.
Code igniter active record - inserting into a Point column
Try:
$this->db->set("id",$id);
$this->db->set("name",$name);
$this->db->set("location",'geomfromtext("POINT(1 1)")',false);
$this->db->insert("Venues");
how to get last insert id after insert query in codeigniter active record
Try this
function add_post($post_data){
$this->db->insert('posts', $post_data);
$insert_id = $this->db->insert_id();
return $insert_id;
}
In case of multiple inserts you could use
$this->db->trans_start();
$this->db->trans_complete();
Active Record insert query to save image in mysql database
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
Straight from the docs.
If you notice, they are sending the upload data to the view. So send it to the model instead:
$this->Users->Image_upload($this->upload->data());
Then your data is available in the array as is also explained in the docs:
$this->upload->data()
This is a helper function that returns an array containing all of the
data related to the file you uploaded. Here is the array prototype:
Array
(
[file_name] => mypic.jpg
[file_type] => image/jpeg
[file_path] => /path/to/your/upload/
[full_path] => /path/to/your/upload/jpg.jpg
[raw_name] => mypic
[orig_name] => mypic.jpg
[client_name] => mypic.jpg
[file_ext] => .jpg
[file_size] => 22.2
[is_image] => 1
[image_width] => 800
[image_height] => 600
[image_type] => jpeg
[image_size_str] => width="800" height="200"
)
So you can access the data in the model like so:
function Image_upload($data)
{
$handle = fopen($data['full_path'],'rb');
...
Related Topics
Arabic Characters in JSON Decoding
Using Ajax in a Wordpress Plugin
Use a Variable Within Heredoc in PHP
Differencebetween ' and " in PHP
Syntax Error, Unexpected T_Encapsed_And_Whitespace, Expecting T_String or T_Variable or T_Num_String
Phpmysql Error - #1273 - #1273 - Unknown Collation: 'Utf8Mb4_General_Ci'
Is Preventing Xss and SQL Injection as Easy as Does This
Sort an Array with Special Characters in PHP
Using Laravel Socialite to Login to Facebook
PHP - How to Implement Password Reset and Token Expiry
How to Convert All Characters to Their HTML Entity Equivalent Using PHP
How Validate Unique Email Out of the User That Is Updating It in Laravel
Check If String Contains Word in Array
Does PHP Optimize Tail Recursion
Is There an Equivalent in C++ of PHP's Explode() Function
Can Closing the Browser Terminate the PHP Script on the Server