2

I have the following PHP file.

<?php
    header('Content-Type: text/xml');
    echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
    session_start();
    echo '<response>';
    $email = $_GET['person'];
    $UserEmail = $_SESSION['login'];

    $conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    mysql_select_db(DB_DATABSE, $conn);

    $sql_find = mysql_query("SELECT * FROM Users WHERE UserEmail='".$email."' LIMIT 1");

    if(mysql_fetch_array($sql_find)) {
        $sql_add = "UPDATE Users SET Contacts = concat(Contacts, ';', '".$email."') WHERE UserEmail = '".$UserEmail."'";
        if (mysql_query($conn, $sql_add)) {
           echo array( 'found' => true, 'msg' => "Person added to your record");
        } 
        else {
           echo array( 'found' => false, 'msg' => "Error adding person to your record \n Is the person emails' correct?");
        }

    }
    else {
        echo array( 'found' => false, 'msg' => "We couldn't find the user in our databases.");
    }

    echo '</response>';
?>

I use Ajax to add data to MySql and return the result to front-end. The problem is that even if the SQL code work when I tested to my server PhPAdmin, it shows me the following error:

XML Parsing Error: mismatched tag. Expected: </br>.
Location: http://secretsea.comli.com/lib/addPerson.php
Line Number 3, Column 261:<br><table border='1' cellpadding='2' bgcolor='#FFFFDF' bordercolor='#E8B900' align='center'><tr><td><div align='center'><a href='http://www.000webhost.com/'><font face='Arial' size='1' color='#000000'>Free Web Hosting</font></a></div></td></tr></table>Array</response>
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------^

I tried to find a solution on the website and other resourses, but I couldn't understand what am I doing wrong. Is my syntax wrong?

  • Are you sure that the pasted code and the error message are related? There is no html code generation in your php code. The error message is likely a result of treating html code as xml. In html using`
    ` tag in itself (just as it is used in the error messge) is a valid syntax, while xml requires an end tag (``) or a self-closed tag (`
    `). Perhaps the doctype used in the frontend is not correct, xml or xhtml is used instead of plain html.
    – Shadow Dec 22 '15 at 13:11
  • **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Dec 22 '15 at 13:13
  • @Quentin if that was the only problem with the code... – Shadow Dec 22 '15 at 13:15
  • @Shadow yes, i'm a student. and? Everyone start from a point! ;) –  Dec 22 '15 at 13:41

2 Answers2

1

echo array(...) will just print the string Array.
The code you've posted does not produce the output

<br ><table border='1' cellpadding='2' bgcolor='#FFFFDF' bordercolor=...

It's probably some advertise added by your free web host. Bad enough, but even worse, it's not suitable for XML since the <br> should be <br /> to be xml compliant.

VolkerK
  • 95,432
  • 20
  • 163
  • 226
  • I use `JSON.parse` to parse the array to js, so the output isn not a problem. But that `br` it is -.- –  Dec 22 '15 at 13:50
  • "I use JSON.parse to parse the array" - There's not much to parse right now, take a look at the output: `Array`. You probably want `echo json_encode( array(....) )` in your php script. see http://docs.php.net/json_encode. Edit: and why would you send json data when you have an xml document (or the other way round: why xml when you want json)? – VolkerK Dec 22 '15 at 15:10
1

Because </br> is not valid tag. You must write <br/> for compatibility with xml.

Duplicate question is https://stackoverflow.com/a/1946442/4536186.

Community
  • 1
  • 1
Harlam
  • 408
  • 2
  • 11