inserting data into postgresql database with codeigniter - php

i have a problem when i try to insert a row in my user table in postgresql, e.g.
I have the normal insert with active record, $this->db->insert($table,$data) and i get an error something like this:
ERROR: it does not exists the column «user_accounts» in the relation «user_accounts» LINE 1: INSERT INTO "user_accounts" ("user_accounts"."uacc_group_fk"
and i think the problem could be the table name inside the fields that i will insert ("user_accounts"."uacc_group_fk"),.
Do you know how can I avoid this? i just want to put the name of fields without table name.
Thanks in advance! =)

Have you checked that the column "user_accounts"."uacc_group_fk" exists ( with quotes )?
this is the data array used in insert_user fuction of https://github.com/haseydesign/flexi-auth/blob/master/library_files/application/models/flexi_auth_model.php:
$sql_insert = array(
$this->auth->tbl_col_user_account['group_id'] => $group_id,
$this->auth->tbl_col_user_account['email'] => $email,
$this->auth->tbl_col_user_account['username'] => ($username) ? $username : '',
$this->auth->tbl_col_user_account['password'] => $hash_password,
$this->auth->tbl_col_user_account['ip_address'] => $ip_address,
$this->auth->tbl_col_user_account['last_login_date'] => $this->database_date_time(),
$this->auth->tbl_col_user_account['date_added'] => $this->database_date_time(),
$this->auth->tbl_col_user_account['activation_token'] => $activation_token,
$this->auth->tbl_col_user_account['active'] => 0,
$this->auth->tbl_col_user_account['suspend'] => $suspend_account
);

Related

how can i retrieve id using this method? - codeignitor db

The given syntax can insert the given values but i cannot retrieve the value with method. do anyone knoe how can i do that?
$supplier = array(
'cname' => $request->getPost('cname'),
'clname' => $request->getPost('clname'),
'tlidt' => $request->getPost('tlidt'),
'tledt' => $request->getPost('tledt'),
'pname' => $request->getPost('pname'),
'tla' => $request->getPost('tla'),
);
$builder = $db->table('suppliers');
$builder->insert($supplier);
$id = $this->$db->insert_id();
Try inserting like that
$this->db->insert('suppliers',$supplier);
$id = $this->db->insert_id();
or try removing $ sign from your last line 'db'.

wpdb insert with no ID

I'm new in WordPress
I'm trying to insert on database without knowing the id, here what I'm trying to do:
$create = $wpdb->insert('wp_ito_plan', array('name' => $_POST['name'], 'tickets' => $_POST['tickets'], 'price' => $_POST['price'], 'visits' => $_POST['visits']));
and I got this error:
WordPress database error: [Duplicate entry '0' for key 'PRIMARY']
INSERT INTO `wp_ito_plan` (`name`,`tickets`,`price`,`visits`) VALUES ('asd','123','123','123')
I tried adding to the array: 'id' => $wpdb->insert_id, but stills the same.
How can I fix this? Do I need to check what is the last ID on database and then increment? There's no easiest way?
It appears as though you aren't auto incrementing the ID column so you're insert is going to overwrite an existing row. Set the ID column to auto increment and it should work fine.

Querying for keywords in photos using DynamoDB and PHP

Requirement: I'm storing photos in S3 and want the user to be able to lookup all photos that contain a keyword they type in on a webpage.
My solution: I have created 2 tables:
tblPhotos:
Primary Key=HashKey: PhotoID
Fields:
PhotoID,
S3Location
tblKeywords:
Primary Key=HashKey (HashID) and RangeKey (Keyword)
Fields:
HashID (always set to 1),
Keyword (one big string; the keyword followed by PhotoID)
S3Location
I'm using the following php code to find all records that have my keyword in it:
$table_name = 'tblKeywords';
$keywordresponse = $dynamodb->query(array(
'TableName' => $table_name,
'HashKeyValue' => array(AmazonDynamoDB::TYPE_STRING => '1'),
'RangeKeyCondition' => array(
'ComparisonOperator' => AmazonDynamoDB::CONDITION_BEGINS_WITH,
'AttributeValueList' => array(
array(AmazonDynamoDB::TYPE_STRING => $_GET['sSearch'])
),
)
));
if ($keywordresponse->isOK())
{
foreach ($keywordresponse->body->Items as $item)
{
$aaData[] = array(
'Keyword' => (string) $item->Keyword->{AmazonDynamoDB::TYPE_STRING},
'S3Location' => (string) $item->S3Location->{AmazonDynamoDB::TYPE_STRING}
);
}
echo json_encode(array('keywordstatus' => 1, 'aaData' => $aaData));
}
else
{
echo json_encode(array('keywordstatus' => 0));
}
As you can see I'm passing sSearch as the keyword to filter results on. My question is, do you know a better way to achieve this filtering of records using DynamoDB query? I'm trying to avoid a scan as its very inefficient, although my solution above whilst it works doesn't seem particularly elegant!
If your primary goal is to be able to return all the photos for a given key word I think the following schema would make more sense:
HashKey = Keyword
RangeKey = PhotoID
Additional possible fields: S3Name (if it is not the same as PhotoID)

Cannot insert email to wordpress database

I am trying to insert a email into the database. All other values except email gets posted. '0' is being inserted into the database instead of email id.
$wpdb->bloodregistration = $wpdb->prefix.'bloodregistration';
$wpdb->insert( $wpdb->bloodregistration,array( 'name' => $_POST['myname'], 'myemail' => $mailid,'age' => $_POST['age'],'gender' => $_POST['gender'],'location' => $_POST['location']
,'address' => $_POST['address'],'phone_land' => $_POST['ph_land'],'phone_mob' => $_POST['ph_mobile'],'blood_group' => $_POST['blood_group']
,'dob' => $dob,'lastblooddate' => $doblood),array('%s','%d') );
What DB do you use? My guess is that you forgot to assign sequence/serial/auto-increment to ID column.
EDIT:
So the problem was this part 'myemail' => $mailid you were inserting empty or not set variable into this field and therefore you got 0 in DB.
In DB set the column "myemail" to auto-increment, NOT NULL and INDEX PRIMARY.
Then in PHP simply skip this field when adding new records, DB will auto-increment it.
Try:
$wpdb->bloodregistration = $wpdb->prefix.'bloodregistration';
$wpdb->insert( $wpdb->bloodregistration,array( 'name' => $_POST['myname'],'age' => $_POST['age'],'gender' => $_POST['gender'],'location' => $_POST['location']
,'address' => $_POST['address'],'phone_land' => $_POST['ph_land'],'phone_mob' => $_POST['ph_mobile'],'blood_group' => $_POST['blood_group']
,'dob' => $dob,'lastblooddate' => $doblood),array('%s','%d') );

how to use insert_batch with unique ids

I am using codeignitor's insert batch function to insert multiple rows to a table.
$this->db->insert_batch('table', $sizes);
my $sizes array looks like this
$sizes = array(
array(
'size' => 'M' ,
'product' => 'Hat'
),
array(
'size' => 'L' ,
'product' => 'Hat'
)
);
I was intending for separate rows to be added to my db table with these values along with unique ids, however when each nested array is added, It adds 0 to the id field, rather than a unique ID
I am receiving the error "Duplicate entry '0' for key 'id'"
What is the best approach to solve this? Thanks for reading!
Make your UNIQUE Column with AUTO_INCREMENT

Categories