2013-08-29 37 views
0

这里没有值.xml格式:载XML到MySQL PHP - 插入表

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?><!--File Created By Call Logs Backup & Restore v3.21 on 29/08/2013 19:53:23--><?xml-stylesheet type="xsl" href="calls.xsl"?> 
<calls count="500"> 
<call number="+919257035805" duration="0" date="1377691732581" type="3" readable_date="28/08/2013 17:38:52" contact_name="(Unknown)" /> 
<call number="+919257035805" duration="38" date="1377691747866" type="2" readable_date="28/08/2013 17:39:07" contact_name="(Unknown)" /> 
</calls> 

这里名为.php脚本

if ($_FILES[xml][size] > 0) { 
$file = $_FILES[xml][tmp_name]; 
$xml = simplexml_load_file($file); 
    $count = 0; 
foreach ($xml->call as $call) { 
$number = mysql_real_escape_string($call->number); 
$duration = mysql_real_escape_string($call->duration); 
$type = mysql_real_escape_string($call->type); 
$readable_date = mysql_real_escape_string($call->readable_date); 
$contact_name = mysql_real_escape_string($call->contact_name); 


mysql_query("INSERT INTO call_log (number, duration, type, readable_date, contact_name) VALUES ('$number', '$duration', '$type', '$readable_date', '$contact_name')") or die ("Error in query: $insert. ".mysql_error()); 

    } 

//redirect 
    header('Location: upload_sql.php?success=1?inserts=' . $count . ''); die; 

} 

但是这个剧本剂量不会添加到表中的任何值。 .....完全空白

回答

0

您试图以错误的方式读取属性值。您需要使用数组电话:

$number = mysql_real_escape_string($call['number']); 
$duration = mysql_real_escape_string($call['duration']); 
$type = mysql_real_escape_string($call['type']); 
$readable_date = mysql_real_escape_string($call['readable_date']); 
$contact_name = mysql_real_escape_string($call['contact_name']); 

当您使用$call->number SimpleXMLElement对象设法得到它不存在<call>子节点<number>

另外一件事,DB中的字段类型readable_date是什么?
如果是VARCHAR那么没有问题,但如果插入MySQL的过程中TIMESTAMPDATETIME将转换为0000-00-00 00:00:00这是不对的,所以你应该首先将其转换为正确的DB日期时间格式:
更容易使用转换已经存在的UNIX时间戳$call['date']date()功能比转换$call['readable_date']

$readable_date = date('Y-m-d H:i:s', (int) $call['date']); 

但因为在我看来,你的datereadable_date不匹配,则可能需要正确地转换它同一日期:

$readable_date = convertDate($call['readable_date']); 

功能转换:

function convertDate($in) 
{ 
    preg_match('#^(\d{2})/(\d{2})/(\d{4}) (\d{2}):(\d{2}):(\d{2})$#', $in, $matches); 
    return $matches[3] . '-' . $matches[2] . '-' . $matches[1] . ' ' . 
     $matches[4] . ':' . $matches[5] . ':' . $matches[6]; 
}