Please somebody fix this code
In postgres database, I have 5 tables table1, table2, table3, table4 and
table5. I have 5 identical columns in each tables those are date,
location, item1, item2 and item3. Date (which consists date and time) is a
primary key in all tables.
I also have 3 folders item1, item2 and item3 where I have 3 different
files item1.xml, item2.xml and item3.xml. In xml file there are more than
20 different dates and under date tag there are 5 elements. I need to read
those data and insert into the database.
Sample of xml file:
<?xml version='1.0'>
<xml_code>
<date valid_time = '2013091006'>
<element id = '1'>2.9</element>
<element id = '2'>2.9</element>
<element id = '3'>2.9</element>
<element id = '4'>2.9</element>
<element id = '5'>2.9</element>
</date>
<date valid_time = '2013091012'>
<element id = '1'>2.9</element>
<element id = '2'>2.9</element>
<element id = '3'>2.9</element>
<element id = '4'>2.9</element>
<element id = '5'>2.9</element>
</date>
</xml_code>
Till the date will be 2013092000.
Sample of php code:
<?php
$dbh = new PDO("pgsql:host=host;dbname=dbname", "username", "password");
if (!$dbh) {
echo "Failed to connect database!";
}
$table = array('table1', 'table2', 'table3', 'table4', 'table5');
$item = array('item1', 'item2', 'item3');
foreach($item as $item_code)
{
$location = 'location';
$xml = folder/file;
$obj_DOM = simplexml_load_file($xml_file);
$file_dates = $objDOM->date;
foreach($file_dates as $file_date) {
$datestring = $file_date['valid time']
$elements = $file_date->element;
$i = 0;
foreach($elements as $element) {
$value = $element;
if($item_code == 'item1') { $item1 = $value; }
if($item_code == 'item2') { $item2 = $value; }
if($item_code == 'item3') { $item3 = $value; }
$row_exist = db->prepare("SELECT count(*) from " . $table[$i] .
"WHERE date = :dateandtime")
$row_exist->bindParam(':dateandtime', $datestring);
$row_exist->execute();
$num_rows = count($row_exist->fetchAll());
if($num_rows < 1)
{
$sql = $dbh->prepare("INSERT INTO " . $table[$i].
"(date, location, item1, item2, item3)
VALUES(:date, :location, :item1, :item2, :item3)");
$sql->bindParam(':date', $datestring);
$sql->bindParam(':location', $location);
$sql->bindParam(':item1', $item1);
$sql->bindParam(':item2', $item2);
$sql->bindParam(':item3', $item3);
$sql->execute();
}
else{
$sql = $dbh->prepare("Update" . $table[$i] .
"SET item1=:item1, item2=:item2, item3=:item3
WHERE date = :date AND location = :location")
$sql->bindParam(':date', $datestring);
$sql->bindParam(':location', $location);
$sql->bindParam(':item1', $item1);
$sql->bindParam(':item2', $item2);
$sql->bindParam(':item3', $item3);
$sql->execute();
}
$i++;
}
}
}
?>
Just trying make it clear. For example: value from element ID=1 from
item1.xml, item2.xml and item3.xml are supposed to be stored in columns
item1, item2 and item3 of table1 respectively along with date and
location.Now I am having issue in update. After I run this code, only
item1 values are inserted into all 5 tables but not item2 and item3 which
supposed to be inserted from update code. Can somebody please assist me
and show what is wrong I am doing here?
These are not the real code. If somebody do not understand the code please
let me know where I can make the improvements but please those people who
do not know how to fix this code do not bother yourself posting other
unnecessary comments.
Thank you in advance!!
No comments:
Post a Comment