Deanznet Posted December 3, 2007 Share Posted December 3, 2007 Hey WOrking on this item cart and its been killing me.. This is what i got it lets them buy, Right now when they buy it adds the quanity to the person invetory. But i dosent subtract the quantity from the store. if you know what i mean, That is their are 5 items in the store the users can keep on buying and the 5 never gose down. How would i make it run out after 5 times if ya know what i mean? Second problem is displaying items in the store. if the quantity value is 0 than dont display anything. if its greater than 0 than display. <?php function dbQuery($sql) { return mysql_query($sql) or die('Query failed. ' . mysql_error()); } function dbFetchAssoc($result) { return mysql_fetch_assoc($result); } function dbNumRows($result) { return mysql_num_rows($result); } //select the users money $query = mysql_query("SELECT user_points FROM phpbb_users WHERE user_id='$user_id'")or die(mysql_error()); $user_points = mysql_fetch_assoc($query); //check if they submitted the form if (isset($_POST['submit'])) { if ($_POST['action'] == 'buy') { echo "<br>"; //Loop through the selected items foreach($_POST['selected'] as $itemID){ echo $itemID.'<br>'; //get the items price $get_price = mysql_query("SELECT item_price FROM items WHERE item_name='$itemID'") or die(mysql_error()); $price = mysql_fetch_assoc($get_price); //THIS IS WHERE THE MYSQL QUERY TO SELL THE ITEM SHOULD GO. $result = mysql_query("SELECT item_name, item_quanity FROM items WHERE item_name='$itemID'")or die(mysql_error()); $num_rows = mysql_num_rows($result); if (dbNumRows($result) != 1) { // the product doesn't exist echo "Product Dosent Exist<br>"; // how many of this product we // have in stock $row = dbFetchAssoc($result); $currentStock = $row['item_quanity']; if ($currentStock == 0) { // we no longer have this product in stock // show the error message echo "Item Not In Stock<br>"; exit; } } if (dbNumRows($result) == 0) { // put the product in cart table $sql = "INSERT INTO user_item (item_name, item_owner_id) VALUES ('$itemID', '$user_id'"; $result = dbQuery($sql); } else { // update product quantity in cart table $sql = "UPDATE user_item SET item_quanity = item_quanity + 1 WHERE user_owner_id = '$user_id' AND item_name = '$itemID' "; $result = dbQuery($sql); } echo "Item Bought<br>"; //they have enough, now do a query to subtract their money $subtract = "UPDATE phpbb_users SET user_points=user_points-{$price['item_price']} WHERE user_id='$user_id'"; $subtract_final = mysql_query($subtract)or die('ERROR: '.mysql_error().' with query<br>'.$subtract); } } } $result = mysql_query("SELECT * FROM items WHERE item_in_shop = '1' ")or die(mysql_error()); $num_rows = mysql_num_rows($result); print "<br>There are $num_rows cards left in the store.<P>"; print "<form action='{$_SERVER['PHP_SELF']}' method='post'>"; print '<table width=500 height= 100 border=1>'."\n"; while ($get_info = mysql_fetch_assoc($result)) { print "<tr>"; print "\t<td><font face=arial size=1/>"; print "<input type='checkbox' name='selected[]' value='{$get_info['item_name']}' />" .$get_info['item_name'].'<br><td><center>'.$get_info['item_price'].'<br><center>'.Boomies.'<br><td><center>Quality:<br><center>'.$get_info['item_quality'].'<br><td>'.$get_info['item_desc']."</font></td>"; print "</tr>"; } echo '<select name="action">' .'<option value="buy">Buy</option>' .'</select><p>'; echo '<input type="submit" name="submit">'; echo '</form>'; ?> Quote Link to comment Share on other sites More sharing options...
mr_mind Posted December 3, 2007 Share Posted December 3, 2007 Try this and see if it works. <?php if(isset($user_id)) { $user_info_query = mysql_query("SELECT * FROM phpbb_users WHERE user_id='$user_id'"); $user_info_array = mysql_fetch_array($user_info_query); $user_cash = $user_info_array['user_points']; if($_POST['submit']) { if($_POST['action']=='buy') { foreach($_POST['selected'] as $itemID){ $price_query = mysql_query("SELECT * FROM items WHERE item_name='$itemID'"); $price_array = mysql_fetch_array($price_query); $price = $price_array['item_price']; $quantity_query = mysql_query("SELECT * FROM items WHERE item_name='$itemID'"); $quantity_array = mysql_fetch_array($quantity_query); $quantity = $quantity_array['item_quantity']; if($quantity => 1) { $num_owned_query = mysql_query("SELECT * FROM user_item WHERE item_owner_id='$user_id' AND item_name='$itemID'"); $num_owned_array = mysql_fetch_array($num_owned_query); $num_owned = $num_owned_array['item_quantity']; if($user_cash => $price_array['item_price']) { if($num_owned == 0) { if(mysql_query("INSERT INTO user_item (item_name, item_owner_id) VALUES ('$itemID', '$user_id')")) { $user_cash_new = $user_cash-$price_array['item_price']; if(mysql_query("UPDATE phpbb_users SET user_points='$user_cash_new' WHERE user_id='$user_id'")) { $quantity_new = $quantity-1 if(mysql_query("UPDATE items SET item_quantity='$quantity_new' WHERE item_name='$itemID'")) { print 'Item bought'; } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { $num_owned_new = $num_owned_array['item_quantity']+1; if(mysql_query("UPDATE user_item SET item_quanity='$num_owned_new' WHERE item_owner_id='$user_id' AND item_name='$itemID'")) { $user_cash_new = $user_cash-$price_array['item_price']; if(mysql_query("UPDATE phpbb_users SET user_points='$user_cash_new' WHERE user_id='$user_id'")) { $quantity_new = $quantity-1 if(mysql_query("UPDATE items SET item_quantity='$quantity_new' WHERE item_name='$itemID'")) { print 'Item bought'; } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { print 'Failed connecting to the server. Please contact the site admin.'; } } } else { print 'Item is out of stock'; } } else { print 'You do not have enough money'; } } } } else { $item_query = mysql_query("SELECT * FROM items"); print '<form action=' . $_SERVER['PHP_SELF'] . ' method=post>'; print '<table width=500px border=1>'; print '<tr>'; print '<td> Item Name </td>'; print '<td width=125px> Price </td>'; print '<td width=125px> In Stock </td>'; print '<td width=125px> Quantity </td>'; print '<td width=125px> Buy </td>'; while($item_array = mysql_fetch_array($item_query)) { $item_name = $item_array['item_name']; if($user_cash => $item_array['item_price']) { $item_price = '<span style="color: #00FF00;">' . $item_array['item_price'] . '</span>'; } else { $item_price = '<span style="color: #FF0000;">' . $item_array['item_price'] . '</span>'; } $item_quantity = $item_array['item_quantity']; if($item_quantity => 1) { $item_in_stock = '<span style="color: #00FF00;">In Stock</span>'; } else { $item_in_stock = '<span style="color: #FF0000;">Sold Out</span>'; } print '<tr>'; print '<td> ' . $item_name . ' </td>'; print '<td width=125px> ' . $item_price . ' </td>'; print '<td width=125px> ' . $item_in_stock . ' </td>'; print '<td width=125px> ' . $item_quantity . ' </td>'; print '<td width=125px>'; print '<input type=checkbox name=selected value=' . $item_name . ' />'; print '</td>'; print '</tr>'; } print '</table>'; print '<input type=submit name=submit value="Buy Items">'; print '</form>'; } } else { print 'You must be logged in to view this page'; } ?> Quote Link to comment Share on other sites More sharing options...
Deanznet Posted December 3, 2007 Author Share Posted December 3, 2007 Parse error: parse error, unexpected T_DOUBLE_ARROW in ... on line 114 if($quantity => 1) { Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 3, 2007 Share Posted December 3, 2007 if($quantity >= 1) { PhREEEk Quote Link to comment Share on other sites More sharing options...
Deanznet Posted December 3, 2007 Author Share Posted December 3, 2007 Thanks Parse error: parse error, unexpected T_IF line 124 if(mysql_query("UPDATE items SET item_quantity='$quantity_new' WHERE item_name='$itemID'")) { print 'Item bought'; Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 3, 2007 Share Posted December 3, 2007 That error occurred one line up $quantity_new = $quantity-1; if(mysql_query("UPDATE items SET item_quantity='$quantity_new' WHERE item_name='$itemID'")) { print 'Item bought'; PhREEEk Quote Link to comment Share on other sites More sharing options...
mr_mind Posted December 3, 2007 Share Posted December 3, 2007 Sorry i was really tired last night and didnt test it Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 3, 2007 Share Posted December 3, 2007 Sorry i was really tired last night and didnt test it Said that to my wife once... she wasn't amused! oh wait.. no... replace tired with drunk! yep.. she wasn't amused!! = o = D Quote Link to comment Share on other sites More sharing options...
Deanznet Posted December 3, 2007 Author Share Posted December 3, 2007 Haha that was funny lol.. That code doesn't work. Wont display anything just say user not logged in. I know the code i have works really good just need it to show items that have more than 0 in stock and update the quantity when they buy it Quote Link to comment Share on other sites More sharing options...
Deanznet Posted December 3, 2007 Author Share Posted December 3, 2007 Anyone ??? Quote Link to comment Share on other sites More sharing options...
mr_mind Posted December 3, 2007 Share Posted December 3, 2007 I stated that the form of user verification that i included should be changed there at the beggining and listed off some things that you should change to make it work prooper;ly Quote Link to comment Share on other sites More sharing options...
Deanznet Posted December 3, 2007 Author Share Posted December 3, 2007 Okay. <?php if($userdata['session_logged_in']) { $user_info_query = mysql_query("SELECT * FROM phpbb_users WHERE user_id='$user_id'"); $user_info_array = mysql_fetch_array($user_info_query); $user_cash = $user_info_array['user_points']; if($_POST['submit']) { if($_POST['action']=='buy') { foreach($_POST['selected'] as $itemID){ $price_query = mysql_query("SELECT * FROM items WHERE item_name='$itemID'"); $price_array = mysql_fetch_array($price_query); $price = $price_array['item_price']; $quantity_query = mysql_query("SELECT * FROM items WHERE item_name='$itemID'"); $quantity_array = mysql_fetch_array($quantity_query); $quantity = $quantity_array['item_quantity']; if($quantity >= 1) { $num_owned_query = mysql_query("SELECT * FROM user_item WHERE item_owner_id='$user_id' AND item_name='$itemID'"); $num_owned_array = mysql_fetch_array($num_owned_query); $num_owned = $num_owned_array['item_quantity']; if($user_cash >= $price_array['item_price']) { if($num_owned == 0) { if(mysql_query("INSERT INTO user_item (item_name, item_owner_id) VALUES ('$itemID', '$user_id')")) { $user_cash_new = $user_cash-$price_array['item_price']; if(mysql_query("UPDATE phpbb_users SET user_points='$user_cash_new' WHERE user_id='$user_id'")) { $quantity_new = $quantity-1; if(mysql_query("UPDATE items SET item_quantity='$quantity_new' WHERE item_name='$itemID'")) { print 'Item bought'; } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { $num_owned_new = $num_owned_array['item_quantity']+1; if(mysql_query("UPDATE user_item SET item_quanity='$num_owned_new' WHERE item_owner_id='$user_id' AND item_name='$itemID'")) { $user_cash_new = $user_cash-$price_array['item_price']; if(mysql_query("UPDATE phpbb_users SET user_points='$user_cash_new' WHERE user_id='$user_id'")) { $quantity_new = $quantity-1; if(mysql_query("UPDATE items SET item_quantity='$quantity_new' WHERE item_name='$itemID'")) { print 'Item bought'; } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { print 'Failed connecting to the server. Please contact the site admin.'; } } } else { print 'Item is out of stock'; } } else { print 'You do not have enough money'; } } } } else { $item_query = mysql_query("SELECT * FROM items"); print '<form action=' . $_SERVER['PHP_SELF'] . ' method=post>'; print '<table width=500px border=1>'; print '<tr>'; print '<td> Item Name </td>'; print '<td width=125px> Price </td>'; print '<td width=125px> In Stock </td>'; print '<td width=125px> Quantity </td>'; print '<td width=125px> Buy </td>'; while($item_array = mysql_fetch_array($item_query)) { $item_name = $item_array['item_name']; if($user_cash >= $item_array['item_price']) { $item_price = '<span style="color: #00FF00;">' . $item_array['item_price'] . '</span>'; } else { $item_price = '<span style="color: #FF0000;">' . $item_array['item_price'] . '</span>'; } $item_quantity = $item_array['item_quantity']; if($item_quantity >= 1) { $item_in_stock = '<span style="color: #00FF00;">In Stock</span>'; } else { $item_in_stock = '<span style="color: #FF0000;">Sold Out</span>'; } print '<tr>'; print '<td> ' . $item_name . ' </td>'; print '<td width=125px> ' . $item_price . ' </td>'; print '<td width=125px> ' . $item_in_stock . ' </td>'; print '<td width=125px> ' . $item_quantity . ' </td>'; print '<td width=125px>'; print '<input type=checkbox name=selected value=' . $item_name . ' />'; print '</td>'; print '</tr>'; } print '</table>'; print '<input type=submit name=submit value="Buy Items">'; print '</form>'; } } else { print 'You must be logged in to view this page'; } ?> Shows The Table.. says out of stock if 0 green if in stock.. When i hit check it and hit buy. the table just disappers nothing happens Quote Link to comment Share on other sites More sharing options...
Deanznet Posted December 3, 2007 Author Share Posted December 3, 2007 Updated Code... change the mysql query from * to specfic things maybe? still dosent work <?php if($userdata['session_logged_in']) { $user_info_query = mysql_query("SELECT user_points FROM phpbb_users WHERE user_id='$user_id'"); $user_info_array = mysql_fetch_array($user_info_query); $user_cash = $user_info_array['user_points']; if($_POST['submit']) { if($_POST['action']=='buy') { foreach($_POST['selected'] as $itemID){ $price_query = mysql_query("SELECT item_price FROM items WHERE item_name='$itemID'"); $price_array = mysql_fetch_array($price_query); $price = $price_array['item_price']; $quantity_query = mysql_query("SELECT item_quantity FROM items WHERE item_name='$itemID'"); $quantity_array = mysql_fetch_array($quantity_query); $quantity = $quantity_array['item_quantity']; if($quantity >= 1) { $num_owned_query = mysql_query("SELECT item_quantity FROM user_item WHERE item_owner_id='$user_id' AND item_name='$itemID'"); $num_owned_array = mysql_fetch_array($num_owned_query); $num_owned = $num_owned_array['item_quantity']; if($user_cash >= $price_array['item_price']) { if($num_owned == 0) { if(mysql_query("INSERT INTO user_item (item_name, item_owner_id) VALUES ('$itemID', '$user_id')")) { $user_cash_new = $user_cash-$price_array['item_price']; if(mysql_query("UPDATE phpbb_users SET user_points='$user_cash_new' WHERE user_id='$user_id'")) { $quantity_new = $quantity-1; if(mysql_query("UPDATE items SET item_quantity='$quantity_new' WHERE item_name='$itemID'")) { print 'Item bought'; } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { $num_owned_new = $num_owned_array['item_quantity']+1; if(mysql_query("UPDATE user_item SET item_quanity='$num_owned_new' WHERE item_owner_id='$user_id' AND item_name='$itemID'")) { $user_cash_new = $user_cash-$price_array['item_price']; if(mysql_query("UPDATE phpbb_users SET user_points='$user_cash_new' WHERE user_id='$user_id'")) { $quantity_new = $quantity-1; if(mysql_query("UPDATE items SET item_quantity='$quantity_new' WHERE item_name='$itemID'")) { print 'Item bought'; } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { print 'Failed connecting to the server. Please contact the site admin.'; } } } else { print 'Item is out of stock'; } } else { print 'You do not have enough money'; } } } } else { $item_query = mysql_query("SELECT * FROM items"); print '<form action=' . $_SERVER['PHP_SELF'] . ' method=post>'; print '<table width=500px border=1>'; print '<tr>'; print '<td> Item Name </td>'; print '<td width=125px> Price </td>'; print '<td width=125px> In Stock </td>'; print '<td width=125px> Quantity </td>'; print '<td width=125px> Buy </td>'; while($item_array = mysql_fetch_array($item_query)) { $item_name = $item_array['item_name']; if($user_cash >= $item_array['item_price']) { $item_price = '<span style="color: #00FF00;">' . $item_array['item_price'] . '</span>'; } else { $item_price = '<span style="color: #FF0000;">' . $item_array['item_price'] . '</span>'; } $item_quantity = $item_array['item_quantity']; if($item_quantity >= 1) { $item_in_stock = '<span style="color: #00FF00;">In Stock</span>'; } else { $item_in_stock = '<span style="color: #FF0000;">Sold Out</span>'; } print '<tr>'; print '<td> ' . $item_name . ' </td>'; print '<td width=125px> ' . $item_price . ' </td>'; print '<td width=125px> ' . $item_in_stock . ' </td>'; print '<td width=125px> ' . $item_quantity . ' </td>'; print '<td width=125px>'; print '<input type=checkbox name=selected value=' . $item_name . ' />'; print '</td>'; print '</tr>'; } print '</table>'; print '<input type=submit name=submit value="Buy Items">'; print '</form>'; } } else { print 'You must be logged in to view this page'; } ?> and i changed something in this code but now it says i dont have enought money and i do. <?php if($userdata['session_logged_in']) { $user_info_query = mysql_query("SELECT user_points FROM phpbb_users WHERE user_id='$user_id'"); $user_info_array = mysql_fetch_array($user_info_query); $user_cash = $user_info_array['user_points']; if($_POST['submit']) { if($_POST['action']=='buy') { foreach($_POST['selected'] as $itemID){ $price_query = mysql_query("SELECT item_price FROM items WHERE item_name='$itemID'"); $price_array = mysql_fetch_array($price_query); $price = $price_array['item_price']; $quantity_query = mysql_query("SELECT item_quantity FROM items WHERE item_name='$itemID'"); $quantity_array = mysql_fetch_array($quantity_query); $quantity = $quantity_array['item_quantity']; if($quantity >= 1) { $num_owned_query = mysql_query("SELECT item_quantity FROM user_item WHERE item_owner_id='$user_id' AND item_name='$itemID'"); $num_owned_array = mysql_fetch_array($num_owned_query); $num_owned = $num_owned_array['item_quantity']; if($user_cash >= $price_array['item_price']) { if($num_owned == 0) { if(mysql_query("INSERT INTO user_item (item_name, item_owner_id) VALUES ('$itemID', '$user_id')")) { $user_cash_new = $user_cash-$price_array['item_price']; if(mysql_query("UPDATE phpbb_users SET user_points='$user_cash_new' WHERE user_id='$user_id'")) { $quantity_new = $quantity-1; if(mysql_query("UPDATE items SET item_quantity='$quantity_new' WHERE item_name='$itemID'")) { print 'Item bought'; } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { $num_owned_new = $num_owned_array['item_quantity']+1; if(mysql_query("UPDATE user_item SET item_quanity='$num_owned_new' WHERE item_owner_id='$user_id' AND item_name='$itemID'")) { $user_cash_new = $user_cash-$price_array['item_price']; if(mysql_query("UPDATE phpbb_users SET user_points='$user_cash_new' WHERE user_id='$user_id'")) { $quantity_new = $quantity-1; if(mysql_query("UPDATE items SET item_quantity='$quantity_new' WHERE item_name='$itemID'")) { print 'Item bought'; } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { print 'Failed connecting to the server. Please contact the site admin.'; } } } else { print 'Item is out of stock'; } } else { print 'You do not have enough money'; } } } } else { $item_query = mysql_query("SELECT * FROM items"); print '<form action=' . $_SERVER['PHP_SELF'] . ' method=post>'; print '<table width=500px border=1>'; print '<tr>'; print '<td> Item Name </td>'; print '<td width=125px> Price </td>'; print '<td width=125px> In Stock </td>'; print '<td width=125px> Quantity </td>'; print '<td width=125px> Buy </td>'; while($item_array = mysql_fetch_array($item_query)) { $item_name = $item_array['item_name']; if($user_cash >= $item_array['item_price']) { $item_price = '<span style="color: #00FF00;">' . $item_array['item_price'] . '</span>'; } else { $item_price = '<span style="color: #FF0000;">' . $item_array['item_price'] . '</span>'; } $item_quantity = $item_array['item_quantity']; if($item_quantity >= 1) { $item_in_stock = '<span style="color: #00FF00;">In Stock</span>'; } else { $item_in_stock = '<span style="color: #FF0000;">Sold Out</span>'; } print '<tr>'; print '<td> ' . $item_name . ' </td>'; print '<td width=125px> ' . $item_price . ' </td>'; print '<td width=125px> ' . $item_in_stock . ' </td>'; print '<td width=125px> ' . $item_quantity . ' </td>'; print '<td width=125px>'; print "<input type='checkbox' name='selected[]' value='{$get_info['item_name']}' />"; print '</td>'; print '</tr>'; } print '</table>'; echo '<select name="action">' .'<option value="buy">Buy</option>' .'</select><p>'; echo '<input type="submit" name="submit">'; echo '</form>'; } } else { print 'You must be logged in to view this page'; } ?> Quote Link to comment Share on other sites More sharing options...
mr_mind Posted December 3, 2007 Share Posted December 3, 2007 You made it so that the value for the item would post as nothing. I have fixed a couple of things including that it now checks to see if the item exists and prints how much money you have before where it tells you to buy. <?php if($userdata['session_logged_in']) { function item_exists($item); $item_exists_query = mysql_query("SELECT * FROM items WHERE item_name='$item'"); if(mysql_num_rows($item_exists_query)>0) { return TRUE; } else { return FALSE; } } $user_info_query = mysql_query("SELECT user_points FROM phpbb_users WHERE user_id='$user_id'"); $user_info_array = mysql_fetch_array($user_info_query); $user_cash = $user_info_array['user_points']; if($_POST['submit']) { if($_POST['action']=='buy') { if(item_exists($_POST['selected'])) { foreach($_POST['selected'] as $itemID){ $price_query = mysql_query("SELECT * FROM items WHERE item_name='$itemID'"); $price_array = mysql_fetch_array($price_query); $price = $price_array['item_price']; $quantity = $price_array['item_quantity']; if($quantity >= 1) { $num_owned_query = mysql_query("SELECT * FROM user_item WHERE item_owner_id='$user_id' AND item_name='$itemID'"); $num_owned_array = mysql_fetch_array($num_owned_query); $num_owned = $num_owned_array['item_quantity']; if($user_cash >= $price_array['item_price']) { if($num_owned == 0) { if(mysql_query("INSERT INTO user_item (item_name, item_owner_id) VALUES ('$itemID', '$user_id')")) { $user_cash_new = $user_cash-$price_array['item_price']; if(mysql_query("UPDATE phpbb_users SET user_points='$user_cash_new' WHERE user_id='$user_id'")) { $quantity_new = $quantity-1; if(mysql_query("UPDATE items SET item_quantity='$quantity_new' WHERE item_name='$itemID'")) { print 'Item bought'; } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { $num_owned_new = $num_owned_array['item_quantity']+1; if(mysql_query("UPDATE user_item SET item_quanity='$num_owned_new' WHERE item_owner_id='$user_id' AND item_name='$itemID'")) { $user_cash_new = $user_cash-$price_array['item_price']; if(mysql_query("UPDATE phpbb_users SET user_points='$user_cash_new' WHERE user_id='$user_id'")) { $quantity_new = $quantity-1; if(mysql_query("UPDATE items SET item_quantity='$quantity_new' WHERE item_name='$itemID'")) { print 'Item bought'; } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { print 'Failed connecting to the server. Please contact the site admin.'; } } } else { print 'Item is out of stock'; } } else { print 'You do not have enough money'; } } } else { print 'Item does not exists'; } } } else { $item_query = mysql_query("SELECT * FROM items"); print '<form action=' . $_SERVER['PHP_SELF'] . ' method=post>'; print '<table width=500px border=1>'; print '<tr>'; print '<td> Item Name </td>'; print '<td width=125px> Price </td>'; print '<td width=125px> In Stock </td>'; print '<td width=125px> Quantity </td>'; print '<td width=125px> Buy </td>'; while($item_array = mysql_fetch_array($item_query)) { $item_name = $item_array['item_name']; if($user_cash >= $item_array['item_price']) { $item_price = '<span style="color: #00FF00;">' . $item_array['item_price'] . '</span>'; } else { $item_price = '<span style="color: #FF0000;">' . $item_array['item_price'] . '</span>'; } $item_quantity = $item_array['item_quantity']; if($item_quantity >= 1) { $item_in_stock = '<span style="color: #00FF00;">In Stock</span>'; } else { $item_in_stock = '<span style="color: #FF0000;">Sold Out</span>'; } print '<tr>'; print '<td> ' . $item_name . ' </td>'; print '<td width=125px> ' . $item_price . ' </td>'; print '<td width=125px> ' . $item_in_stock . ' </td>'; print '<td width=125px> ' . $item_quantity . ' </td>'; print '<td width=125px>'; print '<input type="checkbox" name="selected" value="' . $item_name . '" />'; print '</td>'; print '</tr>'; } print '</table>'; print 'You currently have $' . $user_cash . '<br />'; print '<select name="action">'; print '<option value="buy">Buy</option>'; print '</select>'; print '<input type="submit" name="submit">'; print '</form>'; } } else { print 'You must be logged in to view this page'; } ?> Quote Link to comment Share on other sites More sharing options...
Deanznet Posted December 3, 2007 Author Share Posted December 3, 2007 Well now i get Warning: Invalid argument supplied for foreach() in /home/content/K/i/c/KickGame/html/tradeadmin.php on line 117 foreach($_POST['selected'] as $itemID){ Quote Link to comment Share on other sites More sharing options...
mr_mind Posted December 4, 2007 Share Posted December 4, 2007 Sorry try this <?php if($userdata['session_logged_in']) { function item_exists($item); $item_exists_query = mysql_query("SELECT * FROM items WHERE item_name='$item'"); if(mysql_num_rows($item_exists_query)>0) { return TRUE; } else { return FALSE; } } $user_info_query = mysql_query("SELECT user_points FROM phpbb_users WHERE user_id='$user_id'"); $user_info_array = mysql_fetch_array($user_info_query); $user_cash = $user_info_array['user_points']; if($_POST['submit']) { if($_POST['action']=='buy') { if(item_exists($_POST['selected'])) { $selected_array = $_POST['selected']; foreach($selected_array as $itemID){ $price_query = mysql_query("SELECT * FROM items WHERE item_name='$itemID'"); $price_array = mysql_fetch_array($price_query); $price = $price_array['item_price']; $quantity = $price_array['item_quantity']; if($quantity >= 1) { $num_owned_query = mysql_query("SELECT * FROM user_item WHERE item_owner_id='$user_id' AND item_name='$itemID'"); $num_owned_array = mysql_fetch_array($num_owned_query); $num_owned = $num_owned_array['item_quantity']; if($user_cash >= $price_array['item_price']) { if($num_owned == 0) { if(mysql_query("INSERT INTO user_item (item_name, item_owner_id) VALUES ('$itemID', '$user_id')")) { $user_cash_new = $user_cash-$price_array['item_price']; if(mysql_query("UPDATE phpbb_users SET user_points='$user_cash_new' WHERE user_id='$user_id'")) { $quantity_new = $quantity-1; if(mysql_query("UPDATE items SET item_quantity='$quantity_new' WHERE item_name='$itemID'")) { print 'Item bought'; } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { $num_owned_new = $num_owned_array['item_quantity']+1; if(mysql_query("UPDATE user_item SET item_quanity='$num_owned_new' WHERE item_owner_id='$user_id' AND item_name='$itemID'")) { $user_cash_new = $user_cash-$price_array['item_price']; if(mysql_query("UPDATE phpbb_users SET user_points='$user_cash_new' WHERE user_id='$user_id'")) { $quantity_new = $quantity-1; if(mysql_query("UPDATE items SET item_quantity='$quantity_new' WHERE item_name='$itemID'")) { print 'Item bought'; } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { print 'Failed connecting to the server. Please contact the site admin.'; } } } else { print 'Item is out of stock'; } } else { print 'You do not have enough money'; } } } else { print 'Item does not exists'; } } } else { $item_query = mysql_query("SELECT * FROM items"); print '<form action=' . $_SERVER['PHP_SELF'] . ' method=post>'; print '<table width=500px border=1>'; print '<tr>'; print '<td> Item Name </td>'; print '<td width=125px> Price </td>'; print '<td width=125px> In Stock </td>'; print '<td width=125px> Quantity </td>'; print '<td width=125px> Buy </td>'; while($item_array = mysql_fetch_array($item_query)) { $item_name = $item_array['item_name']; if($user_cash >= $item_array['item_price']) { $item_price = '<span style="color: #00FF00;">' . $item_array['item_price'] . '</span>'; } else { $item_price = '<span style="color: #FF0000;">' . $item_array['item_price'] . '</span>'; } $item_quantity = $item_array['item_quantity']; if($item_quantity >= 1) { $item_in_stock = '<span style="color: #00FF00;">In Stock</span>'; } else { $item_in_stock = '<span style="color: #FF0000;">Sold Out</span>'; } print '<tr>'; print '<td> ' . $item_name . ' </td>'; print '<td width=125px> ' . $item_price . ' </td>'; print '<td width=125px> ' . $item_in_stock . ' </td>'; print '<td width=125px> ' . $item_quantity . ' </td>'; print '<td width=125px>'; print '<input type="checkbox" name="selected[]" value="' . $item_name . '" />'; print '</td>'; print '</tr>'; } print '</table>'; print 'You currently have $' . $user_cash . '<br />'; print '<select name="action">'; print '<option value="buy">Buy</option>'; print '</select>'; print '<input type="submit" name="submit">'; print '</form>'; } } else { print 'You must be logged in to view this page'; } ?> Quote Link to comment Share on other sites More sharing options...
Deanznet Posted December 4, 2007 Author Share Posted December 4, 2007 I get item dosent exist. Think is has something to do with this function item_exists($item); dident work so i had to change it to function item_exists($item){ to make it work function item_exists($item){ $item_exists_query = mysql_query("SELECT * FROM items WHERE item_name='$itemID'"); if(mysql_num_rows($item_exists_query)>0) { return TRUE; } else { return FALSE; } } Quote Link to comment Share on other sites More sharing options...
Deanznet Posted December 4, 2007 Author Share Posted December 4, 2007 Anyone know i wanna get this part done by tonight if possible. Quote Link to comment Share on other sites More sharing options...
mr_mind Posted December 4, 2007 Share Posted December 4, 2007 oh yes you were right. what happens when you do that? Quote Link to comment Share on other sites More sharing options...
Deanznet Posted December 4, 2007 Author Share Posted December 4, 2007 I get Item does not exists Quote Link to comment Share on other sites More sharing options...
teng84 Posted December 4, 2007 Share Posted December 4, 2007 I get item dosent exist. Think is has something to do with this function item_exists($item); dident work so i had to change it to function item_exists($item){ to make it work function item_exists($item){ $item_exists_query = mysql_query("SELECT * FROM items WHERE item_name='$itemID'"); if(mysql_num_rows($item_exists_query)>0) { return TRUE; } else { return FALSE; } } unbelievable how come you get that result out of this function when all this function do is to return true or false!!!! Quote Link to comment Share on other sites More sharing options...
Deanznet Posted December 4, 2007 Author Share Posted December 4, 2007 Do what im confused Lol.. ??? Quote Link to comment Share on other sites More sharing options...
teng84 Posted December 4, 2007 Share Posted December 4, 2007 Do what im confused Lol.. ??? that code you posted (function) is boolean meaning it will only returns TRUE or FALSE based on that code! so its impossible to get that result because you can only have T/F on that function Quote Link to comment Share on other sites More sharing options...
mr_mind Posted December 4, 2007 Share Posted December 4, 2007 It should work because i have used that exact function before except for it was users and not items. Nevertheless i have changed it to return a number which should satisfy any doubts. If this does not work then try looking at the html output to see if everything is set correctly there I also noticed a bug that had it checking the posted value which was an array instead of the $itemID in the foreach which i fixed try this maybe <?php if($userdata['session_logged_in']) { function item_exists($item) { $item_exists_query = mysql_query("SELECT * FROM items WHERE item_name='$item'"); if(mysql_num_rows($item_exists_query)>0) { return '1'; } else { return '0'; } } $user_info_query = mysql_query("SELECT * FROM phpbb_users WHERE user_id='$user_id'"); $user_info_array = mysql_fetch_array($user_info_query); $user_cash = $user_info_array['user_points']; if($_POST['submit']) { if($_POST['action']=='buy') { $selected_array = $_POST['selected']; foreach($selected_array as $itemID){ if(item_exists($itemID)==1) { $price_query = mysql_query("SELECT * FROM items WHERE item_name='$itemID'"); $price_array = mysql_fetch_array($price_query); $price = $price_array['item_price']; $quantity = $price_array['item_quantity']; if($quantity >= 1) { $num_owned_query = mysql_query("SELECT * FROM user_item WHERE item_owner_id='$user_id' AND item_name='$itemID'"); $num_owned_array = mysql_fetch_array($num_owned_query); $num_owned = $num_owned_array['item_quantity']; if($user_cash >= $price_array['item_price']) { if($num_owned == 0) { if(mysql_query("INSERT INTO user_item (item_name, item_owner_id) VALUES ('$itemID', '$user_id')")) { $user_cash_new = $user_cash-$price_array['item_price']; if(mysql_query("UPDATE phpbb_users SET user_points='$user_cash_new' WHERE user_id='$user_id'")) { $quantity_new = $quantity-1; if(mysql_query("UPDATE items SET item_quantity='$quantity_new' WHERE item_name='$itemID'")) { print 'Item bought'; } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { $num_owned_new = $num_owned_array['item_quantity']+1; if(mysql_query("UPDATE user_item SET item_quanity='$num_owned_new' WHERE item_owner_id='$user_id' AND item_name='$itemID'")) { $user_cash_new = $user_cash-$price_array['item_price']; if(mysql_query("UPDATE phpbb_users SET user_points='$user_cash_new' WHERE user_id='$user_id'")) { $quantity_new = $quantity-1; if(mysql_query("UPDATE items SET item_quantity='$quantity_new' WHERE item_name='$itemID'")) { print 'Item bought'; } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { print 'Failed connecting to the server. Please contact the site admin.'; } } else { print 'Failed connecting to the server. Please contact the site admin.'; } } } else { print 'Item is out of stock'; } } else { print 'You do not have enough money'; } } else { print 'Item does not exists'; } } } } else { $item_query = mysql_query("SELECT * FROM items"); print '<form action=' . $_SERVER['PHP_SELF'] . ' method=post>'; print '<table width=500px border=1>'; print '<tr>'; print '<td> Item Name </td>'; print '<td width=125px> Price </td>'; print '<td width=125px> In Stock </td>'; print '<td width=125px> Quantity </td>'; print '<td width=125px> Buy </td>'; while($item_array = mysql_fetch_array($item_query)) { $item_name = $item_array['item_name']; if($user_cash >= $item_array['item_price']) { $item_price = '<span style="color: #00FF00;">' . $item_array['item_price'] . '</span>'; } else { $item_price = '<span style="color: #FF0000;">' . $item_array['item_price'] . '</span>'; } $item_quantity = $item_array['item_quantity']; if($item_quantity >= 1) { $item_in_stock = '<span style="color: #00FF00;">In Stock</span>'; } else { $item_in_stock = '<span style="color: #FF0000;">Sold Out</span>'; } print '<tr>'; print '<td> ' . $item_name . ' </td>'; print '<td width=125px> ' . $item_price . ' </td>'; print '<td width=125px> ' . $item_in_stock . ' </td>'; print '<td width=125px> ' . $item_quantity . ' </td>'; print '<td width=125px>'; print '<input type="checkbox" name="selected[]" value="' . $item_name . '" />'; print '</td>'; print '</tr>'; } print '</table>'; print 'You currently have $' . $user_cash . '<br />'; print '<select name="action">'; print '<option value="buy">Buy</option>'; print '</select>'; print '<input type="submit" name="submit">'; print '</form>'; } } else { print 'You must be logged in to view this page'; } ?> Quote Link to comment Share on other sites More sharing options...
Deanznet Posted December 4, 2007 Author Share Posted December 4, 2007 Well crap i did get this Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in line 125 Failed connecting to the server. Please contact the site admin. Than i added the @in front of the mysql_fetch_array looks like this now @mysql_fetch_array Now i just get Failed connecting to the server. Please contact the site admin. $num_owned_query = mysql_query("SELECT * FROM user_item WHERE item_owner_id='$user_id' AND item_name='$itemID'"); $num_owned_array = mysql_fetch_array($num_owned_query); Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.