I can’t seem to figure out how to save an XML file that I generated with the DOM objects to my database..
Here is my PHP:
$xmlraw = $doc->saveXML();
$xmlQuery=sprintf("INSERT INTO xmlTestTable (XMLString) VALUES ('%s')", $xmlraw);
$result = mysql_query($xmlQuery);
I also tried:
$xmlQuery=sprintf("INSERT INTO xmlTestTable (XMLString) VALUES ('%s')", $doc->saveXML());
$result = mysql_query($xmlQuery);
Where $doc is the XML Document I created.
I am able to see the XML output in my browser when I do this:
echo $doc->saveXML();
There are no errors being outputted or anything…
My MySQL Column that this is being injected into is ‘Long Text’
Thank you in advance!!
First of all, you need to get the XML string, using, as you guessed, the
saveXML()method :Then, you need to insert this value ; but you must escape it properly !
Escaping a string to inject it into an SQL query is something you’ll do using the specific function that’s provided by the API you’re using to connect to your database — as you are using
mysql_*functions, you’ll usemysql_real_escape_string()Now, you have the string you can inject into your SQL query :
You can also use sprintf, like you did, of course.