Recursive PHP PDO POST INSERT - php

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.

Related

Datastax PHP Cassandra, getting "No Value or Column for name" error

Cassandra\Exception\InvalidArgumentException: No value or column for name
I'm trying to update some old code from YACassandraPDO to Datastax's new PHP extension, and I'm running into some issues. Notably, this error. I can't figure out why or what's wrong.
A parameter numbers match. Is there something drastically different Datastax is doing for prepared statements?
$query = "UPDATE {$this->keyspace}.visit USING TTL {$this->ttl}
SET ip=:ip, browser=:browser, referrer=:ref, terms=:terms
, url=:url, stime=:stime, etime=:etime
, company=:company, isp=:isp, domain=:domain, client_type = :client_type
, city=:city, state=:state_cd, postal_cd=:postal, country=:country
, country_cd=:country_cd, latitude=:lat, longitude=:long, area_cd=:area_cd
, user_id=:user_id
WHERE visit_dt=:dt AND cid=:cid AND sid=:sid AND pvid=:pvid";
The array is:
$params = [
':dt' => new Timestamp(strtotime(date('Y-m-d')), 0),
':cid' => $data['customerId'],
':sid' => $data['sid'],
':pvid' => $data['pvid'],
// Columns
':ip' => $ip,
':browser' => utf8_encode($data['browser']),
':client_type' => utf8_encode($device),
':ref' => utf8_encode($data['referrer']),
':terms' => utf8_encode($terms),
':url' => utf8_encode($data['url']),
':stime' => $data['now'],
':etime' => $data['etime'],
':user_id' => $data['userid'],
// Geodecode information
':company' => utf8_encode($geo->isp),
':domain' => utf8_encode($geo->domainName),
':isp' => utf8_encode($geo->isp),
':city' => utf8_encode($geo->cityName),
':state_cd' => utf8_encode($geo->regionName),
':country' => utf8_encode($geo->countryName),
':country_cd' => utf8_encode($geo->countryCode),
':postal' => $geo->zipCode,
':lat' => floatval($geo->latitude),
':long' => floatval($geo->longitude),
':area_cd' => $areac
];
Execution:
$prep = $this->adapter()->prepare($stmnt);
$return = $this->adapter()->execute($prep, ['arguments' => $params]);
Okay, I've discovered the issue: the colens. ':key' needs to be just 'key'.
Man, that took just too long to see. All my old bind names were $stmnt->bindValue(':company', [...]);; so, it completely escaped my view.

inserting selected option from dropdown into db codeigniter

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

Cast double to int inside Smarty

