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:

shop.php
<?php
session_start(); // start up your PHP session!
?>
<?php require ("header.php");?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<script src="selectuser.js"></script>
<link rel="stylesheet" type="text/css" href="css/layout.css">
</head>
<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" type="text/javascript"></script>
</body>

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

<!--- PAGE CONTENT --->
<div class="shop">
<div align="center">
<center>
<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#000000" width="95%" id="AutoNumber1" bgcolor="#131313">
<tr>
<td width="100%" colspan="5">&nbsp;</td>
</tr>
<tr>
<td width="100%" colspan="5"> </td>
</tr>
<?php
$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
$x=0;
while ($x<$numrows){ //loop through the records

$theid=mysql_result($results,$x,$id);
$thename=mysql_result($results,$x,$name);
$thecost=mysql_result($results,$x,$cost);
$themax=mysql_result($results,$x,$max);
$thedbname=mysql_result($results,$x,$dbname);
$theatt=mysql_result($results,$x,$att);
$thedef=mysql_result($results,$x,$def);
?>
<tr>
<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">
<form>
<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>
</select>
<a href="#" onClick="showUser('<?=$thedbname?>')">BUY</a>
</td>
</form>
</tr>
<?
$x++;
}
}
?>
<tr>
<td width="100%" colspan="5">&nbsp;</td>
</tr>
<tr>
<td width="100%" colspan="5">
<p align="center"><b>Total Attack: <?=number_format($attack)?></b></td>
</tr>
</table>
</center>
</div>
</div>

<!--- END WRAPPER DIV --->
</div>
</html>


selectuser.js:
Code: var xmlHttp;

function showUser(str)
{
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request");
return;
}
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";
url=url+"?q="+quantity;
url=url+"&myid="+myid;
url=url+"&cost="+cost;
url=url+"&dbname="+str;
url=url+"&attack="+attack;
url=url+"&defense="+defense;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

function stateChanged()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
}
}

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

$id=$_GET['myid'];
$cost=$_GET['cost'];
$quantity=$_GET['q'];
$dbname=$_GET['dbname'];
$attack=$_GET['attack'];
$defense=$_GET['defense'];

$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");
die();
}
$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'";
mysql_query($sql);

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

mysql_close($con);
?>

This is really annoying and would appreciate any help.

Thanks

No comments posted yet

Your Answer:

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

dropdown box help - open php files to textarea
Hi, I am using tinymce to edit content located in several php files. The code I attached works but i

Backflush
I am looking for a report in SAP that would give me a list of all parts that are populated with a ba

Pop-up Banner
Hello Friends,I need to use banner in our client site.Example: www.example.com if i enter this site

question about header() security
is is safe to just use the header() function to redirect someone if they are, say, not logged in? or

2 things: enter doesn't work in IE & empty form
i have this search form that works well except for 2 things.

1) on IE when i hit enter instea

Data storage spaces in varchar2
Trying to understand what's happening.
I am selecting a value from a table that is defined as c

single values into sql field
i have a form which i want the user to able to select multiple answers. But i want the answers to be

Syntax Help
I'm trying to make a form that edits a php file which contains config settings. I'm using strings t

MSSQL/PHP
I am tryint to setup a webapplication developed in PHP, Apache2.0 and SQLServer2000 as backend which

foreach help.
Hello all,

First time I'm attempting to use a foreach statement and was just wondering if my

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