Jump to content

Subtracting with php?


hdshngout

Recommended Posts

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?

[code]<?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>";
}
}
?>[/code]

(by the way, I'm sorry if I accidentally broke any of the rules, still new here and learning my way around)
Link to comment
Share on other sites

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

[code]
<?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>";
}
}
?>
[/code]
Link to comment
Share on other sites

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
Link to comment
Share on other sites

[code]
<?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>";
}
}
?>
[/code]
You do not need ('s or )'s in a -
Link to comment
Share on other sites

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

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
[code]
$sql2 = "SELECT * FROM shops WHERE owner = '" . $logged[username] . "'";
$result2 = mysql_query($sql2) or print ("Can't select table.<p>" .
[/code]

Never sure about these but that's what I'd try!
Link to comment
Share on other sites

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.