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.
Related
I have three tables defined where always
column 3 is "query", varchar(255) null
column 4 is "tag", varchar(255) null
both columns are the same charset
I submit string $s to column 3 via $wpdb->insert which should do sanitization (but adding extra sanitization to col 3 and col 4 or not adding it does not change anything at all)
function abc($a=null,$tag=null) {
global $wpdb;
$data = array(
'timestamp' => time(),
'query' => sanitize_text_field($a),
'tag' => sanitize_text_field($tag)
);
$format = array('%s','%d');
$wpdb->insert(SS_STATS,$data,$format);
return $wpdb->insert_id;
}
in the template I tried:
abc($s,$s);
abc("$s","$s");
abc("plz save this","$s");
abc("plz save this","plz save this: $s");
In each and every case, column 3 in the db recods 0. In each and every case, column 4 records the correct value just as it is submitted.
Why?
I tried:
changing the name of the column (maybe query is protected)
adding extra sanitization
not adding any sanitization
changing data type to text
changing data type to blob
For every attempt I drop the db and let it recreate.
No change in any case.
How can I save the string in column 3?
The problem was in the format:
$format = array('%s','%d');
$wpdb->insert(SS_STATS,$data,$format);
Removing the formatting solved the issue.
$wpdb->insert(SS_STATS,$data);
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!
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
I have one doubt in data provider,
I have more number of fields in the db.If i retrieve the data in specific field,it fetch null values as well as exist values are shown here.I want only the exist values .Without null values i want to load the vales How to do that...Anyone know please help me.
For example C grid view data provider 5 rows have null values in db and another 5 rows have values i want load without null values..please help me.. :(
public function actionDataProvider {
$uid=Yii::app()->user->id;
if(isset($_GET['user_id']))
$uid=$_GET['user_id'];
$criteria = new CDbCriteria();
$criteria->condition=("user_id= $uid ");
$dataProvider=new CActiveDataProvider('UserGallery',array('criteria'=>$criteria,'pagination'=>array('pageSize'=>20)));
$this->render('product_pic',array('model' => userGallery::model()->findAll($criteria),'dataProvider'=>$dataProvider,));
}
Append to your other $criteria's:
To show NULL values,
$criteria->addInCondition( 'columnWithNull', array( null ) );
To hide NULL values:
$criteria->addNotInCondition( 'columnWithNull', array( null ) );
I found this in Yii documentation (in really reading CDBCriteria source code).
I have a page for browsing db records. The viewer can filter records by category, author, and tags. I'm using a form instead of url segments for filtering records (it feels more secure because I can validate inputs.)
For instance, when all form inputs are populated the query looks like this:
SELECT * FROM (`posts`) WHERE `category` = 'technolgy' AND `author` = 'lila' AND `tags` = 'ebook'
However if one input or more is empty, I get no results. For example:
SELECT * FROM (`posts`) WHERE `category` = '' AND `author` = 'lila' AND `tags` = ''
I want the inputs to be optional so for example if just author name is entered, I can return records made by that author regardless of category and tags. How can I omit the and where clause if empty?
Note: or_where clause is not the solution because it doesn't return a precise query if all filter inputs all filled.
My Model
function filter($form_values)
{
$query = $this->db->get('posts');
$this->db->where($form_values); //adds clause to all array items
return $query->result();
}
The function parameter is an array with input values from my view. Example,
$form_values = array('category' => $category, 'author' => $author, 'tags' => $tags);
and my View
$form_values = array (
'category' => $this->input->post('category'),
'author' => $this->input->post('author'),
'tags' => $this->input->post('tags')
);
$this->Records_model->filter($form_values);
I know that in Codeigniter if $_POST' are empty they are set to FALSE. Can that be used to achieve what I'm trying? I'm not sure if I'm on the right track
You are correct that $this->input->post() will return FALSE if the $_POST value is not set. Unless you specifically want IS NULL to be part of the query (which I believe will happen by passing FALSE to param 2 of where(), not 100% sure), just filter out the empty values:
function filter($form_values)
{
$form_values = array_filter($form_values);
// NOTE:
// where() needs to be called first, or your query won't use the WHERE clause
// You may need to make sure there is at least one value in $form_values
if ( ! empty($form_values)) // however you wish to check for a value
{
$this->db->where($form_values); //adds clause to all array items
}
$query = $this->db->get('posts');
return $query->result();
}
http://php.net/manual/en/function.array-filter.php
The important part to note on array_filter():
If no callback is supplied, all entries of input equal to FALSE (see converting to boolean) will be removed.