UPDATE not working with arrays - php

I am trying to get the info stored in the array updated to the database. I cannot seem to get it to work. Any help will be appreciated. Thanks.
In the Config Class:
function update_cfg(array $upd)
{
if(is_array($upd))
{
$sql = array();
foreach($upd as $column => $info)
{
if(isset($info) && $column != 'update_cfg')
{
$sql[] = "`" . $column . "`='" . $info . "'";
}
}
if(is_array($sql))
{
$result = $this->Sys->db->query("UPDATE `dj_settings` SET " . implode(', ', $sql));
if($result)
{
unset($sql);
return TRUE;
}
else
{
unset($sql);
return FALSE;
}
}
}
}
Script:
<?php
global $Sys;
if(isset($_POST['update_cfg']))
{
unset($_POST['update_cfg']);
$update = $_POST;
unset($_POST);
if($Sys->Config->update_cfg($update))
{
$Sys->Template->setAlerts('The website has been successfully updated!');
}
else
{
$Sys->Template->setAlerts('The website has not been updated.', 'error');
}
unset($update);
}
?>
<div id="webCfg">
<h2>Website Configuration</h2>
<?php
global $Sys;
global $handle;
$alerts = $Sys->Template->getAlerts();
if ($alerts != '') { echo '<div><ul class="alerts">' . $alerts . '</ul></div>'; }
asort($handle);
if($handle['status'] == 1)
{
$enable = 'checked="checked"';
}
else
{
$disable = 'checked="checked"';
}
foreach ($handle as $key => $val)
{
$label = str_replace('_', ' ', $key);
if(strlen($val) <= 50 && !is_numeric($val))
{
$input[$key] = '<tr><td class="label"><label for="' . $key . '">' . ucwords($label) . '</label></td><td class="content"><input type="text" id="' . $key . '" name="' . $key . '" value="' . $val . '"/></td></tr>';
}
elseif(strlen($val) >= 51 && !is_numeric($val))
{
$input[$key] = '<tr><td class="label"><label for="' . $key . '">' . ucwords($label) . '</label></td><td class="content"><textarea id="' . $key . '" name="' . $key . '">' . ucfirst($val) . '</textarea></td></tr>';
}
elseif(is_numeric($val))
{
$input[$key] = '<tr><td class="label"><label for="' . $key . '">Website Status</label></td><td class="content"><input type="radio" id="' . $key . '" name="' . $key . '" value="1" ' . $enable . '> Enable <input type="radio" id="' . $key . '" name="' . $key . '" value="0" ' . $disable . '> Disable </td></tr>';
}
}
?>
<form action="" method="post">
<table cellpadding="1" cellspacing="0" id="webCfgTbl">
<?php
foreach (array_keys($input) as $key)
{
echo '<div class="row">' . $input[$key] . '</div>';
}
?>
<tr><td class="label"><label for="submit"></label></td><td class="content"><div class="row submitrow"><input type="submit" id="submit" name="update_cfg" class="submit" value="Update Settings" /></div></td></tr>
</table>
</form>
</div>
?>
The info from the database is loaded when the page is. When I change the values and click Update Settings, $Sys->Config->update_cfg() returns FALSE everytime. I am stumped on this and would appreciate any help. Thanks.
EDIT
When I echo the query in the update_cfg() function after the foreach() loop I get this:
UPDATE `dj_settings` SET `status`='0', `disabled_msg`=':: Test Message for Disabled Website ::', `email_auto_response`='Thank you so much for your email. I will respond back as soon as I am able. Usually within 30 minutes. Please feel free to look at the pictures and videos on our website, and the FAQ and Pricing tab is a great resource also. I will respond personally to any questions you may have shortly. Have a great day! Some tips on picking the right DJ: For most hiring a DJ is a first time experience. Picking the right DJ can be daunting and sometimes overwhelming task. Here are some good tips to follow to help you through your process. 1. Make sure they have their LLC or INC, and have a business license for your area. 2. Make sure they are insured. 3. Make sure that you sign some kind of a contract throughout the booking process. 4. Check reviews on reputable websites. ie.. thumbtack.com , weddingwire.com, or yellowpages.com. 5. If the price is too good to be true, it is. This is a big one, because you definitely get what you pay for when you hire a DJ. 6. Sit down with a couple of DJ's and talk them, generally your own intuition will get you far. If you follow those steps, you're sure to get a good DJ. No matter who it is, with my company or with a competitor. Your event will go off smoothly 99% of the time. Marshall Bracewell-Owner Dynamic DJ Company, LLC 803-807-1243 http://www.dynamicdjcompany.com', `web_url`='http://www.dynamicdjcompany.com', `web_email`='marshall#dynamicdjcompany.com'

