php - How Can I Populate a Simple Shopping Cart from Two MySQL Databases? -


i developing simple php shopping cart , overall going well. difficulty having in being able pull data 2 separate databases in order populate fields of shopping cart (unit price, name, quantities, etc...).

here showcart function:

function showcart() { global $db; $cart = $_session['cart']; if ($cart) {     $items = explode(',',$cart);     $contents = array();     foreach ($items $item) {         $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;     }     $output[] = '<form action="cart.php?action=update" method="post" id="cart">';     $output[] = '<table>';     foreach ($contents $id=>$qty) {          $sql = 'select * table_name id ='. $id;         $result = $db->query($sql);         $row = $result->fetch();         extract($row);         $output[] = '<tr>';         $output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">remove</a></td>';         $output[] = '<td>'.$product_name.' '.$country.'</td>';         $output[] = '<td>&#36;'.$price.'</td>';         $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';         $output[] = '<td>&#36;'.($price * $qty).'</td>';         $total += $price * $qty;         $output[] = '</tr>';     }     $output[] = '</table>';     $output[] = '<p>grand total: <strong>&#36;'.$total.'</strong></p>';     $output[] = '<div><button type="submit">update cart</button></div>';     $output[] = '</form>'; } else {     $output[] = '<p>you shopping cart empty.</p>'; } return join('',$output); } 

the cart works fine shown above. can see have pulled id, price, product name , few other bits mysql database table. now, make price come different database table. this, tried following...

first, created new connection price value after (get_price.php):

include 'connect.php';  $actual_price='_post['new_price']';  $offer_price = mysql_connect("$host", "$username", "$password") or die ("unable connect server"); mysql_select_db("$database", $offer_price) or die ("unable select database");  $get_value=mysql_query("select * offers actual_price = '$actual_price'");  $value_rslt = $db->query($get_value);         $vlu_row = $value_rslt->fetch();         extract($vlu_row);          $vlu_otp[] = '<td>&#36;'.$special_offer.'</td>';         $vlu_otp[] = '<td><input type="text" name="qty'.$unique_id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';         $vlu_otp[] = '<td>&#36;'.($special_offer * $qty).'</td>';         $total += $special_offer * $qty;         $vlu_otp[] = '</tr>'; 

then used call:

        include 'get_price.php'; 

as substitute in original code:

    function showcart() { global $db; $cart = $_session['cart']; if ($cart) {     $items = explode(',',$cart);     $contents = array();     foreach ($items $item) {         $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;     }     $output[] = '<form action="cart.php?action=update" method="post" id="cart">';     $output[] = '<table>';     foreach ($contents $id=>$qty) {          $sql = 'select * table_name id ='. $id;         $result = $db->query($sql);         $row = $result->fetch();         extract($row);         $output[] = '<tr>';         $output[] = '<td><a href="cart.php?action=delete&id='.$id.'" class="r">remove</a></td>';         $output[] = '<td>'.$product_name.' '.$country.'</td>';          $output[] = '<td><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>';          include 'get_price.php'; } else {     $output[] = '<p>you shopping cart empty.</p>'; } return join('',$output); } 

this created ton of errors , dropped values. solution?

first, try , see if use single database this.

if not, take @ get_price.php. trying connect second database here , use variable $offer_price it. but, when running query against second database, using $db->query($get_value); run against first database.

solution: create 2 database connections in connect.php - $db_cart , $db_price. proceed usual using relevant connection. rest of code seems fine.


Comments

Popular posts from this blog

c# - How to set Z index when using WPF DrawingContext? -

razor - Is this a bug in WebMatrix PageData? -

visual c++ - Using relative values in array sorting ( asm ) -