cakephp211 date insertion - cakephp-2.1

this is my variable:
<?php
//...
$creat='2000-03-15' ,from an input field.
?>
this is my insertion code:
<?php
//...
$this->Comment->updateAll(
array('Comment.c' => $creat ),
array('Comment.id' => '3' ));
?>
when i want to save it in a table which have a date field type, instead of '2000-03-15', i have '0000-00-00'?
why?
thanks for comments!

the updateAll function does not escape your input. It will also not wrap your data in quotes.
In your example, to properly escape your $creat variable, i would suggest using the following code:
<?php
App::uses('Sanitize', 'Utility');
$Comment->updateAll(array(
'Comment.c' => "'" . Sanitize::escape($creat) . "'"
), array(
'Comment.id' => 3
));
We wrap the date with quotes and also pass it through Sanitize::escape to make sure we protect ourselves against SQL injection.

Related

Why does adding a class to a p tag in the json of a seeder break that seeder?

I have a seeder that, in one part of it seeds a json column as shown below. If I have the text in a p tag everything seeds just fine.
This works:
DB::table('table_name')->insert(array(
array(
'json' => '<p>some text</p>'
)
))
However if I add a class or a style tag to the text, it breaks the seeder! why? How? How do I get around this?
DB::table('table_name')->insert(array(
array(
'json' => '<p class="subtitle">some text</p>'
)
))
The error that I get is <<Unknown error>>: 3140 Invalid JSON text: "Invalid value." at position 27806 in value for column
Try it like this:
DB::table('table_name')->insert(array(
array(
'json' => json_encode('<p class="subtitle">some text</p>')
)
))

Active records on codeigniter page

I want print a table from database on view page with where condition, means I want print table of student where class is seven and fee status is paid.
How we do this?
i tried it but not work:
<?php
$students = $this->db->get_where('student' , array('status' => paid,'class_id'=>7))->result_array();
foreach($students as $row):?>
I have used this code to find the answer for you Please see it and try it out
$query = $this->db->get_where('tasks', array('description' => 7,'status' => 1));
echo "<pre>";
print_r($query->result_array());
change your array element as this since paid is a string it must be in quots
array('description' => "paid",'class_id'=>7)

Joomla database query SELECT AS

So I'm just hypothetically thrilled to be querying my hypothetical database:
$query->select($db->quoteName(array('user_id', 'name')));
I would, however, like the query to look like:
SELECT `user_id` AS `uid`, `name` AS `User-Name`
How the heck do I get the AS in there?
I know this question is 6 months old, so you've probably found an answer or worked around it, but for anyone else who has a similar problem:
$query->select($db->quoteName(array('user_id','name'),array('uid','User-Name')));
If you only want to use an alias for some fields, just pass null in the array for the fields you don't want to alias, so for example:
$query->select($db->quoteName(array('user_id','name'),array(null,'User-Name')));
would give
"SELECT `user_id`, `name` AS `User-Name`"
My preferred way is this:
I create an array with the fields I want to select:
$fields = array(
'a.id' => 'id',
'a.field1' => 'field1',
'a.field2' => 'field2',
'a.field3' => 'field3',
'b.field1' => 'bfield1',
'b.field2' => null,
'b.field3' => 'bfield3',
);
In the above array, the keys are used for the db Column names of the query, the values for the aliases, as you can see later in the $query->select().
*When you do not need an alias - just set null.
This helps better to control and check what fields I want and how to name them - and is better for maintenance or changes and is portable enough, as I only have to change my $fields array according to my needs.
Then the Joomla select command can be like:
$query->select( $db->quoteName(
array_keys($fields),
array_values($fields)
));
This will produce the following SQL SELECT query:
SELECT `a`.`id` AS `id`,`a`.`field1` AS `field1`,`a`.`field2` AS
`field2`,`a`.`field3` AS `field3`,`b`.`field1` AS
`bfield1`, `field2`, `b`.`field3` AS `bfield3`
Try the following:
$query->select($db->quoteName('user_id') .' AS '. $db->quoteName('name') .' AS '. $db->quoteName('User-Name'));
Be sure to use the full query as described here:
http://docs.joomla.org/Selecting_data_using_JDatabase
So yours will look something like this:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select($db->quoteName('user_id') .' AS '. $db->quoteName('name') .' AS '. $db->quoteName('User-Name'));
$query->from($db->quoteName('#__tablename'));
$db->setQuery($query);
$results = $db->loadObjectList();
Please note that I havent tested this query using AS so let me know if it works or not
I've got this to work:
->select($db->quoteName(array('user_id`' . ' AS ' . '`uid', 'name`' . ' AS ' . '`User-Name')))
It's better to use what Joomla! suggest us.
Note by putting 'a' as a second parameter will generate #__content AS a
So, you may have something like this:
$query
->select('*')
->from($db->quoteName('#__content', 'a'))

How to add array to $data array CodeIgniter

I have some $data that i am transfering to view, ok that is easy
But i want this
$data['promenjive']=array
(
'1' => prva,
'2' => 'druga',
'3' => 'treca',
);
And
$this->load->view("view_home", $data);
My question is how to echo a single value from $data['promenjive']
To make something like this
1
I dont want to foreach entire array just one element?
In your example prva would echoed from within your view template with:
<?php echo $promenjive[1];?>
To echo 1 from your example you would have to do:
<?php echo array_search('druga',$promenjive);?>

How to use truncate of text helper inside html(tag) helper in view of cakephp?

echo $this->Html->tag('span',$user['User']['user_title'], array('class' => "nameOfUser");
###
in this tag i want to truncate user_title to 20 characters .
SO how i can used truncate in HTML tag ?
Use the TextHelper; http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#texthelper
echo $this->Html->tag('span',
$this->Text->truncate($user['User']['user_title'], 20),
array('class' => "nameOfUser")
);

Resources