Well...that's a start I guess...at least the update is working. Try first doing $info = $this->Sys->db->real_escape_string($info); then putting that into the "" . $column . "='" . $info . "'";

Related

Changing radiolist to dropdown

I have stuck ;/ I have activeradiolist and works ok, but I need to create another list bud with dropdowns item's
my code with $model for activeradioList
echo Html::activeradioList($add, 'type_contact',
$items, ['item' => function ($index, $label, $name, $checked, $value) {
$return = '<div style="max-height:178px!important;" class="radio col-xs-12 col-lg-6"><input type="radio" name="' . $name . '" value="' . $value . '" tabindex="3" id="' . $name . $index . '" ' . ($checked ? 'checked' : '') . '>';
$return .= '<label style="padding-top:0!important" for="' . $name . $index . '">' . $label . '</label></div>';
if ($checked && $index === 1) {
$return .= '<script>$(document).ready(function(){$(\'#ref-form\').slideDown()});</script>';
}
return $return;
}]
); ?>
Now i try to convert this to dropdownList like ->
echo CHtml::dropDownList($add, 'type_contact',
$items, ['item' => function ($index, $label, $name, $checked, $value) {
$return = '<div style="max-height:178px!important;" class="radio col-xs-12 col-lg-6"><input type="radio" name="' . $name . '" value="' . $value . '" tabindex="3" id="' . $name . $index . '" ' . ($checked ? 'checked' : '') . '>';
$return .= '<label style="padding-top:0!important" for="' . $name . $index . '">' . $label . '</label></div>';
if ($checked && $index === 1) {
$return .= '<script>$(document).ready(function(){$(\'#ref-form\').slideDown()});</script>';
}
return $return;
}]
); ?>
and have htmlspecialchars() expects parameter 1 to be string, object given
1) The CHtml is class from old Yii 1.x framework. Yii2 doesn't use the C prefix for its class names.
2) You are pairing the form field with your model instance so you should use the activeDropDownList() method instead of dropDownList().
3) In your radio button options you have item callback that is used to generate the html code for radio button. The drop down list doesn't have anything like that so you should remove it from its options. You can completly omit the fourth parameter because the item callback is the only option there.
So the code for drop down list should look like this:
echo Html::activeDropDownList($add, 'type_contact', $items);

Not getting $_POST data

I've never run into this situation I'm having now. And am using this setup in another part of the site. I cannot figure out why I don't get any post data, my session variables never update.
Here is the html form, dynamically creating a row of check boxes:
$html .= '<form id="status_form">';
foreach ($leads as $key => $type) {
$html .= '<input type="checkbox" name"' . $key . '" id="' . $key . '" class="box_status" value="1"';
if (something = true) {
$html .= ' checked="checked"';
}
$html .= ' />';
$html .= '<label class="checkbox-inline" for"' . $key . '">' . $type . ' (' . #$count[$key] . ')</label>';
}
$html .= '</form>';
echo $html;
Each time a checkbox is checked, javascript is submitting the form to primarily update session variables and then reload the page:
function update_report() {
var form_data = $("#status_form").serialize();
$.ajax({
type: 'POST',
url: 'scripts/test.php',
data: form_data,
success: function() {
window.location.reload(true);
}
});
}
$('.box_status').change(function(){
update_report();
});
And here is the test.php file:
session_start();
if (isset ($_POST)) {
$_SESSION['crm']['reports']['index'] = array();
foreach ($_POST as $k => $v) {
$_SESSION['crm']['reports']['index'][] = $k;
}
$_SESSION['post'] = $_POST;
echo true;
}
I've double checked and the form is displayed correctly, the ajax makes the call, I've done "hello world" checks in test.php.. it even goes into the if(isset($_POST)) portion, but the session variables don't change, and if I assign $_POST to another session variable, it shows me nothing.
You have an error in your string concatenation:
$html .= '<input type="checkbox" name"' . $key . '" id="' . $key . '" class="box_status" value="1"';
name"' is supposed to be name="' You forgot the equal sign. jQuery serialize only takes input tags with names.
Your solution:
$html .= '<input type="checkbox" name="' . $key . '" id="' . $key . '" class="box_status" value="1"';
Note:
The best way to debug this sort of problem is to take a look at your generated html. When you load the page, check the page source. You also made the same error on your label tag.

