Jay@TastefulTitles.com Posted July 9, 2020 Share Posted July 9, 2020 I'm a novice to PHP. What I want to do is add a new customer record to my XML database. It looks as if a simple way to do this would be to create a string using heredoc, which will insert the variable values, then turn it into a simpleXML object: $xml=simplexml_load_file("Test data.xml"); //load XML file // these $vars are all defined from form input in previous code $str= <<<EOS <!----> <customer> <cId>$Customer</cId> <organization>$organization</organization> <website>$URL</website> <contact>$name</contact> <email>$email</email> <phone>$phone</phone> <addr>$address</addr> <city>$city</city> <state>$state</state> <zip>$zip</zip> </customer> EOS; $new = new SimpleXMLElement($str); //turn $str into SimpleXML element $xml->addChild("customer",$new); //add it to DB My first question is whether DreamWeaver CS5 will allow me to do this. It gives me the "syntax error on line 1." Is it able to recognize heredoc? Second, am I approaching this the right way? I'm sure this is something that is done regularly for myriad websites, but I haven't found any clear direction on the best way to handle it. Finally, if this is the right approach, what am I missing to make it work? Link to comment Share on other sites More sharing options...
Ingolme Posted July 10, 2020 Share Posted July 10, 2020 What is on line 1? The heredoc isn't on line 1. Link to comment Share on other sites More sharing options...
Jay@TastefulTitles.com Posted July 10, 2020 Author Share Posted July 10, 2020 "Syntax error on line 1" is apparently how DW CS5 reports a syntax error anywhere in a PHP doc. The only thing on line 1 is "<?php". The error could be in line 12,345, and fixing it will make the error report go away. In this case, if I comment out the heredoc (lines 138-155), it reports "No syntax errors." Link to comment Share on other sites More sharing options...
Ingolme Posted July 10, 2020 Share Posted July 10, 2020 I guess DreamWeaver doesn't understand the heredoc syntax. Try to find a way to make it ignore the error and then run the code on a PHP server. I've never used DreamWeaver. For many years it a bad reputation because its preview tool was very wrong, so most developers steered clear of it. I assume by now they've fixed that, but since there are hundreds of free alternatives I don't have a reason to pay for it, especially now that it's a continuous subscription service instead of a one time purchase. Link to comment Share on other sites More sharing options...
Jay@TastefulTitles.com Posted July 11, 2020 Author Share Posted July 11, 2020 (edited) Okay, I'll hunt for a DW replacement. It has to run on OS10.11.6. Any recommendations? Meanwhile, I can upload and run the code regardless of the error message, but it doesn't seem to work. I was wondering if you could see a reason why. Is this a reasonable way to approach my goal? Some of the discussions I've read seem to imply I have to insert each tag separately, but I can't tell for sure. Edited July 11, 2020 by Jay@TastefulTitles.com Link to comment Share on other sites More sharing options...
Ingolme Posted July 11, 2020 Share Posted July 11, 2020 I usually use DOMDocument instead of SimpleXML, so I would have to read the documentation. I would look up SimpleXML on the manual at php.net to see if they have examples of loading documents from a string. Link to comment Share on other sites More sharing options...
Jay@TastefulTitles.com Posted July 11, 2020 Author Share Posted July 11, 2020 I will look at it. Do you have an example of using DOMDocument? That might work just as well for me. Thanks! Link to comment Share on other sites More sharing options...
Ingolme Posted July 11, 2020 Share Posted July 11, 2020 This is an example of loading a document from a string using DOMDocument: https://www.php.net/manual/en/domdocument.loadxml.php It looks like for SimpleXML, you can use simplexml_load_string(): https://www.php.net/manual/en/function.simplexml-load-string.php Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now