In CodeIgniter, I want to insert a row into a db that includes a timestamp for "now".
$message_row = array(
"sender_email" => $recruiter_email,
"recipient_email" => $recruiter_email,
"message" => $recruiter_email,
"send_date" => "NOW()" // hmmm how do I do this?
);
$this->db->insert('messages', $message_row);
If I use "NOW()" it will just enter the string, and not actual have MySQL use its NOW keyword.
In order to use NOW() function in CodeIgniter you need to use "set" function
$table = 'user';
$message_row = array(
"sender_email" => $recruiter_email,
"recipient_email" => $recruiter_email,
"message" => $recruiter_email
);
$this->db->set('send_date', 'NOW()', false);
$this->db->insert($table, $message_row);
if you need to be timezone aware, i recommend you store the datetime in UTC format.
so:
$now = new DateTime ( NULL, new DateTimeZone('UTC'));
$message_row = array(
"sender_email" => $recruiter_email,
"recipient_email" => $recruiter_email,
"message" => $recruiter_email,
"send_date" => $now->format('Y-m-d H:i:s')
);
when you retrieve the information, you have to know the user's timezone, using a $row that contains the send_date:
$then = new DateTime( $row->send_date, new DateTimeZone('UTC'));
$then->setTimeZone(new DateTimeZone($userstimezone));
echo $then->format('Y-M-D H:i:s');
Related
I have to update the expiry date by counting from the month. Please guide me. While saving data expiry date is working fine. But I don't understand how to update it.
My Controller Code For Save Data
public function pay_success(Request $request){
$input = $request->all();
date_default_timezone_set('asia/calcutta');
$input['months'] = $request->months;
$expiry_date = Carbon::now()->addMonths($input['months']);
$input['expiry_date'] = $expiry_date;
$input['password'] = bcrypt($input['password']);
$user = User::create($input);
//Send Email
$email = $input['email'];
$messageData = ['email' =>$input['email'],'name' =>$input['name'],'package' =>$input['package'],'months' =>$input['months'],'amount' =>$input['amount'],'expiry_date' =>$input['expiry_date']];
Mail::send('emails.mail',$messageData,function($message) use($email){
$message->to($email)->subject('Registration with AddSpy');
});
$arr = array('msg' => 'Payment successful.', 'status' => true);
return Response()->json($arr);
}
My Update Code is
public function update(Request $request) {
date_default_timezone_set('asia/calcutta');
$months = $request->months;
$expiry_date = Carbon::now()->addMonths($months);
$request['expiry_date'] = $expiry_date;
$data = ['id'=>$request->id, 'name'=>$request->name, 'phone'=>$request->phone, 'country'=>$request->country, 'state'=>$request->state,
'purpose'=>$request->purpose, 'package'=>$request->package, 'months'=>'$months', 'quantity'=>$request->quantity, 'amount'=>$request->amount, 'expiry_date'=>'$expiry_date'];
DB::table('users')->where('id',$request->id)->update($data);
return response()->json($data);
}
Anyone please suggest me a answer. I do changes in my code but It gives this message "message": "SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: '$expiry_date' for column addspy.users.expiry_date at row 1 (SQL: update users set id = 47, name = Ayush, phone = 6393611129, country = India, state = UP, purpose = parent, package = basic, months = $months, quantity = 1, amount = 4000, expiry_date = $expiry_date where id = 47)",
"exception": "Illuminate\Database\QueryException",
Thanks in advance
You're sending strings to the database ('$months' & '$expiry_date'). Simply removing the quotes should fix your problem.
i.e.
$data = [
'id' => $request->id,
'name' => $request->name,
'phone' => $request->phone,
'country' => $request->country,
'state' => $request->state,
'purpose' => $request->purpose,
'package' => $request->package,
'months' => $months,
'quantity' => $request->quantity,
'amount' => $request->amount,
'expiry_date' => $expiry_date,
];
I am using Codeigniter 3.x and want to see an update_batch query, but not run it, while I am debugging the code.
This works for an update_batch:
$this->db->update_batch("`" . $this->fullGamesTable . "`", $fullGames, 'gameid');
and updates the database, but I want to view the update and not actually do the update.
Thanks.
Can you do like this
$data = array(
array(
'opt_id' => $hoptid1,
'q_id' => $hid,
'opt_val' => $sin_yes,
'opt_crct' => $sin_yescrt,
'opt_mark' => '1'
),
array(
'opt_id' => $hoptid2,
'q_id' => $hid,
'opt_val' => $sin_no,
'opt_crct' => $sin_nocrt,
'opt_mark' => '1'
)
);
$this->db->update_batch('option', $data, 'opt_id');
Try this
public function update_batch()
{
$data = $this->db->select('id,description')->from('insert_batch')->group_by('url')->get()->result_array();
$batch_update = [];
foreach ($data as $key => $value) {
$value['description'] = 'description';
$batch_update[] = [
'id' =>$value['id'],
'description' => $value['description']
];
}
echo "<pre>"; print_r($batch_update);
$this->db->update_batch('insert_batch',$batch_update,'id');
}
i already tried different ways and some post here
the 1st data is working savings to acadans_tbl but the update who's user login is not working.
view_file
<td><input type="text" name="assessval" id="assessval" value="1"></td>
Controller
$data1 = array(
'Studnum' => $this->session->userdata['snum'],
'Department' => $this->session->userdata['dept'],
'Course' => $this->session->userdata['course'],
'answer1' => $this->input->post('answer1'),
'answer2' => $this->input->post('answer2'),
'answer3' => $this->input->post('answer3'),
'answer4' => $this->input->post('answer4'),
'answer5' => $this->input->post('answer5'),
'answer6' => $this->input->post('answer6'),
'answer7' => $this->input->post('answer7'),
'answer8' => $this->input->post('answer8'),
'answer9' => $this->input->post('answer9'),
'answer10' => $this->input->post('answer10')
);
$data2 = array(
'assessval' => $this->input->post('assessval')
);
if ($this->Questions_model->create_ans($data1, $data2)) {
MODEL
public function create_ans($data1, $data2) {
$this->db->insert('acadans_tbl', $data1);
$data2['assesed'] = $this->db->insert_id();
$this->db->insert('users', $data2);
}
In your Controller, you write:
$data2 = array(
'assessval' => $this->input->post('assessval')
);
Its mean that you will insert the value from $this->input->post('assessval') to field assessval in the database. So, you have to ensure in the users table you have assessval field.
And in your model, just insert to the database:
$this->db->insert('users', $data2);
I have the following query
public static function createConversation( $toUserId )
{
$now = date('Y-m-d H:i:s');
$currentId = Auth::user()->id;
$results = DB::table('pm_conversations')->insert(
array( 'user_one' => $currentId, 'user_two' => $toUserId, 'ip' => Request::getClientIp(), 'time' => $now )
);
return $results;
}
How would i return the id of the row just inserted?
Cheers,
Instead of doing a raw query, why not create a model...
Call it Conversation, or whatever...
And then you can just do....
$result = Conversation::create(array( 'user_one' => $currentId, 'user_two' => $toUserId, 'ip' => Request::getClientIp(), 'time' => $now ))->id;
Which will return an id...
Or if you're using Laravel 4, you can use the insertGetId method...In Laravel 3 its insert_get_id() I believe
$results = DB::table('pm_conversations')->insertGetId(
array( 'user_one' => $currentId, 'user_two' => $toUserId, 'ip' => Request::getClientIp(), 'time' => $now )
);
This method requires that the id of the table be auto-incrementing, so watch out for that...
The last method, is that you can just return the last inserted mysql object....
Like so...
$result = DB::connection('mysql')->pdo->lastInsertId();
So if you choose that last road...
It'll go...
public static function createConversation( $toUserId )
{
$now = date('Y-m-d H:i:s');
$currentId = Auth::user()->id;
$results = DB::table('pm_conversations')->insert(
array( 'user_one' => $currentId, 'user_two' => $toUserId, 'ip' => Request::getClientIp(), 'time' => $now )
);
$theid= DB::connection('mysql')->pdo->lastInsertId();
return $theid;
}
I would personally choose the first method of creating an actual model. That way you can actually have objects of the item in question.
Then instead of creating a model and just save()....you calll YourModel::create() and that will return the id of the latest model creation
You can use DB::getPdo()->lastInsertId().
Using Eloquent you can do:
$new = Conversation();
$new->currentId = $currentId;
$new->toUserId = $toUserId;
$new->ip = Request::getClientIp();
$new->time = $now;
$new->save();
$the_id = $new->id; //the id of created row
The way I made it work was I ran an insert statement, then I returned the inserted row ID (This is from a self-learning project to for invoicing):
WorkOrder::create(array(
'cust_id' => $c_id,
'date' => Input::get('date'),
'invoice' => Input::get('invoice'),
'qty' => Input::get('qty'),
'description' => Input::get('description'),
'unit_price' => Input::get('unit_price'),
'line_total' => Input::get('line_total'),
'notes' => Input::get('notes'),
'total' => Input::get('total')
));
$w_id = WorkOrder::where('cust_id', '=', $c_id)->pluck('w_order_id');
return $w_id;
I am trying to add an entry to an Oracle table which has a date field. So far, I've only been able to do it like this:
$createdDate = $entry->createdDate->toString('yyyy-MM-dd');
$data = array(
'ID' => $entry->id,
'STATE' => $entry->state,
'CREATED_DATE' => new Zend_Db_Expr("to_date('$createdDate', 'YYYY-MM-DD')")
);
$this->_getGateway()->insert($data);
Is there a better way? This solution smells dirty to me.
You should be able to do this instead:
$createdDate = $entry->createdDate->toString('yyyy-MM-dd');
$data = array(
'ID' => $entry->id,
'STATE' => $entry->state,
'CREATED_DATE' => new Zend_Db_Expr("date '$createdDate'")
);
$this->_getGateway()->insert($data);
The only difference is the use of an ANSI date literal in line 5.