google checkout php callback explained

Hi all i don't know if i'm doing this right or not but i can't understand how google checkout works.
I have the php code that sends the XML to the google checkout everything is ok i reach on the checkout process taxes prices shipping is ok.
$_currency = $params['currency'];
$base_domain = 'https://' . (($params['mode'] == 'transaction') ? 'checkout.google.com' : 'sandbox.google.com/checkout');
$base_url = $base_domain . '/cws/v2/Merchant/' . $params['merchantId'] . '/checkout';
// Form XML array with cart items
$_items = '';
$google_products = $this->CI->go_cart->contents();
foreach ($google_products as $k => $v) {
$item_options = '';
if (isset($v['options'])) {
foreach ($v['options'] as $name => $value) {
if (is_array($value)) {
foreach ($value as $item) {
$item_options .= $item;
}
} else {
$item_options .= $value;
}
}
}
$_items .= '<item>' . '<merchant-item-id>' . $v['id'] . '</merchant-item-id>' . '<item-name>' . strip_tags($v['name']) . ' ' . $item_options . '</item-name>' . '<item-description>' . substr(strip_tags($item_options), 0, 299) . '</item-description>' . "<unit-price currency='" . $_currency . "'>" . $v['price'] . '</unit-price>' . '<quantity>' . $v['quantity'] . '</quantity>' . '</item>';
}
$xml_cart = "<?xml version='1.0' encoding='UTF-8'?>
<checkout-shopping-cart xmlns='http://checkout.google.com/schema/2'>
<shopping-cart>
<merchant-private-data>
<additional_data>
<session_id>" . base64_encode($customer['bill_address']["email"] . date('h:i:s')) . "</session_id>
<currency_code>" . $_currency . "</currency_code>
<payment_id>" . base64_encode($customer['bill_address']["email"] . date('h:i:s')) . "</payment_id>
" . $private_ship_data . "
</additional_data>
</merchant-private-data>
<items>" . $_items . "</items>
</shopping-cart>
<checkout-flow-support>
<merchant-checkout-flow-support>
<platform-id>971865505315434</platform-id>
<request-buyer-phone-number>true</request-buyer-phone-number>
<edit-cart-url>" . $edit_cart_url . "</edit-cart-url>
<merchant-calculations>
<merchant-calculations-url>" . $calculation_url . "</merchant-calculations-url>
</merchant-calculations>
<continue-shopping-url>" . $return_url . "</continue-shopping-url>
" . $shippings . $taxes . "
</merchant-checkout-flow-support>
</checkout-flow-support>
</checkout-shopping-cart>";
$signature = $this->fn_calc_hmac_sha1($xml_cart, $params['merchantKey']);
$b64_cart = base64_encode($xml_cart);
$b64_signature = base64_encode($signature);
echo '<form method="post" action="' . $base_url . '" name="BB_BuyButtonForm">
<input type="hidden" name="cart" value="' . $b64_cart . '" />
<input type="hidden" name="signature" value="' . $b64_signature . '" />
<input alt="" src="' . $base_domain . '/buttons/checkout.gif?merchant_id=' . $params['merchantId'] . '&w=160&h=43&style=' . 'white' . '&variant=text&loc=en_US" type="image"/>
</form>';
exit;
}
After i click on submit payment i'm then redirected to a page with successfull message and with a link to return to my website.
I'm wondering how can i check if the payment was or not successfully.
Also there is a callback that i don't know exactly what is supposed to do.
Please help me understand better
I have attached the code below.
Disclaimer: Am not a PHP dev.
Your code above is known as Part 1. Checkout API documented here. It covers how you get your data/cart to Google for Checkout
Part II. Order Processing covers how to get data from Google (into your systems). A tutorial is provided here. This is where you provide a Callback API URL that listens to (waits for) HTTP POSTs from Google and proceed to process the data.

