Welcome, Guest. Please login or register.
Did you miss your activation email?
May 26, 2012, 08:47:51 AM

Login with username, password and session length
Search:     Advanced search
Wollen Sie dem WebsiteBaker Team beitreten?
Nähere Informationen finden Sie unter hier und auf unserer neuen Webseite.
155535 Posts in 21713 Topics by 7737 Members
Latest Member: chris85
* Home Help Search Login Register
Pages: [1]   Go Down
Print
Author Topic: Querying database using my php code doesn't work?  (Read 516 times)
Locoblade

Offline Offline

Posts: 10


« on: April 29, 2010, 10:58:01 PM »

Hi All

Im a bit of a php virgin but am embarking on a task to mod my Bakery cart process so I can offer Promotional Code / Voucher discounts on orders.

For those not familiar with the bakery cart process, when a user adds stuff to the cart then submits the order, it brings up view_form.php which allows the customer to input their name / address etc, then continuing goes to view_summary.php where the user confirms the order and contact details are correct which before submitting agains to the billing merchant (Paypal etc).

In my view_form.php Ive added a text box where the customer can add in a voucher code if they have one. When the customer submits the order, this box populates a new field I've created in wb_mod_bakery_custo mer called "cust_voucher". This is the table where each order is recorded and basically ties the promotion code to the order being placed. This all works fine and I can see the populated "cust_voucher" in the database.

I've also created a new table in the database called wb_promocode to store the promotional code details. Within this table there are two fields
"code1" stores the text for the promo code (e.g. "APRIL5")
"code1val" stores a number used to calculate the amount of discount to apply (e.g 0.95 to give a 5% discount)
I've populated these two fields in the database, so they can be queried.

The next part of the standard cart process is to open view_summary.php. In here I've entered the following code to query the database to determine if there's a valid promo code, but this is where I'm stuck as the variables don't seem to be correctly set, because if I try to echo the variables, the page fails to display.

Code:
$vouchercode = $database->query("SELECT cust_voucher FROM ".TABLE_PREFIX."mod_bakery_customer WHERE order_id = '{$_SESSION['bakery']['order_id']}'");
echo $vouchercode;
$vcode1 = $database->query("SELECT code1 FROM ".TABLE_PREFIX.promocode);
echo $vcode1;

Obviously once I get the variables working I'll be doing something else with them to calculate the discount and won't just be echoing them out to screen! I've tried debug but it doesn't help, so can anyone explain why the above code won't work and where I'm going wrong as my query seems to be identical in format to others used in the same form?

thanks
Chris
« Last Edit: April 30, 2010, 04:28:07 AM by Locoblade » Logged
crnogorac081
AddOn Development
*
Offline Offline

Posts: 1706



« Reply #1 on: April 30, 2010, 08:21:12 AM »

Hi,

In the third line of your code there is one " missing

$vcode1 = $database->query("SELECT code1 FROM ".TABLE_PREFIX."promocode);

try to correct that and see if t works
« Last Edit: April 30, 2010, 08:22:54 AM by crnogorac081 » Logged

Wow, I coded something myself: PM Messanger Modul ,Searchbox with suggestions
Locoblade

Offline Offline

Posts: 10


« Reply #2 on: April 30, 2010, 08:57:49 AM »

Thanks, I'm pretty sure I tried it with and without the quotation marks on that one, but what confuses me is that the first query doesnt work either, and that's a straight copy of another query to the same table on the same page, the only difference is I changed the field from SELECT cust_last_name to SELECT cust_voucher to read the field I created in that table rather than one that's there by default  huh.
Logged
thorn

Offline Offline

Posts: 980


WWW
« Reply #3 on: April 30, 2010, 10:39:06 AM »

Hello,

Code:
$vouchercode = $database->query("SELECT cust_voucher FROM ".TABLE_PREFIX."mod_bakery_customer WHERE order_id = '{$_SESSION['bakery']['order_id']}'");
echo $vouchercode;
$vcode1 = $database->query("SELECT code1 FROM ".TABLE_PREFIX.promocode);
echo $vcode1;

$database->query()  doesn't return a string, but a mysql-resource (a result-set).
Code:
if($res = $database->query("SELECT link FROM ".TABLE_PREFIX."pages WHERE page_id = '$page_id'")) {
    if($res->numRows() > 0) {
        $page = $res->fetchRow();
         echo $page['link'];
   }
}

if($res = $database->query("SELECT * FROM ".TABLE_PREFIX."mod_news_posts WHERE active = '1'")) {
    if($res->numRows() > 0) {
        while($post = $res->fetchRow()) {
            echo $post['title'];
        }
    }
}


thorn.
Logged

Locoblade

Offline Offline

Posts: 10


« Reply #4 on: April 30, 2010, 03:33:30 PM »

Many thanks thorn, I can now see where I was going wrong!!

that bit's all sorted now and I can specify the variables I need Smiley

cheers
Chris
Logged
Pages: [1]   Go Up
Print
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.16 | SMF © 2011, Simple Machines Valid XHTML 1.0! Valid CSS!