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') );
Related
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
I created one table with decimal data types in mysql database. when i want to store some data it stores as 0 only. I tried by changing data types but no result. i am including the portion of php code
$que_sgpdetails = "INSERT INTO sgpa_details ( usn, sem_I, sem_II, sem_III, sem_IV, sem_V, sem_VI, sem_VII, sem_VIII ) VALUES ( :usn, :sem_I, :sem_II, :sem_III, :sem_IV, :sem_V, :sem_VI, :sem_VII, :sem_VIII )";
$query_params5 = array( ':usn' => $_POST['usn'], ':sem_I' => $_POST['sem_I'], ':sem_II' => $_POST['sem_II'], ':sem_III' => $_POST['sem_III'], ':sem_IV' => $_POST['sem_IV'], ':sem_V' => $_POST['sem_V'], ':sem_VI' => $_POST['sem_VI'], ':sem_VII' => $_POST['sem_VII'], ':sem_VIII' => $_POST['sem_VIII'] );
$statement5 = $db->prepare($que_sgpdetails);
$result5 = $statement5->execute($query_params5);
I'm working on a rating system. When a user rates, $inc increments the field, and addToSet adds the user_id to make sure the user only clicks rate once. I am checking if the user_id is already in the x field before updating, but that is another query which I'd rather avoid. Can I reach this purpose without having to write another query? I mean, $addToSet only adds if there is no value like that; can I instead get affected rows? Can you suggest other queries?
Thank you!
..->update(
array("_id" => $idob),
array(
'$inc' => array($type => (int) 1),
'$addToSet' => array("x" => (int) $user_id)
)
);
Ok I see the problem.
..->update(
array("_id" => $idob),
array(
'$inc' => array($type => (int) 1),
'$addToSet' => array("x" => (int) $user_id)
)
);
The problem is that you need a conditional $inc there so that it only $incs if it does add to set.
This is not possible with a unique index since unique indexes work from the root of the document atm. Also you probably want to use the $inc as a form of pre-aggregation or what not.
One method could be:
update(
array('_id' => $idob, 'x' => array('$nin' => array($user_id))),
array(
'$inc' => array($type => 1),
'$push' => array('x' => (int)$user_id)
)
)
This will only do the update if that user_id does not already exist in x.
$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')
));
I have data from a form submission stored in a variable called $post_data. When I do print_r($post_data); I get the following array:
Array
(
[element_3] => John Doe
[element_2] => john#example.com
[element_14] => City
[element_15] => Country
[form_id] => 1
[submit] => Submit
);
I want to store some of the fields in another array to pass to another script. Will my code below work? If not, how do I fix it?
$submitted_data = array(
'Fields' => array(
array(
'Key' => 'Name',
'Value' => $post_data['element_3']
)
array(
'Key' => 'Email',
'Value' => $post_data['element_2']
)
)
)
Also, a PHP noob question, do I need another comma (,) in between the Name and Email array?
Thanks!
I'm not exactly sure why you would want to do this, but depending on the field name you can consider using loops to help automate the entire process.
$field_map = array(
'element_3' => 'Name',
'element_2' => 'E-mail',
'element_14' => 'City',
'element_15' => 'Country'
);
$submitted_data = array('fields' => array());
foreach ( $field_map as $key => $label)
{
$submitted_data['fields'][] = array(
'key' => $key, // e.g. element_2
'label' => $label, // e.g. E-mail
'value' => $post_data[$key] // e.g. john#example.com
);
}
This separates the storage/mapping of key/label pairs from the part which processes it, making it easier to maintain and modify in the future.
Another way might be (depending on how "fixed" the second script is, if you can alter it).
$submitted_data['Name']=$post_data['element_3'];
$submitted_data['Email']=$post_data['element_2'];
To get a result more like the one in your question:
$submitted_data['Fields']['0']['Key']='Name';
$submitted_data['Fields']['0']['Value']=$post_data['element_3'];
$submitted_data['Fields']['1']['Key']='Email';
$submitted_data['Fields']['1']['Value']=$post_data['element_2'];