The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
Turn an number negative
Discuss Turn an number negative in the PHP Development forum on Dev Shed. Turn an number negative PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 13th, 2013, 05:52 PM
|
 |
Contributing User
|
|
Join Date: Apr 2004
Posts: 234
Time spent in forums: 2 Days 16 h 8 m 56 sec
Reputation Power: 10
|
|
|
Turn an number negative
I am sure this is fairly simple but I am not getting it. I need disQty to be negative. The user using the preceding, simple, html from must input a positive number. Any help please.
PHP Code:
if(isset($_POST['disQty']))
{
$disQty = $_POST['disQty'];
$pulledItem = $_POST['pulledItem'];
$n = count($disQty);
$i = 0;
while ($i < $n)
{
$dbh=mysql_connect ($data_base_connection, $user, $password) or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("hmsglobal");
$result=mysql_query("INSERT INTO exchInv (exchInvItemID,exchQty) VALUES ('{$pulledItem[$i]}','{$disQty[$i]}')")or die("Insert Error: ".mysql_error());
mysql_close;
$date = addslashes($date);
$i++;
}
}
|

February 13th, 2013, 06:36 PM
|
 |
Lost in code
|
|
|
|
|
Multiply the value by -1. You can't perform multiplication inside {} in a string, you need to use normal concatenation instead; ie "string" . (expression) . "string"
FYI Your code contains SQL injection vulnerabilities.
|

February 13th, 2013, 09:07 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Quote: | Originally Posted by E-Oreo FYI Your code contains SQL injection vulnerabilities. |
... and several other bad practices like this "or die()" stuff and reconnecting to the database for every single row (why do you even do that?).
Check the link in my signature and throw away whatever tutorial or book you got those code patterns from.
|

February 13th, 2013, 09:11 PM
|
 |
Contributing User
|
|
Join Date: Apr 2004
Posts: 234
Time spent in forums: 2 Days 16 h 8 m 56 sec
Reputation Power: 10
|
|
Quote: | Originally Posted by E-Oreo Multiply the value by -1. You can't perform multiplication inside {} in a string, you need to use normal concatenation instead; ie "string" . (expression) . "string"
FYI Your code contains SQL injection vulnerabilities. |
Okay, I have tried this and it is still positive.
INSERT INTO exchInv (exchInvItemID,exchQty) VALUES ('{$pulledItem[$i]}','$disQty[$i] * -1')
|

February 13th, 2013, 09:17 PM
|
 |
Contributing User
|
|
Join Date: Apr 2004
Posts: 234
Time spent in forums: 2 Days 16 h 8 m 56 sec
Reputation Power: 10
|
|
Quote: | Originally Posted by Jacques1 ... and several other bad practices like this "or die()" stuff and reconnecting to the database for every single row (why do you even do that?).
Check the link in my signature and throw away whatever tutorial or book you got those code patterns from. |
Okay thank you for the feedback. Ill make some changes but in the case of reconnecting to the database what has happened before is a never ending loop inserting the first row until the database with my isp shuts down.
|

February 13th, 2013, 09:29 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Quote: | Originally Posted by jlewis01 Okay, I have tried this and it is still positive. |
Have you read what E-Oreo said about string concatenation and that you can't do multiplication inside a string?
Quote: | Originally Posted by jlewis01 Ill make some changes but in the case of reconnecting to the database what has happened before is a never ending loop inserting the first row until the database with my isp shuts down. |
Well, then you should fix that rather than use strange workarounds. Going through the whole start-up procedure again and again is a performance killer and also stresses your server, so it's really not something you should have in your code.
What you could try is build the query up and then insert all values at once.
|

February 13th, 2013, 09:41 PM
|
 |
Contributing User
|
|
Join Date: Apr 2004
Posts: 234
Time spent in forums: 2 Days 16 h 8 m 56 sec
Reputation Power: 10
|
|
Quote: | Originally Posted by Jacques1 Have you read what E-Oreo said about string concatenation and that you can't do multiplication inside a string?
Well, then you should fix that rather than use strange workarounds. Going through the whole start-up procedure again and again is a performance killer and also stresses your server, so it's really not something you should have in your code.
What you could try is build the query up and then insert all values at once. |
Oh okay, I will do that. I did read what E-Oreo said and I got it but just didn't get it. I did however figure it out by your re-explanation. Code listed below. Also, is trigger_error is more acceptable rather than or die?
PHP Code:
INSERT INTO exchInv (exchInvItemID,exchQty) VALUES ('{$pulledItem[$i]}','$disQty[$i]' *-1)
Last edited by jlewis01 : February 13th, 2013 at 09:43 PM.
|

February 13th, 2013, 10:20 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Quote: | Originally Posted by jlewis01 Also, is trigger_error is more acceptable rather than or die? |
Yes! Because that actually generates an error, which can be turned off later, redirected to a log file etc.
A die() just dumps its output on the page, not matter if it's you testing the code on your local server or your users visiting your live site. Query errors expose a lot of the internal database code, so they can be used to launch attacks specifically on weak parts of the code.
In conjunction with the injection vulnerabilities (which are still there), I fear your website wouldn't last very long. There are automated tools for this, so it doesn't even have to be a genius "hacker".
|

February 14th, 2013, 07:44 AM
|
|
Registered User
|
|
Join Date: May 2012
Posts: 6
Time spent in forums: 1 h 16 m 11 sec
Reputation Power: 0
|
|
|
Pos to Neg
Why not just convert it when you read in the positive number? Like so:
$disQty = -(abs($_POST['disQty']));
Jim
|

February 14th, 2013, 08:05 AM
|
 |
Contributing User
|
|
Join Date: Apr 2004
Posts: 234
Time spent in forums: 2 Days 16 h 8 m 56 sec
Reputation Power: 10
|
|
Quote: | Originally Posted by Jacques1 Yes! Because that actually generates an error, which can be turned off later, redirected to a log file etc.
A die() just dumps its output on the page, not matter if it's you testing the code on your local server or your users visiting your live site. Query errors expose a lot of the internal database code, so they can be used to launch attacks specifically on weak parts of the code.
In conjunction with the injection vulnerabilities (which are still there), I fear your website wouldn't last very long. There are automated tools for this, so it doesn't even have to be a genius "hacker". |
I will address the vulnerabilities. 90% of the database is set to INT and this is not a public site.
|

February 14th, 2013, 08:32 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Quote: | Originally Posted by jlewis01 90% of the database is set to INT |
That makes no difference. The point is that your queries can be manipulated by whoever happens to visit the site. If you check testing tools like sqlmap, you'll realize those vulnerabilities can compromise your whole server. So even if you don't give a damn about your data, I'm pretty sure you do care about your server.
Quote: | Originally Posted by jlewis01 and this is not a public site. |
I know, there are a thousand justifications for unsecure code (it's a private site, it's just for testing, we'll rewrite everything later and so on).
But, seriously: Aren't those just excuses? Security isn't "only" about protecting yourself against evil hackers, it's also about correct code. Using unescaped variables is technically wrong, because the whole thing will blow up as soon as the input happens to contain characters like quotes, slashes, hyphens etc. So you don't even need an actual attacker, who does this on purpose.
To put it bluntly: security holes are bugs, even if nobody has noticed them yet.
I understand that you might have to deal with a lot of legacy code you cannot possibly rewrite. But at least for new features, it might be a good idea to be conscious of those issues and do it properly. 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|