I try to insert a row in the table on code igniter from a Array, but something is going wrong.
That's the array:
Array
(
[Date] => 2001-08-15
[Number] => 962883
[Time] => 17:40
[Etc1] => 0
[Etc2] => 0
)
And this the insert:
$this->db->insert('mytable', $myarray);
A new line is inserted, but all columns are empty!
Trying to find de error, I printed the last query by
echo $this->db->last_query() ." <br>";
And I got:
INSERT INTO `mytable` (`Date`, `Number`, `Time`, `Etc1`, `Etc2`)
VALUES
('\02\00\00\01\0-\00\08\0-\01\05\0', '\09\06\02\08\08\03\0', '\01\07\0:\04\00\0', '\00\0', '\00\0')
For some reason I can not get, the codeigniter ( or PHP ) is wrongly escaping the values.
Any Idea?
Firstly your change your array like this:
$data_array = Array
(
'Date'=> 2001-08-15
'Number' => 962883
'Time' => 17:40
'Etc1' => 0
'Etc2' => 0
);
$this->your_model->insert_data($data_array);
and inside your model write function like this
function insert_data($data=array())
{
$this->db->trans_start();
$this->db->insert('your_table_name',$data);
$this->db->trans_complete();
return TRUE;
}
i hope this will solve your problem
try this , this may help.
$this->db->set('Date', '2001-08-15', FALSE);
$this->db->set('Number', '962883', FALSE);
$this->db->set('Time', '17:40', FALSE);
$this->db->set('Etc1', '0', FALSE);
$this->db->set('Etc2', '0', FALSE);
this->db->insert('mytable');
I tried emulating your problem and was able to get the correct SQL statement genrated.
But I am pasting here the code that worked for me:
$data = array
(
'Date' => '2001-08-15',
'Number' => '962883',
'Time' => '17:40',
'Etc1' => '0',
'Etc2' => '0'
);
$this->db->insert('mytable', $data);
Let me know if this works - and if not, what the error message is.
Related
i am using the following code to get files from folder id. But I am not getting the files id instead empty array returning.
putenv('GOOGLE_APPLICATION_CREDENTIALS=C:\Users\new\Downloads\zonefunnel1.json');
$client = new Google_Client();
$client->addScope(Google_Service_Drive::DRIVE);
$client->useApplicationDefaultCredentials();
$service = new Google_Service_Drive($client);
$res=$service->files->listFiles();
//print_r($res);
$folderId="0B_bZZilaXPR4WkNvdFpMZmdneEU";
$optParams = array(
'pageSize' => 10,
'fields' => "nextPageToken, files(contentHints/thumbnail,fileExtension,iconLink,id,name,size,thumbnailLink,webContentLink,webViewLink,mimeType,parents)",
'q' => "'".$folderId."' in parents"
);
$results = $service->files->listFiles($optParams);
print_r($results);die();
I am getting the following,
Google_Service_Drive_FileList Object ( [collection_key:protected] => files [filesType:protected] => Google_Service_Drive_DriveFile [filesDataType:protected] => array [incompleteSearch] => [kind] => [nextPageToken] => [internal_gapi_mappings:protected] => Array ( ) [modelData:protected] => Array ( ) [processed:protected] => Array ( ) [files] => Array ( ) )
Please help me I have spent one week time for this.But not able to sort it out.
Regards,
Rekha
$files_list = $service->files->listFiles(array())->getFiles($optParams);
This is very strange: On Windows these functions with exactly the same databases works greats, but when I upload the project to Linux the pageinfo() function returns null. Of course the first thing I thought was that this has to do with case sensitivity, but all seems well formatted...
In my Page Model:
public function pageinfo() {
return $this->hasMany('App\Models\PageToElement', 'page_id');
}
(The Model PageToElement is exactly called like this)
In my controller:
$this->info = Page::find($page->page_id);
$meta = $this->info->pageinfo->where('element_id', 1)->first();
On Windows this nicely returns array:
App\Models\PageToElement Object (
[attributes:protected] => Array(
[id] => 1
[page_id] => 1
[element_id] => 1
[element_parent_id] => 0
[parent_id] => 0
[sorting] => 0
[is_active] => 1
[created_at] => 2015-12-07 10:00:03
[updated_at] => 2015-12-07 10:00:03
)
)
But on Linux, the response is null
What am I overlooking?
After some research, I came to the conclusion that $meta = $this->page->pageinfo->where('element_id', 1)->first(); returns null on the Linux server. But when I do $meta = $this->page->pageinfo; I do get more than one item in return. When I then do $meta = $this->page->pageinfo->first(); it returns one. So I think somehow the where('element_id', 1) is not working for Linux.
But what do you know, $meta = $this->page->pageinfo->where('element_id', '1')->first(); works! element_id is an integer, so why does it need quotes???
But what do you know, $meta = $this->page->pageinfo->where('element_id', '1')->first(); works! But then again not on Windows.
So I wounded up adding an extra line $id = 1; and parsing that in the query.
Strange workaround though
I have a situation while developing an application on Laravel. I have three arrays in the following order. I need to merge them and display their values in the following order too. Here , it goes.
stone_name['ruby','diamond','catseye'];
stone_weight[112,223,445];
stone_rate[1000,2500,670];
I need to merge these three arrays and display the out put in this order.
stone_info = array(stone_name[0] , stone_weight[0] , stone_rate[0]);
So that the final result will be like :
stone_info = array("ruby",112,1000);
I was in the same situation and I had done it in this way. May be this can help you out.
<?php
$stone_name = ['ruby','diamond','catseye'];
$stone_weight = [112,223,445];
$stone_rate = [1000,2500,670];
$result = mergeArrays($stone_name, $stone_weight, $stone_rate);
function mergeArrays($stone_name, $stone_weight, $stone_rate) {
$result = array();
foreach ($stone_name as $key => $name ) {
$result[] = array( 'stone_name' => $name, 'stone_weight' => $stone_weight[$key], 'stone_rate' => $stone_rate[ $key ] );
}
return $result;
}
print_r($result);
Output:
Array
(
[0] => Array
(
[stone_name] => ruby
[stone_weight] => 112
[stone_rate] => 1000
)
[1] => Array
(
[stone_name] => diamond
[stone_weight] => 223
[stone_rate] => 2500
)
[2] => Array
(
[stone_name] => catseye
[stone_weight] => 445
[stone_rate] => 670
)
)
DEMO
$updatedOrder = array(
'ship_status' => 'shipped',
'shipped_carrier' => (string)$selectedShipper->shipper->name,
'base_rate' => (float)$selectedShipper->rate,
'discount_rate' => (float)$selectedShipper->rate,
'tracking_number' => '123',
);
$this->orders_m->where('id', $tmpOrder->id)
->update('orders', $updatedOrder);
This yields the following SQL query: UPDATE default_orders SET ship_status = 'shipped', shipped_carrier = 'UPS Next Day Air', base_rate = 22.85, discount_rate = 22.85, tracking_number = '123' WHERE id = '1' AND id = 'orders'
Where did that last bit come from? id='orders'?
Just make sure that $tmpOrder->id is a variable and not an array.
var_dump($tmpOrder->id);
Maybe there is an error somewhere where you are getting the $tmpOrder and it returns an array for that.
I use Zend\InputFilter\InputFilter class for form validation. point field should accept only integers between 1 and 5. But it doesn't work properly it accepts string starting with integers between 1 and 5. For example 1kjhkjh, 2tgfjhgfjhf, 4jhkljg...
What is wrong in my code?
$inputFilter->add (
$inputFilter->getFactory()->createInput (
array (
'name' => 'point',
'required' => true,
'validators' => array (
array(
'name' => 'Digits'),
array (
'name' => 'Between',
'options' => array (
'min' => 1,
'max' => 5,
'messages' => array('notBetween' => 'Point must be between %min% and %max%')
)
)
)
)
)
);
Use the the second parameter of zend validator to break the validators chain and return an error, breakChainOnFailure (documentation) tells the element to stop validating if this error is triggered, so in your case if it is not a digit the user gets an error, when the user has fixed the error the second validator will get triggered too:
$inputFilter->add (
$inputFilter->getFactory()->createInput (
array (
'name' => 'point',
'required' => true,
'validators' => array (
array(
'name' => 'Digits', 'breakChainOnFailure' => true),
array (
'name' => 'Between',
'options' => array (
'min' => 1,
'max' => 5,
'messages' => array('notBetween' => 'Point must be between %min% and %max%')
)
)
)
)
)
);
Another option would be to replace Zend_Validate_Digits with Zend_Validate_Int (docmentation) depending of what error message you prefer to give to the user if he enters non valid data. Of course as suggested in the comments you could also use the Zend_Filter_Int if what you want is to handle any non valid data by fixing it yourself and do not give the user feedback about what he did wrong.
'breakChainOnFailure': true
should be
'breakChainOnFailure'=> true