1

i want to insert below xml string into mysql database

<message to='". $groupMembersArray[$i] . "@crossmessenger.com' type='groupchat' from='$mFrom'><x  xmlns='jabber:x:event'><offline/><delivered/><displayed/></x><body>$message</body></message>

so for that i have written below code

     $connection = mysqli_connect("localhost","xyz","abc#123","mydb");


for($i = 0 ; $i < $totalMembers ; $i++){

               $stanzaToInsert = "<message to='". $groupMembersArray[$i] ."@crossmessenger.com' type='groupchat' from='$mFrom'><x xmlns='jabber:x:event'><offline/><delivered/><displayed/></x><body>$message</body></message>";

           $msgSize = strlen($stanzaToInsert);
               $insertQuery = "INSERT INTO ofOffline VALUES ('$groupMembersArray[$i]', $messageID, $creationDate, $msgSize, '$stanzaToInsert')";
           mysqli_query($connection, $insertQuery);
           mysqli_commit($connection);
           echo "record $message inserted in table and commited";

}

but nothing is inserting in database. if i replace xml string that means value of stanzaToInsert to some simple text, then it is inserting value into database.

$insertQuery = "INSERT INTO ofOffline VALUES (
                         $groupMembersArray[$i], 
                         $messageID, 
                         $creationDate,
                         $msgSize,
                        'hard coded string'
                       )";

so problem is with xml string.

so can i know "what's wrong with me"?

4
  • Are you getting any errors? if no then check your type of message field. Commented Sep 1, 2014 at 5:33
  • @ManishJ: no ,not getting any error. type is "text" Commented Sep 1, 2014 at 5:34
  • have you tried addslashes() or mysql_escape_string() ? Commented Sep 1, 2014 at 5:36
  • @ManishJ tried mysqli_real_escape_string but not worked Commented Sep 1, 2014 at 5:38

2 Answers 2

1

you should use mysqli_real_escape_string before inserting the value like this

$stanzaToInsert = mysqli_real_escape_string($connection, $stanzaToInsert);
Sign up to request clarification or add additional context in comments.

Comments

0

Please use "N" befor the xml string for inert the xml message like this:--

(NULL,N'<?xml version="1.0" encoding="utf-16" standalone="yes"?><!--This is a test XML file--><filemeta filetype="Audio"><Comments /><AlbumTitle /><TrackNumber /><ArtistName /><Year /><Genre /><TrackTitle /></filemeta>')

Yourcode should be like this:-

$insertQuery = "INSERT INTO ofOffline VALUES (
                             $groupMembersArray[$i], 
                             $messageID, 
                             $creationDate,
                             $msgSize,
                            N'$xmlstring'
                           )";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.