Jump to content

Recommended Posts

Hi,

I am building a shopping cart, which works great on my localhost. but when i upload it to my server i was getting errors, i have fixed most of them and now just need to get rid of any object code in my scitpt.

Could someone please help?

Heres the error
Fatal error: Call to a member function on a non-object in D:\Domains\spirestest.com\wwwroot\login2\cart\inc\functions.inc.php on line 39

Heres the code

function showCart() {

$cart = $_SESSION['cart'];
if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
echo '<form action="cart.php?action=update" method="post" id="cart">';
echo "<center>";
echo "<TABLE width=\"600\" cellpadding=\"0\" cellspacing=\"0\">";
echo
"<TR bgcolor=\"#CCCCCC\">
<TD class=\"Title\">Track</TD>
<TD class=\"Title\">Title</TD>
<TD class=\"Title\">Price</TD>
<TD class=\"Title\">Amount</TD>
<TD class=\"Title\">Total</TD>
<TD class=\"Title\">Remove</TD>
</TR>\n"
;
foreach ($contents as $id=>$qty) {

$sql = 'SELECT * FROM books WHERE id = '.$id;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);



echo '<tr>
<td><br>'.$title.' </td><td><br> '.$author.'</td>
<td><br>&pound;'.$price.'</td>
<td><br><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>
<td><br>&pound;'.($price * $qty).'</td>

<td><br><a href="cart.php?action=delete&id='.$id.'" class="loginBox_text">Remove</a></td>

</tr>';
$total += $price * $qty;
}
echo '</table>';
echo "</center>";
echo "<br><hr>";
echo '<table>
<tr>
<td align=\"right\"><span>Grand total: <strong>&pound;'.$total.'</strong></span></td>&nbsp;
</tr>
<tr>
<td align=\"right\"><div><button type="submit">Update cart</button></div></td>
</tr>
</table>';
echo '</form>';
} else {
echo '<p class="loginBox_text">You shopping cart is empty.</p>';
}
echo "</TABLE>\n";
return join('',$output);
}


Thanks for all of your help. [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]
Link to comment
https://forums.phpfreaks.com/topic/13209-editing-a-function/
Share on other sites

It sounds like the error is coming from when you try to query the database. It is saying that $db is not an object. I don't see it being defined anywhere in your script so I think that is the problem. If $db is supposed to be a database abstraction object you need to include the class file and set it with $db = new DB(); or whatever.
Link to comment
https://forums.phpfreaks.com/topic/13209-editing-a-function/#findComment-50838
Share on other sites

Hay, I have got abit further,

Is there another way of doing this

$result = $db->query($sql);
$row = $result->fetch();
extract($row);

Not in objects??????


Thanks [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /]

Hi buyocat

I really dont know much about objects.
The script was connecting the new db() bla bla bla.
But i'm trying to convert this so i doesn't need to .


Is this a bad idea?
Link to comment
https://forums.phpfreaks.com/topic/13209-editing-a-function/#findComment-50839
Share on other sites

There are a number of ways to skin this cat. I suggest using a PEAR package, such as MDB2, which will handle most everything for you and give you more flexibility. Just in case you are worried about a learning curve, getting it running is as simple as including the file and following the (admittedly sparse) instructions/examples on PEAR. If you don't want to take the plunge however you can accomplish it with something along these lines...

$dbc = mysql_connect(various information here);
// check that the connection was established
$result = @mysql_query(a query here);
// check that the result is what you wanted
// do stuff

I'll let you look up those functions on php.net. You can probably just enter in...
php.net/mysql_connect
php.net/mysql_query
Link to comment
https://forums.phpfreaks.com/topic/13209-editing-a-function/#findComment-50844
Share on other sites


HI thanks for your help.

I have already converted the inc.db page and that connects fine.


<?php
$host = 'europa';
$user = '*******';
$pass = '*******;
$db = 'jeff_cart';

$connect = mysql_connect($host, $user, $pass) or die ("could not connect");
mysql_select_db($db) or die ("could not select db");

?>

However, i just need to now convert this simple bit of code, so it none object related.

$sql = 'SELECT * FROM books WHERE id = '.$id;
$result = $db->query($sql);
$row = $result->fetch();
extract($row);


I been playing around with a few things here. Which removes the error, but no resutls are displaying?

$sql = 'SELECT * FROM books WHERE id = '.$id;
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
extract($row);

