php/mysql problem

Posted on 16th Feb 2014 07:03 pm by admin

Hey all,

Problem: im trying to setup a shop where people can use a drop-down list to select the amount of items they would like to purchase, then when they click on the "buy" button it uses ajax to update the database with the amount they selected.
The problem is it will only get the number in the first drop down list. so if the first list is 1 and you select 5 in the second box (because you want 5 of the second item) it will only add one of the first item.
As the script stands at the moemnt, it will add whatever is in the first dropdown box, but will add the right item, so even if i chose 5 item2's it will only add 1 item2 at the price for item1.

I have worked out how to solve THAT problem, where the function showUser() is i can add strings in there (showUser(itemname,cost,etc etc)) but then it doesnt get the quantity of the item so will still only add one of the item.

Can someone please help me solve this issue.
Here are my pages:

session_start(); // start up your PHP session!
<?php require ("header.php");?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="" xmlns:fb="">
<script src="selectuser.js"></script>
<link rel="stylesheet" type="text/css" href="css/layout.css">
<script src="" type="text/javascript"></script>

<div id="txtHint"></div>

<!--- PAGE CONTENT --->
<div class="shop">
<div align="center">
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#000000" width="95%" id="AutoNumber1" bgcolor="#131313">
<td width="100%" colspan="5">&nbsp;</td>
<td width="100%" colspan="5"> </td>
$results= mysql_query("SELECT * FROM weapons WHERE type = 'att' Order by cost asc");
$id = "id";
$name = "name";
$cost = "cost";
$max = "max";
$dbname = "dbname";
$att = "att";
$def = "def";
echo mysql_error();

if (mysql_Numrows($results)>0) //if there are records in the fields
$numrows=mysql_NumRows($results); //count them
while ($x<$numrows){ //loop through the records

<td width="25%"><?=$thename?></td>
<td width="25%" align="center">$<?=number_format($thecost)?></td>
<td width="25%" align="center">attack: <?=number_format($theatt)?> | Defense: <?=number_format($thedef)?></td>
<td width="25%" align="center">
<input type='hidden' id='myid' value='<?=$fbuid?>' />
<input type='hidden' id='cost' value='<?=$thecost?>' />
<input type='hidden' id='attack' value='<?=$theatt?>' />
<input type='hidden' id='defense' value='<?=$thedef?>' />
<input type='hidden' id='dbname' value='<?=$thedbname?>' />
<select name="quantity" id="quantity">
<option value="1">1</option>
<option value="5">5</option>
<option value="10">10</option>
<option value="25">25</option>
<option value="50">50</option>
<option value="100">100</option>
<a href="#" onClick="showUser('<?=$thedbname?>')">BUY</a>
<td width="100%" colspan="5">&nbsp;</td>
<td width="100%" colspan="5">
<p align="center"><b>Total Attack: <?=number_format($attack)?></b></td>

<!--- END WRAPPER DIV --->

Code: var xmlHttp;

function showUser(str)
if (xmlHttp==null)
alert ("Browser does not support HTTP Request");
var myid = document.getElementById('myid').value;
var quantity = document.getElementById('quantity').value;
var cost = document.getElementById('cost').value;
var attack = document.getElementById('attack').value;
var defense = document.getElementById('defense').value;

var url="ajx/shopbuy.php";

function stateChanged()
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")

function GetXmlHttpObject()
var xmlHttp=null;
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
catch (e)
//Internet Explorer
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
catch (e)
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
return xmlHttp;
and the file that updates the database shopbuy.php:
$con = mysql_connect('localhost', 'username', 'pasword');
if (!$con)
die('Could not connect: ' . mysql_error());
mysql_select_db("game", $con);


$totcost = ($quantity*$cost);

$query = mysql_query("SELECT * from users WHERE fbuser_id = '$id'");
while ($s = mysql_fetch_array($query)) {
$mymoney = $s['money'];

if ($mymoney<$totcost) {
header ("Location: ../shop.php?error=2");
$totatt = ($attack*$quantity);
$totdef = ($defense*$quantity);

$sql = "UPDATE users SET money=money-$totcost, attack=attack+$totatt, defense=defense+$totdef, $dbname=$dbname+$quantity WHERE fbuser_id = '$id'";

echo "<div class="rewards"><span class="rewards_title">Purchased $quantity $dbname for $totcost </span></div>";


This is really annoying and would appreciate any help.


No comments posted yet

Your Answer:

Login to answer
194 Like 44 Dislike
Previous forums Next forums
Other forums

Revoking alter any table from a schema
Hi All,

i am working in Oracle 10g.
my requirement is to revoke all kind of DDL oper

Mysterious Timeouts
I've deployed a few simple AJAX-enabled web page to a local server that is accessed through the Inte

Sum of Values in an Array
This is probably really simple... but it's been years since I've written anything, so bare with me!<

help with insert
Hi there,

I am trying to insert some data into a database, but for some reason, it is not ins

Include a php

I'm trying to include a function that shows the recent searches.
Example I have <

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS

Play Free Quiz and Win Cash