I am trying to display double variable inside an Smarty environment converting it into int.
The File configuring the variables has the following code.
The Variable PRODUCTS_Quantity_x is the viewed Variable which I added. It is giving me a comma separated value with 3 fractional digits. The {$module_data.PRODUCTS_Quantity} is already be able to get displayed in through the html.
I have already tryed things like count() etc. I mostly got an empty variable or smarty errors ...
$t_data_array = array ( 'PRODUCTS_NAME' => htmlspecialchars_wrapper($array['products_name']),
'COUNT' => $array['ID'],
'PRODUCTS_ID' => $array['products_id'],
'PRODUCTS_VPE' => $this->getVPEtext($array, $products_price['plain']),
'PRODUCTS_IMAGE' => $this->productImage($array['products_image'], $image),
'PRODUCTS_IMAGE_W' => $array['products_image_w'],
'PRODUCTS_IMAGE_H' => $array['products_image_h'],
'PRODUCTS_IMAGE_WIDTH' => PRODUCT_IMAGE_THUMBNAIL_WIDTH,
'PRODUCTS_IMAGE_PADDING' => ((PRODUCT_IMAGE_THUMBNAIL_HEIGHT + 8) - $array['products_image_h'])/2,
'PRODUCTS_IMAGE_ALT' => $array['gm_alt_text'],
'PRODUCTS_LINK' => $gm_product_link,
'PRODUCTS_PRICE' => $products_price['formated'],
'PRODUCTS_TAX_INFO' => $gm_tax_info,
'PRODUCTS_SHIPPING_LINK' => $gm_shipping_link,
'PRODUCTS_BUTTON_BUY_NOW' => $buy_now,
'GM_PRODUCTS_BUTTON_BUY_NOW_URL' => $gm_buy_now_url,
'GM_PRODUCTS_BUTTON_BUY_NOW' => $gm_buy_now,
'PRODUCTS_SHIPPING_NAME' => $shipping_status_name,
'PRODUCTS_SHIPPING_IMAGE' => $shipping_status_image,
'PRODUCTS_DESCRIPTION' => $array['products_description'],
'PRODUCTS_EXPIRES' => $array['expires_date'],
'PRODUCTS_CATEGORY_URL' => $array['cat_url'],
'PRODUCTS_SHORT_DESCRIPTION' => $array['products_short_description'],
'PRODUCTS_FSK18' => $array['products_fsk18'],
'GM_FORM_ACTION' => xtc_draw_form('gm_add_to_cart_'.$array['products_id'], xtc_href_link('index.php', 'action=buy_now&BUYproducts_id=' . $array['products_id'] . $gm_cat_search, 'NONSSL', true, true, true), 'post', 'onsubmit="return gm_quantity_check_listing(\'' . $array['products_id'] . '\')"'),
'FORM_DATA' => $t_form_array,
'QTY_DATA' => $t_qty_array,
'PRODUCTS_Quantity_x' => $array['products_quantity'],
'GM_FORM_END' => '</form>',
'GM_PRODUCTS_QTY' => $gm_qty,
'GM_PRODUCTS_STOCK' => $gm_products_stock,
'PRODUCTS_META_DESCRIPTION' => $array['products_meta_description'],
'PRODUCTS_WEIGHT' => gm_prepare_number((double)$array['products_weight'], $xtPrice->currencies[$xtPrice->actualCurr]['decimal_point']),
'SHOW_PRODUCTS_WEIGHT' => $array['gm_show_weight']);
return $t_data_array;
// EOF GM_MOD
Inside the html the following code is given
<span class="label">{$module_data.PRODUCTS_Quantity_x}<span>
I hope you can help me out here
thanks in advance
You can do this 2 way:
1) First way:
You simple use in Smarty the following code with added string_format:
<span class="label">{$module_data.PRODUCTS_Quantity_x|string_format:"%d"}<span>
2) Second way:
You do whatever you need in PHP and assign desired value in PHP.
So in PHP you can change line:
'PRODUCTS_Quantity_x'=> $array['products_quantity'],
into
'PRODUCTS_Quantity_x'=> (int) $array['products_quantity'],
and then in Smarty you use the same code as you used earlier (without string_format):
<span class="label">{$module_data.PRODUCTS_Quantity_x}<span>

Conditionally remove a word from a string?

