Store questions (Resolved)

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Store questions (Resolved)

Post 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>
Last edited by Epiales on Fri Aug 23, 2013 3:45 pm, edited 1 time in total.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Store questions

Post 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.
The indelible lord of tl;dr
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Store questions

Post 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.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: Store questions

Post 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 );
 
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Store questions

Post 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.
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Winawer
Posts: 180
Joined: Wed Aug 17, 2011 5:53 am

Re: Store questions

Post by Winawer »

You're running the wrong query.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Store questions

Post 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:
The indelible lord of tl;dr
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Store questions

Post 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?
Nothing fancy, but a work in progress!

http://gameplaytoday.net
User avatar
MikuzA
Posts: 394
Joined: Thu Aug 08, 2013 8:57 am

Re: Store questions

Post 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')";
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
User avatar
Epiales
Posts: 1119
Joined: Thu Aug 15, 2013 1:38 am

Re: Store questions

Post 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
Nothing fancy, but a work in progress!

http://gameplaytoday.net
Post Reply

Return to “Beginner Help and Support”