I trying to insert a row in codeigniter and row inserted.
Problem is all row inserted properly but in sql bighint(11) field inserted 0.
I checked properly in array value given.
$data = [
'sku' => $POST['rec1'],
'pruch_price' => $POST['rec2'],
'sell_price' => $POST['rec3']
];
$model->insert ($data);
you should use $_POST[] instead of $POST.
But better yet, don't send $_POST directly to the models, instead use the post request provided by Codeigniter 4.
$data = [
'sku' => $this->request->getPost('rec1'),
'pruch_price' => $this->request->getPost('rec2'),
'sell_price' => $this->request->getPost('rec3')
];
$model->insert($data);
Related
While it is possible to have multiple arguments for the updateOrInsert in Laravel query builder and what is the operator used by default.
For example in the documentation it is mentioned:
DB::table('users')
->updateOrInsert(
['email' => 'john#example.com', 'name' => 'John'],
['votes' => '2']
);
Does that mean that email && name are checked or does it mean email || name is checked? How can we control it for one or the other if required?
Please forgive me if this is a silly question or if it is not worded as per the correct vocabulary, as I am new to Laravel. I couldn't find this information in the documentation or API.
updateOrInsert() method is used to update an existing record in the database if matching the condition or create if no matching record exists. Its return type is Boolean.
Syntax :
DB::table('blogs')->updateOrInsert(
[Conditions],
[fields with value]
);
In your query :
DB::table('users')->updateOrInsert(
['email' => 'john#example.com', 'name' => 'John'],
['votes' => '2']
);
It will check if email == 'john#example.com' & name == 'john', then it will update votes=2.
i am using DB to store values in database.
i have "course fees" column i what to "increment" the "course_fees" value in column.
for example
DB::table('student')->where('registration_id','=', $request->registration_id)->increment(['course_fees' =>$request->course_fees]);
this code increment the inserted value
how can i modified below code for increment "course_fees" value like above
DB::table('student')->where('registration_id','=', $request->registration_id)->update(['payment_date' => $request->payment_date,'balance_fees' => $request->balance_fees,'course_fees' =>$request->course_fees]);
You cannot use this method to increment multiple fields. You can use:
$studentQuery = DB::table('student')->where('registration_id','=', $request->registration_id);
(clone $studentQuery)->increment('payment_date',$request->payment_date);
(clone $studentQuery)->increment('balance_fees', $request->balance_fees);
(clone $studentQuery)->increment('course_fees', $request->course_fees);
but this way you will run 3 database queries to update.
But if you are sure there is exactly single record found for registration_id you can do it like this:
$student = DB::table('student')->where('registration_id','=', $request->registration_id)->first();
$student->update([
'payment_date' => $student->payment_date + $request->payment_date,
'balance_fees' => $student->balance_fees + $request->balance_fees,
'course_fees' => $student->course_fees + $request->course_fees
]);
EDIT
If you want to increment only course_fees column and want to update other 2 columns from input you can use:
DB::table('student')->where('registration_id','=', $request->registration_id)
->increment('course_fees' , $request->course_fees, [
'payment_date' => $request->payment_date,
'balance_fees' => $request->balance_fees
])
This is documentation about increment/decrement methods.
increment()/decrement() can take 3 parameters: $column, $amount, $extra.
$column is the field that you want to increment
$amount is by how much you want to increment the field by
$extra is an array of attributes that you also want to update in the query.
If you don't pass an amount the default for $amount is 1.
To achieve what you're after you could do:
DB::table('student')
->where('registration_id', $request->registration_id)
->increment('course_fees', $request->course_fees, [
'payment_date' => $request->payment_date,
'balance_fees' => $request->balance_fees,
]);
I have the following rule in the validator:
'keywords' => 'array|required'
'date_intervals' => 'array|required'
The array needs to be populated with at minimum 1 element (should not be empty).
Is there an existing rule to achieve this, or is it required that I write a custom rule for it?
Does min:1 work with array validation?
Thanks.
No, you'd be better counting the array elements and then passing the count for validation.
$count = count(Input::get('keywords'));
$input = ['keywords' => Input::get('keywords'),'count' => $count];
$rules = ['keywords' => 'required|array', 'count' => 'min:1'];
I have lengthy search form and i am trying to keep the form values in session. I use codeigniter pagination, so i need to store the values entered in the search form in the session (i am trying to avoid storing them in the database)
This problem bother me couple of days and i am unable to solve it. Here is the code. For the sake of clarification i provide the comments for each line of my code:
// my submit button named search_button has been clicked
if ($this->input->post('search_button')) {
// all the values from the form are stored in array
$formValues = $this->input->post(NULL, TRUE);
// i assign the values of $formValues array to session
$this->session->set_userdata('formValues', $formValues);
// i store the values in local variable
$mySessData = $this->session->userdata('formValues');
// i count the values of the $formValues to be sure how many fields i get back
// reason for that is that my form is built dynamically
// returns 19 if the form has been submitted.
// when i submit the form, i get 19
echo "Count of form values is now: " . count($formValues);
// i print the $formValues array, to be sure that it return the form values
// and yes, it does if the form has been submitted.
// see the $formValues returns section bellow to see what is returned
echo "<pre>";
print_r($formValues);
echo "</pre>";
// i print my local variable to ensure that variable $mySessData is not empty
// see the section $mySessData retruns (1) to see returned values
echo "Confirm that sess data has been set up";
echo "<pre>";
print_r($mySessData);
echo "</pre>";
} else {
// else, means that form has not been submitted but that controller has been called from the next link
// in the pagination links. This always return empty array, that is nothing...
echo "<pre>";
print_r($mySessData);
echo "</pre>";
}
If the form has been submitted, the $formValues returns the following:
Array
(
[addtypeid] =>
[isnew] =>
[orderby] =>
[geographicareaid] =>
[catid] => 1
[catid2] =>
[manufacturerid] =>
[modelid] =>
[yearofmanufacturing_from] =>
[yearofmanufacturing_to] =>
[hoursused_from] =>
[hoursused_to] =>
[horsepowers_from] =>
[horsepowers_to] =>
[price_from] =>
[price_to] =>
[colorid] =>
[isdamaged] =>
[search_button] => Submit‚
)
My variable $mySessData returns the very same dataif the form has been submitted:
Array
(
[addtypeid] =>
[isnew] =>
[orderby] =>
[geographicareaid] =>
[catid] => 1
[catid2] =>
[manufacturerid] =>
[modelid] =>
[yearofmanufacturing_from] =>
[yearofmanufacturing_to] =>
[hoursused_from] =>
[hoursused_to] =>
[horsepowers_from] =>
[horsepowers_to] =>
[price_from] =>
[price_to] =>
[colorid] =>
[isdamaged] =>
[search_button] => Submit‚
)
And on the end, if i click on pagination link
$config['base_url'] = site_url('/searchresults/display/'.$limit.'/');
, which point to the same controller and same method, well..i get an empty array.
Any help will be appreciated.
Regards, John
I think It would be better if use method="get" in your form. posting your form with get instead of post would be more preferable in case of search form as u wont need to maintain any session and no need of storing values in database also. you can easily access a feild anytime using
$this ->input->get('fild_name');
You can optimize your code this way and you dont need to maintain session variables as you will always have the search feild valies in your url. Using session another problem is with unsetting the session variable.
I personally dont think that creating a session variable just to maintain pagination is not a good idea.Moreover in a search form there is no need of security so you can send your form using "get" insted of "post"
I am following nettut+ tutorial for pagination and to store POST inputs as querystrings in db. So far, everything works fine until, suppose if I get an array as POST input, i am unable to loop through it and get all the array values and to store into query_array (i.e., store array within array).
The snippets below:
$query_array = array(
'gender' => $this->input->post('gender'),
'minage' => $this->input->post('minage'),
'maxage' => $this->input->post('maxage'),
'Citizenship' => $this->input->post('citizenship'), // checkboxes with name citizenship[]
);
This returns only last stored array value in Citizenship.
The output array:
Array ( [gender] => 1 [minage] => 18 [maxage] => 24 [Citizenship] => 2 )
makes the query string as:
&gender=1&minage=18&maxage=24&Citizenship=2
But, my requirement is to get all the values of 'Citizenship' array instead of last stored value.
The output required to make query string:
Array ( [gender] => 1 [minage] => 18 [maxage] => 24 [Citizenship] => 2 [Citizenship] => 4 [Citizenship] => 6 )
The query string :
&gender=1&minage=18&maxage=24&Citizenship[]=2&Citizenship[]=4&Citizenship[]=6
Any help appreciated..
Thanks.
Doesn't look like code ignighter supports un-named multidimensional arrays as input without a bit of hacking.
If you can access raw $_POST data try replacing
$this->input->post('citizenship')
with
array_map('intval',$_POST['citizenship'])
Alternativly add keys to your post data:
&gender=1&minage=18&maxage=24&Citizenship[0]=2&Citizenship[1]=4&Citizenship[2]=6
I fixed it myself. I just looped through the POST array and got the individual array key & pair values.
foreach($_POST['Citizenship'] as $k => $v) {
$Citizenship[$v] = $v;
}
Hope this helps someone who face similar problem.