Update drop down from SQL database using PHP

I know this should be simple but I just can't seem to get my head around it.
I have a list of continents in a sql database that I get back using PHP DBO and display in a drop down list. What I then want to do is get the users preferred continent from the sql database and select that one in the list. E.g if the list contains World, Africa, Europe, N America and S America but the users favorite is 'Europe' I want that one selected. $getContinent is the users preference.
while ($row = $continent_results->fetch(PDO::FETCH_ASSOC)) {
if ($getContinent != ''){
echo '<option value="' . $getContinent . '" selected="selected" >' . $row['CONTINENT_NAME'] . '</option>';
}else{
echo '<option value=' . $row['CONTINENT_ID'] . '>' . $row['CONTINENT_NAME'] . '</option>';
}
}
I would be most grateful if someone could set me straight as I have found some examples on the internet but have been unable to get them to work :)
Your code should be like this
while ($row = $continent_results->fetch(PDO::FETCH_ASSOC)) {
//just check if the option id is equal to the chosen value
if ($getContinent != '' && $getContinent==$row['CONTINENT_ID'] ){
echo '<option value="' . $getContinent . '" selected="selected" >' . $row['CONTINENT_NAME'] . '</option>';
}else{
echo '<option value=' . $row['CONTINENT_ID'] . '>' . $row['CONTINENT_NAME'] . '</option>';
}
}
Its simple, as you guessed :D
You would use something like this:
while ($row = $continent_results->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="' . $row['CONTINENT_ID'] . '">' . $row['CONTINENT_NAME'] . '</option>';
}
I hope that helps!
--al
You can use ternary operator
while ($row = $continent_results->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="' . $row['CONTINENT_ID'] .
($getContinent == $row['CONTINENT_NAME']) ? '" selected="selected"' : '"' . '>' .
$row['CONTINENT_NAME'] . '</option>';
}

Load data from database & retain data between postbacks

In PHP Project, I'm creating page a which retrieve the data from database on page load then allow the user to change data & Update. I have to use input text and option controls to display data. Editing form data may involve in few POST requests. To load data from database and retain data between POSTs I use this approach.
Load Data from Database
$oClass = new DataClass();
$oClass ->GetRecordById($_REQUEST['id']);
$dataName = $oClass ->Name;
$dataYear = $oClass ->Year;
INPUT TEXTs
<input type="text" name="txtName" value="<?php echo isset($_POST['txtName']) ? $_POST['txtName'] : $dataName ?>" >
OPTIONs
$arrYears = GetYears();
foreach ($arrYears as $year) {
if (isset($_POST['ddlYear'])) {
if ($_POST['ddlYear'] == $year) {
echo '<option value="' . $year . '" selected="selected">' . $year . '</option>';
} else {
echo '<option value="' . $year . '">' . $year . '</option>';
}
} else {
if ($dataYear == $year) {
echo '<option value="' . $year . '" selected="selected">' . $year . '</option>';
} else {
echo '<option value="' . $year . '">' . $year . '</option>';
}
}
}
Is there a better approach to retain data between posts than using like
if (isset($_POST['NAME']))
{$_POST['NAME']}
else
{$name}
Is there a reason why you need to retain the info? If a user submits the form it will update the info and therefore will need to be fetched again for the next person to come in and update or read.
Above though you assign $year = $oClass ->Year; but it's not referenced anywhere. the $year in the foreach loop is a local scope variable only for that foreach loop, after that loop finishes executing it disappears.
you can do stuff like this.
if (isset($_POST['txtName']))
{
$name = $_POST['txtName'];
}
else
{
$name = '';
}
<input type="text" name="txtName" value="<?php echo $name; ?>" />
Since the only difference between the options in the foreach loops are whether or not the selected attribute is applied you can shorten both of them to the following.
if (isset($_POST['ddlYear'])) {
echo '<option value="' . $year . ($_POST['ddlYear'] == $year) ? '" selected="selected"' : '' . '>' . $year . '</option>';
}
else {
echo '<option value="' . $year . ($movieyear == $year) ? '" selected="selected"' : '' . '>' . $year . '</option>';
}
Also to get a quicker answer please provide the simplest working use case, for example above example has $_POST variables that aren't defined anywhere and others like $movieyear also not defined.

Categories