Page 1 of 1

Store questions (Resolved)

Posted: Fri Aug 23, 2013 7:29 am
by Epiales
Okay, now I'mma gonna work on the store LOL! Hope I'm not wearing anyone out :D

I'm getting the message that it's unable to add to my backpack when I purchase an item. Here's me code:

Code: Select all

<?php
session_start();
include("header.php");
if(!isset($_SESSION['uid'])){
 $id=$_SESSION['id'];
    echo "You must be logged in to view this page!";
}else{


$randid=$_GET['randid'];

?>
<div id="table">
<?php

$id = $user['id'];

$iteminfo="SELECT * from store where randid='$randid'";
$iteminfo2=mysql_query($iteminfo) or die("could not get item stats!");
$iteminfo3=mysql_fetch_array($iteminfo2);

if ($stats['gold'] < $iteminfo3['price'])
{
 echo "You do not have enough Gold for this purchase!";
  echo "<center><a href='index.php'>Go Back</center>";
  exit;
}


$name = $iteminfo3['name'];
$stats1 = $iteminfo3['stats'];
$statadd = $iteminfo3['statadd'];
$type = $iteminfo3['type'];
$randid2 = rand(1000,999999999);

$itembought = "INSERT into inventory(id, name, stats, statadd, randid,type) VALUES ('$id','$name','$stats1','$statadd','$randid2','$type')";
mysql_query($itembought) or die("could not insert item into backpack");

$updateplayer="update stats set gold=gold-'$iteminfo3[price]' where id='$id'";
  mysql_query($updateplayer) or die("Could not update player");

echo $name . " Purchased";
 echo "<center><a href='index.php'>Go Back</center>";
  ////did not make a - counter for item////
  }
  ?>
 </div>

Re: Store questions

Posted: Fri Aug 23, 2013 7:44 am
by Jackolantern
What is the exact thing you are seeing in the browser? The "could not insert item into backpack" message?

If so, change that part of the code to this:

Code: Select all

$iteminfo2=mysql_query($iteminfo);
echo mysql_errno().": ".mysql_error(); 
That will give you a much better error message than a simple die() will.

Re: Store questions

Posted: Fri Aug 23, 2013 7:46 am
by Epiales
Thanks. It says 0 minor healing purhcased

I have one in the store and it shows in the store. The only thing I might see wrong is that the randid field in table store shows 0? Also, it's taking my gold, but getting nothing.

Re: Store questions

Posted: Fri Aug 23, 2013 8:10 am
by Winawer
Do the same thing after the insert query, since that's the one that seems to be failing. You should also echo the query to see that it looks like expected. Something like

Code: Select all

$queryThatIsFailing = "INSERT INTO ...";
mysql_query( $queryThatIsFailing ) or die( mysql_error() . ': ' . $queryThatIsFailing );
 

Re: Store questions

Posted: Fri Aug 23, 2013 8:20 am
by Epiales
No matter how I do it, I only see: 0: Minor Healing Purchased

This is what I currently have for the code in question

Code: Select all

$name = $iteminfo3['name'];
$stats1 = $iteminfo3['stats'];
$statadd = $iteminfo3['statadd'];
$type = $iteminfo3['type'];
$randid2 = rand(1000,999999999);

$itembought = "INSERT into inventory(id, name, stats, statadd, randid,type) VALUES ('$id','$name','$stats1','$statadd','$randid2','$type')";
$iteminfo2=mysql_query($iteminfo);
echo mysql_errno().": ".mysql_error(); 

$updateplayer="update stats set gold=gold-'$iteminfo3[price]' where id='$id'";
  mysql_query($updateplayer) or die("Could not update player");

echo $name . " Purchased";
 echo "<center><a href='index.php'>Go Back</center>";
  ////did not make a - counter for item////
  }
  ?>
 </div>
I changed $stats to $stats1, as I have conflicting tables with that name. I also changed the $playerid to $id, as I don't use $playerid. My assumption is that is where the problem lies. I do have id defined above as:

Code: Select all

$id = $user['id']; 
So I'm sure there's a conflict going on here. Just not sure how to fix it. It works up to the point of taking my gold, but then the update is not working. My user Id is from another table called users.

Re: Store questions

Posted: Fri Aug 23, 2013 8:22 am
by Winawer
You're running the wrong query.

Re: Store questions

Posted: Fri Aug 23, 2013 8:29 am
by Jackolantern
Yep, you are running $iteminfo instead of $itembought.

Also, do not put single-quotes around anything in a query that is going to be a number. You would only need to put single-quotes around text values in queries. You probably need to change:

Code: Select all

$itembought = "INSERT into inventory(id, name, stats, statadd, randid,type) VALUES ('$id','$name','$stats1','$statadd','$randid2','$type')";
and

Code: Select all

$updateplayer="update stats set gold=gold-'$iteminfo3[price]' where id='$id'";
You need to review each of the variable values and remove the single-quotes on any that contain numeric values. Leaving them in will cause unstable queries, as sometimes MySQL is able to convert them, and sometimes it can't.

EDIT: Also be sure to change your code tags from

Code: Select all

 to [code=php ]. That will make finding errors much easier on the forums. I updated your previous post so you can see what it looks like :cool:

Re: Store questions

Posted: Fri Aug 23, 2013 8:46 am
by Epiales
Ah, so here is the problem: 1062: Duplicate entry '1' for key 'PRIMARY'Minor Healing Purchased

I have already removed and re added, but what do I put in the randid spot?

Re: Store questions

Posted: Fri Aug 23, 2013 8:51 am
by MikuzA
I bet your first ID is primary key and hopefully AUTO_INCREMENT? therefore leave it out of the insert.

Code: Select all

$itembought = "INSERT into inventory(name, stats, statadd, randid,type) VALUES ('$name','$stats1','$statadd','$randid2','$type')";

Re: Store questions

Posted: Fri Aug 23, 2013 9:06 am
by Epiales
MikuzA wrote:I bet your first ID is primary key and hopefully AUTO_INCREMENT? therefore leave it out of the insert.

Code: Select all

$itembought = "INSERT into inventory(name, stats, statadd, randid,type) VALUES ('$name','$stats1','$statadd','$randid2','$type')";
The reason is that the id will be the players id that owns the healing item. I have done it your way and it now shows that the id = 0 whenever anything is bought. Which belongs to nobody. IT's working, but shows 0 id in the inventory