my server setup apparently excludes multiple queries in a single mysql_query() command. i have a script that connects to my db...i added this code:
$sql = "SELECT * FROM notes; INSERT INTO `notes` (`id`, `userid`, `title`, `note`, `url`, `url_title`) VALUES ('', '3', 'I AM THE SQL INJECTION DANGIT', 'IF THIS NOTE APPEARS, YOU COULD SUFFER FROM DROP OR ALTER TABLE COMMANDS.', '', '');";
echo "sql:" . $sql . "</p>";
mysql_query($sql)
or die("QUERY FAILED:" . mysql_error());
echo 'script complete';
the results are:
sql:SELECT * FROM myplan_notes; INSERT INTO myplan_notes (id, userid, title, note, url, url_title) VALUES ('', '3', 'I AM THE SQL INJECTION DANGIT', 'IF THIS NOTE APPEARS, YOU COULD SUFFER FROM DROP OR ALTER TABLE COMMANDS.', '', '');
QUERY FAILED:You have an error in your SQL syntax near '; INSERT INTO myplan_notes (id, userid, title, note, url, `url_title' at line 1
Do most mySQL servers allow multiple queries? What configuration on mine (or what verson?) might exclude multiple operations in a single query?
I did manage to create some query string variables (using urlencode for quotes) that let me access data i didn't feel like sharing.
I would prefer, if possible, just to simply use mysql_real_escape_string() on any query values rather than writing customized code in each case to determine if the data is valid for a query.
i understand now that any time you take user input and incorporate it directly into a query that you need to first take a look at what you're getting. mysql_real_escape_string seems really useful because it will turn quotes into backslashed quotes. in my case above, if anyone were to attempt sql injection, escaping their &id= value would be sufficient in that it would return nothing. two possible cases
case 1:
(id quoted)
"SELECT * FROM notes WHERE id='$id'"
in this case, if i remembered to escape $id, then the query would still be valid even if a user were to put in quotes and try some injection because quotes would be backslashed. even so, no records would be located that matched an sql fragment because 'id' is an integer field.
i'm guessing the case would be the same for this:
"SELECT * FROM notes WHERE id=\"$id\""
case 2:
(no quotes)
"SELECT * FROM notes WHERE id=$id"
in this case, i'm guessing that sql injection attempts would break the query if there were any quotes in the sql injection because they would all be backslashed, but what about asterisks? OR statements? someone could easily set id to
1 OR id>0
in which case the query would look like:
SELECT * FROM notes WHERE id=1 OR id>0
will mysql_real_escape_string() stop that kind of injection? i'll try some tests and see.