php help

Associate
Joined
4 Mar 2010
Posts
206
hi guys

im making a ticket order system but having a problem. the problem is that when i enter the quantity it gives me an error saying there are not enough tickets. in the table ive set the ticket amount to 1000. im looking for it to subtract the quantity from the amount in the table

heres a picture:
UhTF9.jpg


i have 3 tables. packages, bookings and users

p.s. im a noob at coding lol

http://www.jhscott.net46.net/services.php

that is the link to the page.



PHP:
$packageselect = $_POST['packageselect'];
//echo $packageselect;
$orderedtickets = $_POST['orderedtickets'];
//echo $orderedtickets;
$username = $_COOKIE["username"];
//echo $username;


$con = mysql_connect("host","mylogin","dsadsadqweqwe");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
  
  mysql_select_db("a7438072_users", $con);
  // select firstname from user table
  $getname = mysql_query("SELECT firstname FROM users
WHERE username ='$username'");

while($row = mysql_fetch_array($getname))
  {
 $firstname = $row['firstname'];
  //echo $firstname;
  }
  
  //// get surname from table
  $getname = mysql_query("SELECT surname FROM users
WHERE username ='$username'");
//
while($row = mysql_fetch_array($getname))
  {
 $surname = $row['surname'];
//  echo $surname;
  }
  
 
  
  //echo $firstname;
 // echo $surname;
  //echo $packageselect;
  //echo $orderedtickets;
   
// check there is enough tickets

 
 //select total number of tickts to subtract from
  $result = mysql_query("SELECT total FROM package
WHERE package ='$packageselect'");

while($row = mysql_fetch_array($result))
  {
 $totaltickets = $row['total'];
  }
  
  
  if ($orderedtickets > $totaltickets){
	echo " sorry there are not enough tickets left";
}else {
	 

// subtracting tickets ordered from the total
$ticketsremaining = $totaltickets - $orderedtickets;
//echo $ticketsremaining;

//put the new totoal in the package table

mysql_query("UPDATE package SET total = '$ticketsremaining'
WHERE package = '$packageselect'");

// put details into booked table

mysql_query ("INSERT INTO bookings (firstname, lastname, package, tickets)
VALUES ('$firstname', '$surname','$packageselect', '$orderedtickets')");

header('Location:orderthanks.html');
}
?>
 
Just knocked up a test page (http://listen-to.me.uk/total.php) of your code and it appears to work correctly ie: it subtracts the order quantity from the 'package' total until order is greater than total then it displays the error.

I'd make sure you're getting the correct results from your $_POST variables; I did notice that you use 'packageselect' in your code here and 'bandselect' on your website ($packageselect would be NULL in this case).

Also i'd enclose everything below the "// check there is enough tickets" comment in an IF Condition that checks for form submitting (and i'd also check that $_POST variables are set and have a value) ie: -
Code:
if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
//code
}

And unless you're getting multiple rows for $totaltickets, you should limit the rows to 1 ie: -
Code:
"SELECT `total` FROM `package` WHERE `package` = '$packageselect' LIMIT 1"
You could also move the whole lot to a single line and remove the while loop -
Code:
$totaltickets = mysql_result(mysql_query("SELECT `total` FROM `package` WHERE `package` = '$packageselect' LIMIT 1"), 0, 0);

I didn't see it in your code, but make sure you close the mysql connection once you finished with it.
 
Thanks for the reply, the bandselect part was from my first attempt when i was creating a ticketsystem in college for a band so i changed the names for this site. ill see if i can get it working later on tonight. ill keep you updated.

thanks!
 
Not sure if this much help, but this is the code for my demo page -
Code:
<?php
// DB Connection
$con = mysql_connect("localhost", "username", "password") or die(mysql_error());
$db = mysql_select_db("database") or die(mysql_error());

// **** Main Code ****
// Form Variables
$packageselect = $_POST['bandselect'];
$orderedtickets = $_POST['orderedtickets'];

// Update Package
// IF Form Submit
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    //Get Total Tickets for Package
    $totaltickets = mysql_result(mysql_query("SELECT `total` FROM `package` WHERE `package` = '$packageselect' LIMIT 1"), 0, 0);

    if ($orderedtickets > $totaltickets){
        echo "sorry there are not enough tickets left";
    } else {
        //Update Tickets
        $ticketsremaining = $totaltickets - $orderedtickets;
        mysql_query("UPDATE `package` SET `total` = '$ticketsremaining' WHERE `package` = '$packageselect'");
    }
}
// **** END Main Code ****

// Package Results (for Table)
$package_results = mysql_query("SELECT `package`, `date`, `total` FROM `package`");

// Close DB Connection
mysql_close($con);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
  <title></title>
</head>

<body>
<!-- DEBUG INFO -->
<H3>Package Selected - <?=$packageselect?> / Ordered - <?=$orderedtickets?></H3>
<table align="center" width="300" cellspacing="1">
    <?php
    while($row = mysql_fetch_array($package_results)) {
    ?>
    <tr>
        <td><?=$row['package']?></td>
        <td><?=$row['date']?></td>
        <td><?=$row['total']?></td>
    </tr>
    <?php
    }
    ?>
</table>
<!-- END DEBUG -->

<br /><br />

<!-- FORM -->
<form id="form1" name="form1" method="post" action="total.php">
<label> Package:
    <select name="bandselect">
        <option value="deluxe">Deluxe</option>
        <option value="premium">Premium</option>
        <option value="basic">Basic</option>
    </select>
</label>
<label>Quantity: </label><label><input type="text" name="orderedtickets" id="orderedtickets" /></label>
<input type="submit" value="Order" />
</form>
<!-- END FORM -->

</body>
</html>
 
Do a google on 'PHP login scripts' (or something similar), plenty of code around.
Basic gist is login page, check credentials against a DB table (make sure passwords are hashed, don't use MD5 though), if correct set a session (eg: $_SESSION['loggedin'] = 1) variable (redirect to another page perhaps), then on any page you want to secure you check for that session variable. If that session variable doesn't exist or doesn't return the value you're after then redirect the user to the login page etc
Obviously you can make it as basic or advanced (could look at secure PHP logins using javascript for client side hashing something like this) as you want.
 
Back
Top Bottom