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"> </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"> </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
44
Other forums
quick question
Hi ..
i have a question
how do i set a var so it displays via an echo
Code: $logo = '&a
problem with php server update from mid 2009
Hi,
I have this navigation menu on 2 websites which used to work just fine. After a recent up
Multiple includes losing variables
Hey all,
just starting out w php and ran into a problem pretty quickly. I'm
including seve
Parse Error
Hi Guys,
I have a function in my class which returns a string link variable. The problem is i
On page view, minus credit
Hello all, please, I need a little help with this script. I am charging one credit (credits can be p
SAP FICO learning materials
Hi all,
I am new to this world of SAP FICO. I have taken training on SAP FICO, but was wonderin
PHP IMAGE UPLOAD SCRIPT
Hi for the last week i have been looking for scripts that will upload a photo to a certain folder wh
phpmailer class & pop.gmail.com?
Code: <?php
$mail->IsSMTP();
$mail->Host = "pop.gmail.com";
max function question
Hi All
Ihave a table that holds shipment numbers and dates like this
select * from
upload form file types....
Hey all, I am learning PHP and I am writing a script from the W3C Schools tutorials for uploading fi