goto [a href=\"http://www.spirestest.com/login2/cart/cart.php\" target=\"_blank\"]My Webpage[/a]

Thanks once again for all your help.
Its nice to be in the hands of masters.

Link to comment
https://forums.phpfreaks.com/topic/13209-editing-a-function/#findComment-50849
Share on other sites

No Luck,

However, i'm not to sure if i put it in the right place.

Could you please take a look?

function showCart() {

$cart = $_SESSION['cart'];
if ($cart) {
$items = explode(',',$cart);
$contents = array();
foreach ($items as $item) {
$contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1;
}
echo '<form action="cart.php?action=update" method="post" id="cart">';
echo "<center>";
echo "<TABLE width=\"600\" cellpadding=\"0\" cellspacing=\"0\">";
echo
"<TR bgcolor=\"#CCCCCC\">
<TD class=\"Title\">Track</TD>
<TD class=\"Title\">Title</TD>
<TD class=\"Title\">Price</TD>
<TD class=\"Title\">Amount</TD>
<TD class=\"Title\">Total</TD>
<TD class=\"Title\">Remove</TD>
</TR>\n"
;
foreach ($contents as $id=>$qty) {

$sql = 'SELECT * FROM books WHERE id = '.$id;
$result = mysql_query($sql);
$row = mysql_fetch_row($result);

while ($data = mysql_fetch_array($result, mysql_num)) {
echo $data[0];



echo '<tr>
<td><br>'.$row['title'].' </td><td><br> '.$row['author'].'</td>
<td><br>&pound;'.$row['price'].'</td>
<td><br><input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /></td>
<td><br>&pound;'.($price * $qty).'</td>

<td><br><a href="cart.php?action=delete&id='.$id.'" class="loginBox_text">Remove</a></td>

</tr>';
$total += $price * $qty;
}
}
echo '</table>';
echo "</center>";
echo "<br><hr>";
echo '<table>
<tr>
<td align=\"right\"><span>Grand total: <strong>&pound;'.$total.'</strong></span></td>&nbsp;
</tr>
<tr>
<td align=\"right\"><div><button type="submit">Update cart</button></div></td>
</tr>
</table>';
echo '</form>';
} else {
echo '<p class="loginBox_text">You shopping cart is empty.</p>';
}
echo "</TABLE>\n";
return join('',$output);
}


Cheers Buyocat
Link to comment
https://forums.phpfreaks.com/topic/13209-editing-a-function/#findComment-50854
Share on other sites

What error are you getting when you run this? If it isn't printing an error try echoing out something from the top of your script working down until you hit where the error is. Finally, if the error is with the mysql try this:

$result = mysql($query) or die (mysql_error()); Or something like that

Looking at your code, I'm a little confused about what you're doing after the query. It looks like it would try to echo out a result column but then move past that and use mysql_fetch_row. I don't know how mysql_fetch_row works, but it would be worth print_r - ing the $row variable to see what it is being set to.
Link to comment
https://forums.phpfreaks.com/topic/13209-editing-a-function/#findComment-50858
Share on other sites

Sorry to be such a pain.

I'm not getting an error now.
Its just not displaying any $*****
Everything else in the function is being displated

The page before is working, and the edit page is working,
Its just this function.

Goto and you will see,
[a href=\"http://www.spirestest.com/login2\" target=\"_blank\"]My Webpage[/a]

Thanks
Link to comment
https://forums.phpfreaks.com/topic/13209-editing-a-function/#findComment-50866
Share on other sites

Hay, Thanks for your help.

I HAVE DONE IT! WWOOOO


He is the solution,


foreach ($contents as $id=>$qty) {

$sql = 'SELECT * FROM books WHERE id = '.$id;
$result = mysql_query($sql);
$row = mysql_fetch_array($result);

echo '<tr>
<td><br>'.$row['title'].' </td><td><br> '.$row['author'].'</td>
<td><br>&pound;'.$row['price'].'</td>
<td><br><input type="text" name="qty'.$row['id'].'" value="'.$qty.'" size="3" maxlength="3" /></td>
<td><br>&pound;'.($row['price'] * $qty).'</td>

<td><br><a href="cart.php?action=delete&id='.$id.'" class="loginBox_text">Remove</a></td>

</tr>';
$total += $row['price'] * $qty;

}


Thanks once again. [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /]
Link to comment
https://forums.phpfreaks.com/topic/13209-editing-a-function/#findComment-50877
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.