inserting selected option from dropdown into db codeigniter - php

I have been working on a registration form which has a dropdown list populated from an associative array like this:
**<?php
**$options = array(
'cbaringo' => 'Baringo',
'cbomet' => 'Bomet',
'cbungoma' => 'Bungoma',
'cbusia' => 'Busia',
'celgeyo' => 'Elgeyo Marakwet',
'cmigori' => 'Migori',**
?>**
I want to insert the option the user selects into a database as follows
**$data = array(
'scounty' => $this->input->post('counties'),
'ssubcounty' => $this->input->post('subcounty'),
'sname' => $this->input->post('dschoolname'),
'skcpecode' => $this->input->post('dkcpecode'),
'stelno' => $this->input->post('dtelno'),
'steampatronname' => $this->input->post('dpatroname'),
'steampatronemail' => $this->input->post('dpatronemail'),
'steampatrontelno' => $this->input->post('dpatrontelno'),
's_password' => $this->input->post('scpassword')
);**
pupulated the dropdown like this:
**echo form_dropdown('counties', $options, 'cdefault');**
The above line displays the options on the dropdown list as expected
//inserted data into db
**$this->my_model->insert_data($data);**
however on inserting the key instead of value from the associative array is inserted into db. Whats the problem?

what you see in a select list is different then the value that is passed from it
$options =
// the value // what the user sees in the drop down
array(
'cbaringo' => 'Baringo',
'cbomet' => 'Bomet',
'cbungoma' => 'Bungoma',
'cbusia' => 'Busia',
'celgeyo' => 'Elgeyo Marakwet',
'cmigori' => 'Migori',**
?>
so if they choose Baringo, the value passed should be cbaringo

Related

get_field is not returning value in advanced-custom-fields-pro

I using advanced-custom-fields-pro in wordpress to allow wordpress user to type page id they wish to display in web, is there any way to get the data from wordpress user and in backend using wp_query and display the page with the data user entered in advanced-custom-fields-pro?
Please take note, i named the field as “feature” in plugin. For my case, ‘feature’ can be contain multiple value like 1,2,3,4 so i would like to get my ‘feature’ value in array, what can i do for this?
Currently, get_field didnt get value sucessfully.
$feature = get_field(‘feature’);
$the_query_featured = new WP_Query( array(
‘posts_per_page’ => 2,
‘posts_per_page’ => 2,
‘nopaging’ => false,
‘order’ => ‘DESC’, //’order’ => ‘ASC’,
‘orderby’ => ‘date’,
‘meta-key’ => $feature,
‘page_id’ => $feature,
‘post_type’ => ‘any’
));
<?php while ( $the_query_featured->have_posts() ) : $the_query_featured->the_post(); ?>

Auto populate dropDownListRow form field in yii from database

I have a form having some multi select dropdowns in yii. I have to auto populate the selected values in that multi select box when comes to edit the data. I am fetching the data and passing it inot view file, but am not sure how can i populate the values in the dropdown. please help me
Here is the code for the multi select drop down
echo $form->dropDownListRow($modelDomain, 'domain_id', $domain, array('title' => Yii::t('main', 'SELECT_DOAMIN'),'multiple'=>true ,'style' => 'width:250px;height:150px;'));
For multiple select you need to use 'options' parameter:
echo $form->dropDownListRow($modelDomain, 'domain_id', $domain, array('title' => Yii::t('main', 'SELECT_DOAMIN'),'multiple'=>true ,'style' => 'width:250px;height:150px;', 'options' => array(
1 => array('selected' => 'selected'),
2 => array('selected' => 'selected'),
)));
Where 1 and 2 are domain ids taken from the $_POST;
You can do this in the action:
$post = $this->getRequest()->getPost('ModelName');
$selectedDomains = $post['domain_id']; // this should be an array or selected values
$selected = array_fill(0, count($selectedDomains), array('selected' => 'selected')); // this constructs the 'options' value from view
$selectedOptions = array_combine($selectedDomains, $selected) // this is the 'options' array with selected values as keys and htmlOptions as values
Also, the code is untested and you need to do your validations and other logic stuff yourself.

Recursive PHP PDO POST INSERT

I have part of my PHP file below. I'm using PDO for communication with a database and I'm receiving the data from a HTML Form. Both try/catches are laid out in the same format, however only the first try/catch will INSERT the information correctly. The second try/catch is not inserting any information, and it's not catching any errors. Echos display just fine, but again no data in NAMES table.
If anyone can point out my mistake I'd greatly appreciate it.
$thenumdrivers = $_POST['reg_drivers_num'];
//Query for INSERT drivers into DRIVERS table
try{
echo "Number of drivers being registered: $thenumdrivers!<br>";
$STH=$DBH->prepare("INSERT INTO `Drivers` (`Account_id`, `Driver_license`, `Driver_name`, `Driver_birthdate`)
VALUES (:reg_accountid,:drivers_license,:drivers_firstname,:drivers_dob)");
for($i=1;$i<=$thenumdrivers;$i++){
$STH->execute(array(':reg_accountid'=>$theaccountid,':drivers_license'=>$_POST['drivers_license'.$i],':drivers_firstname'=>$_POST['drivers_firstname'.$i],':drivers_dob'=>$_POST['drivers_dob'.$i]));
echo "1 Driver added!<br>";
}
}
catch(PDOException $e){
$e->getMessage();
}
//Query to INSERT driver names into NAMES table
try{
echo "Number of drivers being added to Names: $thenumdrivers!<br>";
$STH=$DBH->prepare("INSERT INTO `Names` (`Account_id`, `Name_first`, `Name_middle`, `Name_last`) VALUES (:reg_accountid,:drivers_firstname,:drivers_middleinit,:drivers_lastname)");
for($k=1;$k<=$thenumdrivers;$k++){
$STH->execute(array(':reg_accountid'=>$theaccountid,':drivers_firstname'=>$_POST['drivers_firstname'.$k],':drivers_middleinit'=>$_POST['drivers_middleinit'.$k],':drivers_lastname'=>$_POST['drivers_lastname'.$k]));
echo "1 Name added!<br>";
}
}
catch(PDOException $e){
$e->getMessage();
}
print_r($_POST) output for just this section of data. I only tried to register 1 driver. So the blanks for the others are correct.
Array ( [reg_vehicles_num] => 1 [reg_make1] => make [reg_model1] => model [reg_year1] => 2014 [reg_vin1] => 3 [reg_make2] => [reg_model2] => [reg_year2] => [reg_vin2] => [reg_make3] => [reg_model3] => [reg_year3] => [reg_vin3] => [reg_make4] => [reg_model4] => [reg_year4] => [reg_vin4] => [reg_drivers_num] => 1 [drivers_lastname1] => Tester [drivers_middleinit1] => J [drivers_firstname1] => Fester [drivers_dob1] => 2014-04-01 [drivers_license1] => 0987654321 [drivers_lastname2] => [drivers_middleinit2] => [drivers_firstname2] => [drivers_dob2] => [drivers_license2] => [drivers_lastname3] => [drivers_middleinit3] => [drivers_firstname3] => [drivers_dob3] => [drivers_license3] => [drivers_lastname4] => [drivers_middleinit4] => [drivers_firstname4] => [drivers_dob4] => [drivers_license4] => )
Ok the only thing I can see is that in your sending $_POST['reg_drivers_num'] but tying to access $_POST['reg_number_drivers'], enable error_reporting(E_ALL).
You might also want to look into structuring your POST input values better and send an array of values, this way you can use a foreach to loop the values and not need the reg_number_drivers value.

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') );

PHPcassa insert randomly

$column_family->insert('row_key1', array('col_name1' => 'col_val1'));
$column_family->insert('row_key2', array('col_name2' => 'col_val2'));
$column_family->insert('row_key3', array('col_name3' => 'col_val3'));
$column_family->insert('row_key4', array('col_name4' => 'col_val4'));
The problem is mystery when the insert function does not insert value in sequence, instead it would appear in jumpping order: col_val2,col_val3,col_val1,col_val4
It bad when I could do a properly get_range() value as some have appear to insert into other partition.
How do I make the code insert orderly?
Have you tried using the batch_insert functionality?
$column_family->batch_insert(array(
'row_key1' => array('col_name1' => 'col_val1'),
'row_key2' => array('col_name2' => 'col_val2'),
'row_key3' => array('col_name3' => 'col_val3'),
'row_key4' => array('col_name4' => 'col_val4')
));

Categories