What needs to be demonstrated?
Second-order SQL injection is nothing more than SQL injection, but unsafe code is not the first line.
So, to demonstrate:
1) Create an SQL input line that will do something unwanted when executed without escaping.
2) Store this string safely in your database (with escaping).
3) Let some other part of your FETCH code have this line and use it elsewhere without escaping.
EDIT: added sample code:
Table:
CREATE TABLE tblUsers ( userId serial PRIMARY KEY, firstName TEXT )
Suppose you have some kind of SAFE code, for example, getting the first name from a form:
$firstname = someEscapeFunction($_POST["firstname"]); $SQL = "INSERT INTO tblUsers (firstname) VALUES ('{$firstname }');"; someConnection->execute($SQL);
So far, so good, assuming someEscapeFunction () does an excellent job. Unable to add SQL.
If I sent the following line as the value for firstname, you would not mind:
blah '); REMOVE FROM tblUsers; //
Now suppose that someone from one system wants to transfer the firstName from tblUsers to tblWhatever, and does it like this:
$userid = 42; $SQL = "SELECT firstname FROM tblUsers WHERE (userId={$userid})"; $RS = con->fetchAll($SQL); $firstName = $RS[0]["firstName"];
And then inserts it into tbl, in any case, without escaping:
$SQL = "INSERT INTO tblWhatever (firstName) VALUES ('{$firstName}');";
Now, if firstname contains some deleteecommand file, it will still be executed.