Jump to content

Recommended Posts

Hey guys I've got this script which I've been coding which allows users to stock cars.. I've been testing it as I code it in which all the error messages display at the right time work on hitting submit but when it comes to the time to update the database it just refreshes the page and don't do anything. I cannot work out the reason why?

elseif (is_numeric($BuyCars)){
 
// Form has value, form is ALSO a number
 
// Now get cost of the car to stock and how many they want
 
// $Stocked is CarName 
 
$CarInfo = $db->prepare("SELECT * FROM `cars` WHERE carname = :carname");
$CarInfo->bindParam(":carname", $Stocked);
$CarInfo->execute();
$CarReal = $CarInfo->rowCount();
 
 
 
$GetTheStockNow = $db->prepare("SELECT * FROM `Dealercars` WHERE location = :location AND carname = :carname");
$GetTheStockNow->bindParam(":location", $DeLoc);
$GetTheStockNow->bindParam(":carname", $Stocked);
$GetTheStockNow->execute();
$GetTheThe = $GetTheStockNow->fetchObject();
 
if ($CarReal != "1"){
 
echo "Failed finding car";
 
}elseif ($CarReal >= "1"){
 
// Car Found
 
$OldCarStats = $CarInfo->fetchObject(); 
 
$BuyPrice = $OldCarStats->value; // Value of Car from defult value
 
$CostOfBuy = $BuyPrice * $BuyCars; // Cost of stock
 
$Money = $UsersTable1->money; // $Usernames money
 
// Has the $Username got enough money to buy the stock?
 
if ($UsersTable1->money < $CostOfBuy){
 
echo "You dont have enough money to buy this amount of stock!";
 
}elseif ($UsersTable1->money >= $CostOfBuy){ 
 
$StuffStock = $GetTheThe->stock;
 
$NewMoney = $Money - $CostOfBuy; // Money after buying stock
 
$NewStock = $StuffStock + $BuyCars; // The New Stock
 
 
// Take the money from user for buying stock 
$TakeMoneyAway = $db->prepare("UPDATE `UsersRegTable` SET money = :money WHERE username = :username");
$TakeMoneyAway->bindValue(':money', $NewMoney);
$TakeMoneyAway->bindValue(':username', $Username);
$TakeMoneyAway->execute();
 
// Update thier car dealership with the stock they just brought
 
$UpdateStock = $db->prepare("UPDATE `Dealercars` SET stock = :stock WHERE location = :location AND carname = :carname");
$UpdateStock->bindValue(':stock', $NewStock);
$UpdateStock->bindValue(':carname', $Stocked);
$UpdateStock->bindValue(':location', $DeLoc);
$UpdateStock->execute(); 
 
 
echo "You successfully brought ".htmlspecialchars(number_format($BuyCars))." for £ ".htmlspecialchars(number_format($CostOfBuy))."";
 
 
   }

Am I missing something very obvious here ?

 

Thanks for any help given

Link to comment
https://forums.phpfreaks.com/topic/302502-form-working-but-only-to-an-extent/
Share on other sites

what debugging have you done to find out WHAT the code IS doing?

 

do you have php's error_reporting set to E_ALL and display_errors set to ON (in the php.ini on your development system) so that php would help you by reporting and displaying all the errors it detects?

 

are you sure the elseif() statement being shown is true and that no prior related if() or elseif() statement was true so that the shown elseif() will even be executed?

 

do you have the PDO error mode set to exceptions so that database errors will throw an exception, combined with the suggested php error_reporting/display_errors settings, so you will see any database errors?

 

is there a header() redirect later in the code that could be redirecting back to the same page, combined with php's output_buffering being on, that would hide any php errors or any output from echo statements in your code?

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.