View Full Version : php and mysql multiple queries
kneuf 05-28-2003, 09:43 PM I've been trying for some time now to get this working. What I'm trying to do is first check how many points the current user has, than check how many points the item costs. It the user has enough points, they can buy it. If not, return a error message. I've posted a topic on this here before, maybe I didn't post enough information.
Joe Bonanno 05-29-2003, 12:30 AM Since you didn't tell us how many tables are used I am going to assume 2.
Hopefully this example will set you in the right direction. In this example I would assume you are passing $youruserid from a session variable and passing $itemnumber from an order form or shopping cart.
<?php
$query = "SELECT user.points, user.anyotherinfo, item.number, item.points, item.anyotherinfo FROM user, item WHERE user.userid = '$youruserid' AND item.number = '$itemnumber' AND item.points <= user.points";
$result = mysql_query($query);
$num_results = mysql_num_rows($result);
if ($num_resluts > 0) {
//display page
}
else {
echo "You do not have enough points to purchase this item";
}
?>
kneuf 05-30-2003, 08:20 AM Yeah, it sorta works. If the person doesn't have enough points, they can still buy it. And, I can't figure out how to subtract the item points from the persons points. Thanks.
ilyash 05-30-2003, 05:50 PM 2 queries:
one: select POINTS FROM TABLE
int NEWPOINTS = POINTS - COST OF PRODUCT
TWO: INSERT INTO TABLE NEWPOINTS
sumtin like that (replace my variables wit the appropriate names)
kneuf 05-31-2003, 02:58 PM OK, so far so good. But ilyash's post with the "int" in it, where's it supposed to go? I tryied it, and it just gave me an error something like "You have an error in your SQL sytanx near int"
plugged 05-31-2003, 03:32 PM it's 2 statements, like:
$sqlstr = "SELECT POINTS FROM TABLE";
// process it.. return $points
$newpoints = $points - $COST_OF_PRODUCT;
if ($newpoints > 0)
// user has enough points..
ilyash 05-31-2003, 04:14 PM i meant integer.. its not actual code.. its the logic
kneuf 05-31-2003, 04:17 PM well, isn't there a way to start a variable inside a SQL statement?
ilyash 05-31-2003, 04:21 PM i think there is.. but the next sql statement wouldnt have the integer
kneuf 05-31-2003, 05:00 PM OK. It works, but whenever the person buys it, the amount the user has turns to 1. Here's the code:
$query1 = "SELECT price FROM product WHERE id = '$id'";
$query2 = "SELECT point FROM point WHERE username = '$username";
$price = @mysql_query($query1) or die("Error at query 1: " . mysql_error());
$current = @mysql_query($query2) or die("Error at query 2: " . mysql_error());
$newpoints = $current - $price;
@mysql_query("UPDATE point SET point='$newpoints' WHERE username='$username'") or die("UPDATE FAILED: " . mysql_error());
kneuf 05-31-2003, 05:22 PM I figured it out, I used mysql_result.
Joe Bonanno 05-31-2003, 05:47 PM My opinion. You are going backwards. Back to your post 2 posts up from this.
You were getting one as a result because you are not retrieving the values properly.
$newpoints = $current - $price;
is doing this
$newpoints = resultidentifier2 - relsultidentifier1 = 1
try:
$varaible = mysql_result($price);
$varaible2 = mysql_result($current);
$newpoints = $variable2 - $variable;
You probably are going to want to insert some code to verify that you are only getting one result per SELECT after you get this working, otherwise use mysql_fetch_array instead of mysql_result.
Joe Bonanno 05-31-2003, 05:55 PM :gthumb:
Guess it took me too long to write that last reply.:)
|