-1

Good evening, today I've been trying to work out a formula on submitting my xml data into my MySQL database, this is something I've not dome before but I've been trying very hard and have unfortunately been unsuccessful, I get the following error message, Parse error: syntax error, unexpected '(', expecting identifier (T_STRING) or variable (T_VARIABLE) or '{' or '$' in locations.php on line 9 please find below my xml file which is a sample, and my PHP code, if anyone is able to help I really appreciate it, thank you very much

xml

<?xml version="1.0" encoding="utf-8"?>
    <datafile>
    <data name="Area One">
        <category>Place One</category>
        <title name="One" size="100" color="red" />
        <title name="Two" size="150" color="yellow" />
        <title name="Three" size="200" color="pink" />
        <title name="Four" size="250" color="green" />
        <title name="Five" size="300" color="purple" />
    </data>
    <data name="Area Two">
        <category>Place Two</category>
        <title name="One" size="500" color="orange" />
        <title name="Two" size="550" color="blue" />
        <title name="Three" size="600" color="silver" />
        <title name="Four" size="650" color="white" />
        <title name="Five" size="700" color="gold" />
    </data>
</datafile>

php

    $xml = simplexml_load_file("locations.xml") 
        or die("Error: Cannot create object");

    foreach($xml->children() as $data) { 
        echo $data->('data name') . ", "; 
        echo $data->category . ", "; 
        echo $data->('title name') . $data->('size') . $data->('color') . "<br>";
    }

    $sql = "INSERT INTO data (data-name, category, information)
        VALUES ('', '', '')";

    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $db->error;
    }

What I've been trying to do is to insert each data name from the xml file into it's own mysql record "data name" to go into "data-name" "category" to go into "category"

and when it comes to "title name", "size" and "color" they all share the "information" field, for example

"one, 100, red
two, 150, yellow
three, 200, pink
four, 250, green
five, 300, purple"

I do apologize in advance if in the way I've explained this is confusing, please feel free to ask me any questions, thank you very much

Amy Lane
  • 3
  • 5
  • 2
    For one thing, `data-name` MySQL is interpreting that as `data minus name`. – Funk Forty Niner Dec 04 '16 at 20:07
  • Possible duplicate of [When to use single quotes, double quotes, and backticks?](http://stackoverflow.com/questions/11321491/when-to-use-single-quotes-double-quotes-and-backticks) – Funk Forty Niner Dec 04 '16 at 20:12
  • i was thinking: http://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them @Fred-ii- –  Dec 04 '16 at 20:13
  • Or, [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) IMHO @Dagon would pretty much cover everything. – Funk Forty Niner Dec 04 '16 at 20:15

1 Answers1

0

First of all, as @Fred-ii mentioned,

.... data-name MySQL is interpreting that as data minus name

So change the column name to dataname or something that suits your need as well as a valid column name.

And second, the syntax errors are because of these lines,

echo $data->('data name') . ", "; 
            ^^^^^^^^^^^^^

and

echo $data->('title name') . $data->('size') . $data->('color') . "<br>";
            ^^^^^^^^^^^^^^          ^^^^^^^^          ^^^^^^^^^ 

If you're planning to insert those information into the database table, then your foreach loop should be like this:

foreach($xml->children() as $data){ 

    foreach($data->attributes() as $name){
        $dataName = $name;
    }
    $category = $data->category;
    foreach($data->title as $title){
        $attr = array();
        foreach($title->attributes() as $attribute){
            $attr[] = $attribute;
        }
        $information = implode(",", $attr);
        $sql = "INSERT INTO `data` (`dataname`, `category`, `information`) VALUES ('{$dataName}', '{$category}', '{$information}')";

        if ($conn->query($sql) === TRUE) {
            echo "New record created successfully";
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }
    }
}

There are few additional points to wrap everything up

  1. data is a reserved word in MySQL, so you can't use it as table name like that in your query. Better that you escape those table and column names using backticks.

    Here's the reference:

  2. $db->error should be $conn->error.

Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37