im use code php in method get to post in column 'tr_no_hp'
like
$nomorhp = $this->input->get('nomor_hp');
but after i try to insert in database column cant be null.
how to fix this problems
this my controllers code
$nomorhp = $this->input->get('nomor_hp');
$data = array(
'us_id' => $this->user->us_id,
'sv_id' => $voucher->sv_id,
'op_id' => $voucher->op_id,
'tr_id_plgn' => $nomor,
'tr_no_hp' => $nomorhp,
);
$this->db->insert('transaksi', $data);
$trx_id =$this->db->insert_id();
and i got this eror code
Query error: Column 'tr_no_hp' cannot be null - Invalid query: INSERT INTO `transaksi`
thanks
Assign default value of 'tr_no_hp' column to NULL in phpmyadmin
You can either modify you tables to allow null (but this might break other things) or set a default value.
For example:
$nomorhp = $this->input->get('nomor_hp') ?: ''; // Default to empty string
Related
I've been working on getting the data from mysql database into blade view. So, I've a field "total_sold" in my table. Below is my code
public function getDealVersions($dealId)
{
if ($dealId == "undefined") {
return array('isVersions' => FALSE);
}
$deal = Deal::find($dealId);
Log::info($deal);
$return = array(
'deal' => $deal,
'isVersions' => count($deal->versions) > 1 ? TRUE : FALSE,
'versions' => $deal->versions
);
return $return;
}
In the above code, $deal has the results from the mysql query and the value of total_sold field is 0 which is the default value but the actual value in the database is 2352. Values for all the other fields are returning the expected values except for this field "total_sold". In mysql, for the total_sold field,
type=int(11), Default Value = 0 and Nullable=YES
So, I'm not able to understand if I should change something with my query or should I change some property in the mysql. Any help or advise is greatly appreciated.
Thank You!
I am using the wordpress replace query to update/add the records But i am facing an error . It updates the records successfully But the values not in array becomes null .Here is my code
function ux_add_data($table,$postdata){
global $wpdb;
$tablename=$wpdb->prefix.$table;
$updated_data=$postdata['BX_data'];
if(isset($updated_data['isActive']) && $updated_data['isActive']==1)
$updated_data['isActive']=1;
else
$updated_data['isActive']=0;
$data=$wpdb->replace($tablename,$updated_data,array('%s') );
if($data)
return UX_flash('success','Data has been added successfully.');
else
return UX_flash('danger','Some errror to save your data.');
}
if i have columns in database recordid,ID,name,class,created,modified and in array i have only recordid,name,modified then ID,class,modified becomes null .
Please tell me where i am doing wrong.
Thanks
You're using $wpdb->replace function, please notice that
wpdb::replace( string $table, array $data, array|string $format = null )
$data
(array) (Required) Data to insert (in column => value pairs). Both $data columns and $data values should be "raw" (neither should be SQL escaped). Sending a null value will cause the column to be set to NULL - the corresponding format is ignored in this case.
So, while you're setting some columns, some others will set null.
find more at here.
I am having an issue with updating a MySql table using codeigniter.
Basically, its inserting 'img' the characters, into the table rather than the value of the variable.
This is so strange!
Here is my model:
public function update_course_progress($progress_data) {
$course_id = $progress_data['course_id'];
$user_id = $progress_data['user_id'];
$progress = $progress_data['progress'];
$status = $progress_data['status'];
$update_data = array (
'progress' => $progress,
'status' => $status,
);
// perform update on the matching row
$this->db->update('training_stats', $update_data, array('course_id' => $course_id, 'user_id' => $user_id));
}
So, the issue is with 'progress' instead of inserting the value of this variable it is inserting 'img'???
So, if i var_dump $update_data i get this:
array(2) {
["progress"]=> string(2) "1a"
["status"]=> string(1) "i"
}
Which is correct:
And if i use the profiler in CI to get the db queries, this is what I get:
UPDATE `training_stats`
SET `progress` = '1a', `status` = 'i'
WHERE `course_id` = '8'
AND `user_id` = '2'
Which is correct.
So WHY ON EARTH is it inserting null into the db instead of 1a.
The table structure for this column is VARCHAR(4).
progress varchar(4) NOT NULL DEFAULT '0',
What the hell is going on? why on earth is it img input???
What can be wrong?
UPDATE:
As i was debugging, i tried an insert instead of an update, and 2 rows were inserted. The first row was the expected data, and the second row was the data with 'img' in it. Both rows were the same except for the 'progress' column, which had img inserted in the second row.
So obviously it had been updating the row with the correct data and then overwriting it with the incorrect data.
But now why are there 2 rows being inserted? There is no loop? and why is the CI profiler not logging the second query, if that is indeed what is happening
As of per documentation of CI I will use the more standard method of updating data.
$updateArray = array (
'progress' => $progress_data['progress'],
'status' => $progress_data['status'],
);
$whereArray = array(
'course_id' => $progress_data['course_id'],
'user_id' => $progress_data['user_id']
)
$this->db->set($updateArray);
$this->db->where($whereArray);
$this->db->update('training_stats');
This should do, I also think you shouldn't put extra variables for the data as you did. With such short function you really are not having any benefits sinds all data is only accessed once and seem like unnecessary to me, though opinions could vary.
I am using Cakephp 3 and trying to get some rows/records from a table having a field as non empty.
For example:-
I have a areas table with a field named Area_block. I need to fetch records for those rows which have something inside Area_block. In simple words, I don't need empty or NULL Area_block rows.
Area_block is not a NULL field by default. So it remains empty/blank.
I have tried
$conditions = ['Area_Block IS NOT NULL'];
$conditions = ['NOT' => array('Area_Block' => '')];
$conditions = ['Area_Block <>' => ''];
$conditions = ['Area_Block !=' => ''];
but nothing works !!
My environment includes MSSQL server 2008 and PHP 5.6 on apache.
Please help !!
I understand your table holds records with Area_Block set both to NULL and '' (empty string).
Try the following:
$conditions = [['Area_Block IS NOT' => null], ['Area_Block IS NOT' => '']];
This should also work:
$conditions = ['Area_Block IS NOT' => [null,'']]; //haven't tested it
However, in order to reduce complexity, in the database you may want to set the Area_Block field either to default to NULL, or default to '' and prevent the field from accepting NULL values.
Working exclusively with NOT NULL values might be easier. If you alter the table to reflect this, you would only need to check for '' values:
$conditions = ['Area_Block IS NOT' => ''];
See Automatic IS NOT NULL Creation in the 3.x Cookbook.
I think the solution provided by #InigoFlores worked to some extend but was failing for some records.
Here's what I did to make it work perfectly.
$conditions = ['DATALENGTH(Area_Block) >' => 0];
I Hope it helps someone.
I have an images table with a column called type. I simply want to update all the rows to change the type to gallery where the user_id matches a particular user.
I am using this code
$this->Image->updateAll(array('Image.type' => 'gallery'),
array('Image.user_id' => $this->Auth->user('id')));
But I get this error: SQL Error: 1054: Unknown column 'gallery' in 'field list'
Why is gallery being added to the field list ?
Isn't the syntax supposed to set type to gallery?
Thanks!
Found this on the manual:
The $fields array accepts SQL expressions. Literal values should be quoted manually.
Thus, the following should work:
$this->Image->updateAll(
array('Image.type' => "'gallery'"),
array('Image.user_id' => $this->Auth->user('id'))
);
In your model do something like this in your method ....
public function saveImage($type='')
{
// I would add a test for $type
$db = $this->getDataSource();
$fields = array('type' => $db->value($type, 'string')); // $db->value() will format strings needed for updateAll()
$condition = array('user_id' => $this->Auth->user('id'));
// I would add a test for user id before running updateAll()
$this->updateAll($fields, $conditions);
}