Jump to content

php/mysql problem


richarro1234

Recommended Posts

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:

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

Rich

Link to comment
Share on other sites

First problem is that you cant have multiple elements on a page with the same id. Second, your select box "quantity" has nothing to tell which item the quantity is for.  Change the id of your elements to something unique like changing it to

<select name="quantity" id="quantity_<?=$theid?>" >
<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>

 

Then change your js to do

document.getElementById('quantity' + myid).value;

Link to comment
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.