How can I insert getElementsByTagName content into sql table?

130 views Asked by At

I would like to parse an XML page, and insert the content into an SQL database, but I don't know how to call the variables in the insert syntax!

Here's my code:

<?php
$server = "localhost";
$user = "root";
$password = "";
$database = "database";

$conn = mysqli_connect($server, $user, $password, $database);

if (!$conn) { die ("Connection Failed:" .mysql_connect_error()); }
echo "Connected Successfully "; 

$sql = "INSERT INTO tableest (stepid, stepname, stepstatus) VALUES ('???', '???', '???')";

?>

<!DOCTYPE html>
<html>
<head>
<script>
if (window.XMLHttpRequest)
{ // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp = new XMLHttpRequest();
}
else
{ // code for IE6, IE5
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","example.xml",false);
xmlhttp.send();
xmlDoc = xmlhttp.responseXML; 


function displayTicket()
{
    var x = xmlDoc.getElementsByTagName("step");
    for (i = 0; i < x.length; i++) {
        id = (x[i].getElementsByTagName("id")[0].childNodes[0].nodeValue);
        name = (x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue);
        skip = (x[i].getElementsByTagName("status")[0].childNodes[0].nodeValue);

        txt = "id:" + id + "<br>name: " + name + "<br>skip? : " + skip;
        document.getElementById("showid").innerHTML=txt;
    }
}
</script>
</head>

<body onload="displayTicket()">
    <div id='showid'></div>

</body>
</html>

But I keep getting undefined variable notice!

2nd issue:

The for loop when used in the displayticket function (as used above) only gives me infos for one step (and doesn't go through all the steps)

but gives me a correct output if used as following (without a function):

var x = xmlDoc.getElementsByTagName("step");
for (i = 0; i < x.length; i++) {
    document.write(x[i].getElementsByTagName("id")[0].childNodes[0].nodeValue);
    document.write(x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue);
    document.write(x[i].getElementsByTagName("status")[0].childNodes[0].nodeValue);
}

Any Idea how I can fix this?

All help and Inputs are appreciated! Thanks in advance

0

There are 0 answers