php debugging help needed

zimu

Diamond Member
Jun 15, 2001
6,209
0
0
this should be the easiest thing in the world to do but i have no clue what i'm missing. think i've got friday afternoon blues.

basically, have table "courses". in it there's an "enabled / disabled" fields.

there's a table that shows all the records and outputs whether the row is enabled or disabled. if enabled, there's a button to disable, and vice versa.

if ($row_course[enabled]=="yes")
{
print ("Enabled<br>"); ?>
<form method = post action = editcourses.php>
<input type = hidden name = id value = <? echo $row_course[id] ?>>
<input type = submit name = disableit value = "Disable">
<form> <?
}
else
{
print ("Disabled<br>"); ?>
<form method = post action = editcourses.php>
<input type = hidden name = id value = <? echo $row_course[id] ?>>
<input type = submit name = enableit value = "Enable">
<form> <?
}

up to here its fine. lets say i have two records: record 5 and 6.

for some reason when i hit enable for row 5, it enables row 6. WTF?

this is the code that does the actual modification.

if (isset($_POST[enableit])) {
print ("enabling $_POST[id]");
mysql_query("UPDATE courses SET enabled='yes' WHERE ID='$_POST[id]'") or die(mysql_error());
}
if (isset($_POST[disableit])) {
print ("disabling $_POST[id]");
mysql_query("UPDATE courses SET enabled='no' WHERE ID='$_POST[id]'") or die (mysql_error());
}

no errors. if i enable row 6 it works fine. if i enable row 5, it enables row 6. what am i missing!
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
A couple of things I notice immediately that need to be fixed. When comparing strings use strcmp instead of ==

For example

if (!strcmp($row_course[enabled],"yes")) would replace if ($row_course[enabled]=="yes")

Also, on your form submission you need to sanitize your input. You are ripe for SQL injection with your current code.

 

zimu

Diamond Member
Jun 15, 2001
6,209
0
0
hey crusty,

thanks for the response! ok, changed the strcmp. still no go though, seems to update the wrong record.

how would i sanitize my input on the form? i wasn't overly worried about that as all this code runs behind an administrator interface which doesn't even have access to the outside world...
 

zimu

Diamond Member
Jun 15, 2001
6,209
0
0
HAHAHA i'm such a dumbass. i put <form> instead of </form> at the end of each of the sections! UGH.

would still love input as to how to sanitize it
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
Basically you need to protect against someone giving you input you don't expect. There are things you can do such as escaping special characters like ' " ; to prevent the php engine from parsing those are part of the command instead of part of the data as well as checking data types.. if you are looking for a integer make sure the user actually gave you an integer and not a string or a date or whatever.

I haven't touched php in ages so I can't give more specific details but there are ways to build your SQL queries using a list of parameters that will automatically escape the characters you need.

As far as running behind an administrator interface... administrators are still users and users by nature can not be trusted.
 

LightningRider

Senior member
Feb 16, 2007
558
0
0
Yes, you should build your sql statements with prepared statements.

Take a look at the PHP sql objects, particularly the sql statement, it will allow you to execute sql queries the way they should be.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |