Jump to content


Photo

Subtracting with php?


  • Please log in to reply
4 replies to this topic

#1 hdshngout

hdshngout
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 06 August 2006 - 07:56 PM

Ok, im working on a new site and I'm working on another feature. What its meant to do is from a set of radio buttons, you choose somthing and it subtracts X amount from your points depending on the choice. Sounds easy, right? Well of course when I mess with it, it won't work. Anyone see and problems that would cause it not to subtract?

<?php if (isset($_POST['submit'])) {
function cleanUp($input)
{
  return htmlspecialchars(strip_tags(stripslashes(trim($input))));
}

$shop_title = cleanUp($_POST['shop_name']);
$shop_front = $_POST['shop_front'];
$shop_front = str_replace("<", "[", $shop_front);
$shop_front = str_replace(">", "]", $shop_front);
$shop_desc = cleanUp($_POST['shop_desc']);
$shop_desc = nl2br($shop_desc);
$shop_size = cleanUp($_POST['shop_size']);
$shop_location = cleanUP($_POST['shop_location']);
$timestamp = strtotime("now");
$username = $logged['username'];

if ( (empty($shop_title)) || (empty($shop_desc)) || (empty($shop_size)) ||

(empty($shop_location))) {

include($_SERVER['DOCUMENT_ROOT']."/connect.php");
$sql2 = "SELECT * FROM shops WHERE owner = '$logged[username]'";
$result2 = mysql_query($sql2) or print ("Can't select table.<p>" .

mysql_query());
$number = mysql_num_rows($result2);

if ($number != 1) {

include("../connect.php");
$sql = "INSERT INTO shops (timestamp, owner, shop_name, shop_desc,

shop_front, shop_size, Shop_location) VALUES

('$timestamp','$username','$shop_title','$shop_desc','$shop_front','$shop_

size','$shop_location')";

$result = mysql_query( $sql ) or print ( mysql_error());
$id = mysql_insert_id();

$shop_size2 = $_POST['shop_size'];

if ($shop_size2 == "4") {
$price = "50";
} else if ($shop_size2 == "8") {
$price = "100";
} else if ($shop_size2 == "12") {
$price = "200";
} else if ($shop_size2 == "16") {
$price = "300";
} else if ($shop_size2 == "20") {
$price = "400";
} else if ($shop_size2 == "60") {
$price = "600";
}


include($_SERVER['DOCUMENT_ROOT']."/connect.php");
$sql4 = "SELECT * FROM users WHERE username = '$logged[username]'";
$result4 = mysql_query($sql4) or print ("Can't select table.<p>" . mysql_query());
while($row4 = mysql_fetch_array($result4)) {
$points = $row4['points'];
}


$equation = ($points - $price);

include("../connect.php");
$sql5 = "UPDATE users SET points='$equation' WHERE

username='$logged[username]' LIMIT 1";
$result5 = mysql_query($sql5) or print (mysql_error());
if ($result5 != false) {

echo "<span style=\"color: green;\">Success!  Your shop has been

successfully created!It is located at <p><a

href=\"shop.php?id=$id\">$address/shop.php?id=$id</a></span>";

}
} else {
echo "<span style=\"color: red;\">You Have already created a

shop!</span>";
}
} else {
echo "<span style=\"color: red;\">You must fill in all the required

fields.</span>";
}
}
?>

