0
votes

hi im having little trouble at inserting date from drupal to mysql here the code that i'm trying

.....
$form['kotak']['tgl'] = array(
'#type' => 'date',
'#title' => t('Tanggal'),
);
.....
function awal_form_submit($form,&$form_state){
global $user;
$entry = array(
 'tanggal'  => $form_state['values']['tgl'],
);
$tabel = 'jp_1';
$return = insert_form($entry,$tabel);
}
.....
function insert_form($entry,$tabel){
$return_value = NULL;
try {
 $return_value = db_insert($tabel)
                ->fields($entry)
                ->execute();    
}
.....

everytime i'm submit, error code like this

db_insert failed. Message = SQLSTATE[21S01]: Insert value list does not match column list: 1136 Column count doesn't match value count at row 1, query= INSERT INTO {jp_1} (tanggal) VALUES (:db_insert_placeholder_0_month, :db_insert_placeholder_0_day, :db_insert_placeholder_0_year)

any suggestion or correction?

2
Frankly speaking I have never used drupal before, I am making my call based on your mysql error. The values of month/day/year should be concatenated into one. Right now they are passed as three separate variables, they have to be combined to make one single variable.Jermin Bazazian

2 Answers

0
votes

From the mysql error it looks like the table you created has required fields (a columns Null property is set to 0, which means that there must be a value for tha column for every row you want to insert)

Check whether there are any columns which have null set to 0.

From your example I can't see what you're trying to achieve, but in many cases it's not necessary to write into db tables manually (using db_insert()) as you can get the same result easier by creating a content type (node type) which handles a lot of functionality for you. I hope that helps, Martin

0
votes

i'm finally managed to find the answer, all i need is download "Date" module and activate its "Date API". Here the code

.....
$datex = '2005-1-1';
$format = 'Y-m-d';
$form['kotak']['tgl'] = array(
'#type' => 'date_select',
'#default_value' => $datex,
'#date_format' => $format,
'#date_year_range' => '-10:+30', 
'#title' => t('Tanggal'),
);
.....
function awal_form_submit($form,&$form_state){
global $user;
$entry = array(
 'tanggal'  => $form_state['values']['tgl'],
);
$tabel = 'jp_1';
$return = insert_form($entry,$tabel);
}
.....
function insert_form($entry,$tabel){
$return_value = NULL;
try {
 $return_value = db_insert($tabel)
                ->fields($entry)
                ->execute();    
}
.....

and now i have no problem delivering to mysql. Hope that will help other drupal newbie developer like me. Thanks :D