I've found answers that are similar to the one I'm looking for, but not one that completely addresses my problem.
A user clicks a link on my page which takes them to Google Books and does a search. However, if the first word in the title of the book contains a contraction, everything gets thrown off and none of my corrections seem to work.
Example:
The link on my page looks like this:
mysite.com/book-search?bookauthor=Lance Armstrong&booktitle=It's Not About the Bike
However, when I run this:
$bookTitle = $_GET['booktitle']
I get the same title with the apostrophe in "It's" removed. And that's enough to completely throw off the Google Books Search and return no results.
The endpoint in the cURL session that does the search looks like this:
https://www.googleapis.com/books/v1/volumes?q=intitle:' . $bookTitle . '+inauthor:' . $bookAuthor;
Try it yourself -
https://www.googleapis.com/books/v1/volumes?q=intitle:It%27s+Not_About+The+Bike+inauthor:Lance+Armstrong
Returns a completely different result than
https://www.googleapis.com/books/v1/volumes?q=intitle:Its+Not_About+The+Bike+inauthor:Lance+Armstrong
You'd think that if I urlencoded the strings, that would solve the problem. But it doesn't. Still, for a reason that I can't fathom, when I do this:
$google_endpoint_a = 'https://www.googleapis.com/books/v1/volumes?q=intitle:' . urlencode($bookTitle) . '+inauthor:' . urlencode($bookAuthor);
...the urlencoding gets stripped out when it's passed to the GoogleAPI and I get a zero result search again.
Can somebody help me wade through this? I've tried everything I can think of.
OH - and here's the bonus - this ONLY happens if the first word in the string contains the apostrophe. So it happens with book titles like "It's Not About The Bike" and "Don't Blink". Apostrophes in other words don't seem to matter. (Like author Bill O'Reilly, for example.)
Is there an easy way to remove the first word from the string IF it contains an apostrophe? Or is there a better way?
The Really Ugly Solution:
So PHP passes $_GET variables through urldecode(). So even though it's ugly, here's what I ended up doing that solved the problem, even though I hope someone will come up with a better way.
Before I passed the variable through the URL, I replaced the apostrophe with a string of gibberish, like this:
$booktitle = str_replace("'", "W5x32RS97", $booktitle);
Then ran this:
$bookTitleTemp = $_GET('booktitle');
$bookTitle = str_replace("W5x32RS97", "'", $bookTitleTemp);
And suddenly, everything works. But there's GOT to be a better way.
Hadn't you try to subtitute the ' by \' in your title string? I mean, instead of:
It's Not About the Bike, using this: It\'s Not About the Bike
Without the apostrophe, you can drop intitle: and just use the following string:
$google_endpoint_a = 'https://www.googleapis.com/books/v1/volumes?q=' . urlencode($bookTitle) . '+inauthor:' . urlencode($bookAuthor);
https://www.googleapis.com/books/v1/volumes?q=Its+Not_About+The+Bike&inauthor:Lance+Armstrong
Update
To write a condition to use intitle if the first word has an apostrophe, you can do something like:
$pieces = explode(" ", $bookTitle);
if (strpos($pieces[0], "'") !== FALSE) {
$google_endpoint_a = 'https://www.googleapis.com/books/v1/volumes?q=' . urlencode($bookTitle) . '+inauthor:' . urlencode($bookAuthor);
}
else {
$google_endpoint_a = 'https://www.googleapis.com/books/v1/volumes?q=intitle:' . urlencode($bookTitle) . '+inauthor:' . urlencode($bookAuthor);
}
I would also see if encoding the variables earlier would work:
$bookTitle = urlencode($_GET[booktitle]);
in your link
use "It\'s"
not IT's
if you want to remove any apostrophe in the first word, then just test that the apostrophe has a lower string position than the first space. If it does, then remove it.
if(strpos($bookTitle,"'") < strpos($bookTitle," ")){
$bookTitle = preg_replace('/\'/', '', $bookTitle, 1);
}
Desperate diseases need desperate remedies.
$translation = array("aint" => "ain't", "arent" => "aren't", "cant" => "can't", "cantve" => "can't've", "cause" => "'cause", "couldve" => "could've", "couldnt" => "couldn't", "couldntve" => "couldn't've", "didnt" => "didn't", "doesnt" => "doesn't", "dont" => "don't", "hadnt" => "hadn't", "hadntve" => "hadn't've", "hasnt" => "hasn't", "havent" => "haven't", "hed" => "he'd", "hedve" => "he'd've", "hell" => "he'll", "hellve" => "he'll've", "hes" => "he's", "hows" => "how's", "Id" => "I'd", "Idve" => "I'd've", "Ill" => "I'll", "Illve" => "I'll've", "Im" => "I'm", "Ive" => "I've", "isnt" => "isn't", "itd" => "it'd", "itdve" => "it'd've", "itll" => "it'll", "itllve" => "it'll've", "its" => "it's", "lets" => "let's", "maam" => "ma'am", "mightve" => "might've", "mightnt" => "mightn't", "mightntve" => "mightn't've", "mustve" => "must've", "mustnt" => "mustn't", "mustntve" => "mustn't've", "oclock" => "o'clock", "oughtnt" => "oughtn't", "oughtntve" => "oughtn't've", "shant" => "shan't", "shantve" => "shan't've", "shed" => "she'd", "shedve" => "she'd've", "shell" => "she'll", "shellve" => "she'll've", "shes" => "she's", "shouldve" => "should've", "shouldnt" => "shouldn't", "shouldntve" => "shouldn't've", "sos" => "so's", "thats" => "that's", "thered" => "there'd", "theres" => "there's", "theyd" => "they'd", "theyll" => "they'll", "theyllve" => "they'll've", "theyre" => "they're", "theyve" => "they've", "wasnt" => "wasn't", "wed" => "we'd", "well" => "we'll", "wellve" => "we'll've", "were" => "we're", "weve" => "we've", "werent" => "weren't", "whatll" => "what'll", "whatllve" => "what'll've", "whatre" => "what're", "whats" => "what's", "whatve" => "what've", "whens" => "when's", "whenve" => "when've", "whered" => "where'd", "wheres" => "where's", "whereve" => "where've", "wholl" => "who'll", "whollve" => "who'll've", "whos" => "who's", "whove" => "who've", "whys" => "why's", "willve" => "will've", "wont" => "won't", "wontve" => "won't've", "wouldve" => "would've", "wouldnt" => "wouldn't", "wouldntve" => "wouldn't've", "yall" => "y'all", "yalldve" => "y'all'd've", "yallre" => "y'all're", "yallve" => "y'all've", "youd" => "you'd", "youdve" => "you'd've", "youll" => "you'll", "youllve" => "you'll've", "youre" => "you're", "youve" => "you've", );
$bookTitle = strtr($bookTitle, $translation);

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