(by the way, I'm sorry if I accidentally broke any of the rules, still new here and learning my way around)

#2 ignace

ignace
  • Moderators
  • Now mod flavored
  • 6,430 posts
  • LocationBelgium

Posted 06 August 2006 - 08:09 PM

i like your sense of humor ;D however what do you want to be substracted?

<?php if (isset($_POST['submit'])) {
function cleanUp($input)
{
  return htmlspecialchars(strip_tags(stripslashes(trim($input))));// well no-one ever gets passed this :D
}

$shop_title = cleanUp($_POST['shop_name']);
$shop_front = $_POST['shop_front'];
$shop_front = str_replace("<", "[", $shop_front);
$shop_front = str_replace(">", "]", $shop_front);
$shop_desc = cleanUp($_POST['shop_desc']);
$shop_desc = nl2br($shop_desc);
$shop_size = cleanUp($_POST['shop_size']);
$shop_location = cleanUP($_POST['shop_location']);
$timestamp = strtotime("now");
$username = $logged['username'];

if ( (empty($shop_title)) || (empty($shop_desc)) || (empty($shop_size)) ||

(empty($shop_location))) {

include($_SERVER['DOCUMENT_ROOT']."/connect.php");
$sql2 = "SELECT * FROM shops WHERE owner = '" . $logged[username] . "'";
$result2 = mysql_query($sql2) or print ("Can't select table.<p>" . mysql_query() . "</p>");
$number = mysql_num_rows($result2);

if ($number != 1) {

include($_SERVER['DOCUMENT_ROOT'] . "/connect.php");
$sql = "INSERT INTO shops (timestamp, owner, shop_name, shop_desc, shop_front, shop_size, Shop_location) 
VALUES ('$timestamp','$username','$shop_title','$shop_desc','$shop_front','$shop_size','$shop_location')";

$result = mysql_query( $sql ) or die ( mysql_error());//exit, can also be used
$id = mysql_insert_id();

$shop_size2 = $_POST['shop_size'];

if ($shop_size2 == "4") {
$price = "50";
} else if ($shop_size2 == "8") {
$price = "100";
} else if ($shop_size2 == "12") {
$price = "200";
} else if ($shop_size2 == "16") {
$price = "300";
} else if ($shop_size2 == "20") {
$price = "400";
} else if ($shop_size2 == "60") {
$price = "600";
}


include($_SERVER['DOCUMENT_ROOT']."/connect.php");
$sql4 = "SELECT * FROM users WHERE username = '" . $logged[username] . "'";
$result4 = mysql_query($sql4) or die ("Can't select table.<p>" . mysql_query() . "</p>");// use exit or die, print won't stop the script..
while($row4 = mysql_fetch_array($result4)) {
$points = $row4['points'];
}


$equation = ($points - $price);

include($_SERVER['DOCUMENT_ROOT'] . "/connect.php");
$sql5 = "UPDATE users SET points='$equation' WHERE username='" . $logged[username] . "' LIMIT 1";// you could also just use $sql again, therefore they are called variables :P
$result5 = mysql_query($sql5) or die (mysql_error());// same here, use $result instead of always incrementing
if ($result5 != false) {

echo "<span style=\"color: green;\">Success!  Your shop has been

successfully created!It is located at <p><a

href=\"shop.php?id=$id\">$address/shop.php?id=$id</a></span>";

}
} else {
echo "<span style=\"color: red;\">You Have already created a

shop!</span>";
}
} else {
echo "<span style=\"color: red;\">You must fill in all the required

fields.</span>";
}
}
?>


#3 hdshngout

hdshngout
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 06 August 2006 - 08:21 PM

basically, what the script does is take the total number of points you have (earned from posting and other things around the site), and subtract the price of creating a user shop (cheesy I know).  This feature specifically is meant to subtact X amount from your total points depending on the size shop you wish to create. The problem is, the subtraction isn't initiating and I can't figure out why!  :P

#4 Chevy

Chevy
  • Members
  • PipPipPip
  • Advanced Member
  • 163 posts

Posted 06 August 2006 - 09:04 PM

<?php if (isset($_POST['submit'])) {
function cleanUp($input)
{
  return htmlspecialchars(strip_tags(stripslashes(trim($input))));// well no-one ever gets passed this :D
}

$shop_title = cleanUp($_POST['shop_name']);
$shop_front = $_POST['shop_front'];
$shop_front = str_replace("<", "[", $shop_front);
$shop_front = str_replace(">", "]", $shop_front);
$shop_desc = cleanUp($_POST['shop_desc']);
$shop_desc = nl2br($shop_desc);
$shop_size = cleanUp($_POST['shop_size']);
$shop_location = cleanUP($_POST['shop_location']);
$timestamp = strtotime("now");
$username = $logged['username'];

if ( (empty($shop_title)) || (empty($shop_desc)) || (empty($shop_size)) ||

(empty($shop_location))) {

include($_SERVER['DOCUMENT_ROOT']."/connect.php");
$sql2 = "SELECT * FROM shops WHERE owner = '" . $logged[username] . "'";
$result2 = mysql_query($sql2) or print ("Can't select table.<p>" . mysql_query() . "</p>");
$number = mysql_num_rows($result2);

if ($number != 1) {

include($_SERVER['DOCUMENT_ROOT'] . "/connect.php");
$sql = "INSERT INTO shops (timestamp, owner, shop_name, shop_desc, shop_front, shop_size, Shop_location) 
VALUES ('$timestamp','$username','$shop_title','$shop_desc','$shop_front','$shop_size','$shop_location')";

$result = mysql_query( $sql ) or die ( mysql_error());//exit, can also be used
$id = mysql_insert_id();

$shop_size2 = $_POST['shop_size'];

if ($shop_size2 == "4") {
$price = "50";
} else if ($shop_size2 == "8") {
$price = "100";
} else if ($shop_size2 == "12") {
$price = "200";
} else if ($shop_size2 == "16") {
$price = "300";
} else if ($shop_size2 == "20") {
$price = "400";
} else if ($shop_size2 == "60") {
$price = "600";
}


include($_SERVER['DOCUMENT_ROOT']."/connect.php");
$sql4 = "SELECT * FROM users WHERE username = '" . $logged[username] . "'";
$result4 = mysql_query($sql4) or die ("Can't select table.<p>" . mysql_query() . "</p>");// use exit or die, print won't stop the script..
while($row4 = mysql_fetch_array($result4)) {
$points = $row4['points'];
}


$equation = $points - $price;

include($_SERVER['DOCUMENT_ROOT'] . "/connect.php");
$sql5 = "UPDATE users SET points='$equation' WHERE username='" . $logged[username] . "' LIMIT 1";// you could also just use $sql again, therefore they are called variables :P
$result5 = mysql_query($sql5) or die (mysql_error());// same here, use $result instead of always incrementing
if ($result5 != false) {

echo "<span style=\"color: green;\">Success!  Your shop has been

successfully created!It is located at <p><a

href=\"shop.php?id=$id\">$address/shop.php?id=$id</a></span>";

}
} else {
echo "<span style=\"color: red;\">You Have already created a

shop!</span>";
}
} else {
echo "<span style=\"color: red;\">You must fill in all the required

fields.</span>";
}
}
?>
You do not need ('s or )'s in a -

#5 king arthur

king arthur
  • Members
  • PipPipPip
  • Advanced Member
  • 335 posts
  • LocationUK HQ

Posted 06 August 2006 - 10:28 PM

Think it may be this bit here
$sql2 = "SELECT * FROM shops WHERE owner = '$logged[username]'";
$result2 = mysql_query($sql2) or print ("Can't select table.<p>" .

If the number of rows returned is zero, the rest of your code doesn't run. The number of rows returned may be zero because I think the query string is not interpolated in the way you want - instead try
$sql2 = "SELECT * FROM shops WHERE owner = '" . $logged[username] . "'";
$result2 = mysql_query($sql2) or print ("Can't select table.<p>" .

Never sure about these but that's what I'd try!
Sir Isaac Newton said "If I have seen farther, it is by standing on the shoulders of giants". But it is not recorded as to whether he said it before or after he was hit on the head by a falling apple.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users