For my job I need to generate a sql scripts for updating some values in the database from a json file as seen bellow:
$jsonContents=file_get_contents('./myfile.json');
$jsonContents=json_decode($jsonContents, true);
$script="";
foreach($jsonContents['data'] as $element)
{
$script.="UPDATE mytable SET element='{$element['value']}' where id={$element['id']};";
}
file_put_contents('./script.sql',$script);
So I wonder once the sql script will be executed, is the danger of sql injection lurking around or is safe to just place the data as is into the sql script? If under some circumstances the sql injection threat is lurking and how I can make my script sql injection safe?
Keep in mind that I was asked to provide an sql script instead of directly accessing the database and updating the values using php. In the later case I could just use the PDO and prepared statements.
The database layer is postgresql.
mysqli_real_escape_string. The thing is, those aren't 100% safe either, but to have any chance for them to work even remotely reliably you need to have an active database connection to the database you want to insert the data into. So… is that possible? Probably not, right?