简体   繁体   中英

Insert data from XML Node into MySQL Database

I have been working on the following script

$servername = "localhost";
$username = "blah";
$password = "blah";
$dbname = "test";


// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}


echo "<form id='post' action='' method='POST'>";
echo "<br />";
echo "<br />";
echo "<input type ='text' value = '' name = 'filepath'/>";
echo "<input type='submit' name='submit_form_1' value='Submit URL' id='submit';'/>";
echo "<br />";
echo "<br />";
echo "</form>";

if(isset($_POST['submit_form_1'])){
     if(empty($_POST['filepath'])) // If the checkbox array called selection is empty
    {
        echo "<p>Please enter a URL that leads to your XML</p>";
    } 
    else // If the selection array is not empty we then need to check whether ID has been selected
    {
    $test = $_POST['filepath'];
    print ($test);
    echo "<br />";

    // WE ONLY CONTINUE IF SOME TEXT WAS WRITTEN



// Testing - Get the data from a stored XML file using Simple XML
if (file_exists($test)){ // Check that the file exists
$getxml = simplexml_load_file($test) or die("Error, unfortunately there was an issue");
echo "The stuff from the XML File <br/>";

// Loop through the children within the XML file
foreach($getxml->children() as $element){

    $ID = $element->id; echo $ID;
    $area = $element->area; echo $area;
    $country = $element->country; echo $country;
    $city = $element->city; echo $city;
    $town = $element->town; echo $town;
    $postcode = $element->postcode; echo $postcode;
    $lotarea = $element->lot_area; echo $lotarea;
    $price = $element->price; echo $price;
    $bedrooms = $element->bedrooms; echo $bedrooms;
    $bathrooms = $element->bathrooms; echo $bathrooms;
    $summary = $element->summary; echo $summary;
    $latitude = $element->latitude; echo $latitude;
    $longlitude = $element->longlitude; echo $longlitude;
    $street = $element->street; echo $street;
    $streetno = $element->streetno; echo $streetno;
    $name = $element->name; echo $name;
    $image = $element->image; echo $image;


    $sql = "INSERT INTO importtest (id, area, country, city, town, postcode, 
        lotarea, price, bedrooms, bathrooms, summary, latitude, longlitude, 
        street, streetno, type, image) 
        VALUES($ID, $area, $country, $city, $town, $postcode, $lotarea, $price, 
        $bedrooms, $bathrooms, $summary, $latitude, $longlitude, $street, $streetno, $name, $image)";


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

}



// Insert the data we gathered from the XML file into our database
// PARAMETERIZE THIS AT SOME POINT!


$conn->close();

} else{
    echo "Unfortunately the path you entered: " . $test . " did not return a file";
}


}
}

The code above uses SimpleXML to load an XML file from a source specified by the user. It then gets all the data in the chld nodes and sets them as variables.

At this point I am attempting to perform an insert statement each time the loop 'loops' however I get an error message returned instead of the data being inserted.

My question is is my theory for doing this type of thing a feasible one and why does my INSERT statement not function as expected?

Also the error returned says error near ' (value I tried to insert)

Essentially I am trying to import some XML data into my database.

I think problem is inside values you trying insert.

try to change your code:

 $sql = "INSERT INTO importtest (id, area, country, city, town, postcode, 
        lotarea, price, bedrooms, bathrooms, summary, latitude, longlitude, 
        street, streetno, type, image) 
        VALUES($ID, $area, $country, $city, $town, $postcode, $lotarea, $price, 
        $bedrooms, $bathrooms, $summary, $latitude, $longlitude, $street, $streetno, $name, $image)";


if ($conn->query($sql) === TRUE) {

to this kind :

$stmt = $mysqli->prepare("INSERT INTO importtest (id, area, country, city, town, postcode, 
        lotarea, price, bedrooms, bathrooms, summary, latitude, longlitude, 
        street, streetno, type, image) 
        VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param('issssssiiisssssss', $ID, $area, $country, $city, $town, $postcode, $lotarea, $price,  $bedrooms, $bathrooms, $summary, $latitude, $longlitude, $street, $streetno, $name, $image);

if ($conn->execute() === TRUE) {

and you have to set 1st parameter of bind_param according to your table structure types http://php.net/manual/en/mysqli-stmt.bind-param.php :

i   corresponding variable has type integer
d   corresponding variable has type double
s   corresponding variable has type string
b   corresponding variable is a blob and will be sent in packets

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM