douzepao0281 2013-08-29 18:58
浏览 44
已采纳

将xml上传到mysql php - 在表中插入无值[关闭]

Here is .xml format :

<?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>

Here is .php script

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; 

}

But this script dose not add any value to Table......completely blank

  • 写回答

1条回答 默认 最新

  • dongmo3413 2013-08-29 19:44
    关注

    You are trying to read attribute values the wrong way. You need to use array 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']);
    

    When you use $call->number SimpleXMLElement object tries to get child node <number> which does not exist in <call>.

    One other thing, what is field type of readable_date in DB?
    If it is VARCHAR then no problem, but if TIMESTAMP or DATETIME during insert mysql will convert to 0000-00-00 00:00:00 which is wrong so you should first convert it to proper DB datetime format:
    It is easier to convert already present UNIX timestamp $call['date'] using date() function than convert $call['readable_date']

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

    But since it seems to me your date and readable_date do not match same dates you may need to properly convert it:

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

    Function for conversion:

    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];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器