duandiaoqian5795 2014-07-22 09:13
浏览 38
已采纳

从php数组中插入表中的错误值

code for scraping from a site :

<?php
require_once("db.php");
$url = 'http://www.indianrail.gov.in/mail_express_trn_list.html';
$ch = curl_init($url);

set_time_limit(600);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);
//echo $result;
error_reporting(E_ERROR | E_PARSE);
$dom    = new DOMDocument('1.0', 'UTF-8');  
$dom->loadHTML($result);
if($dom)
{
        $xpath = new DOMXPath($dom);


        $q = '///*[contains(concat(" ", normalize-space(@class), " "), " table_border ")]/tr';


        $nodes = $xpath->query($q);
        foreach($nodes as $tr){ // DOMNodesList implements traversable 
            echo "<br>";
            $tds = $tr->childNodes;
            $i = 0;
            foreach($tds as $td){
                $arr[$i]=$td->nodeValue;
                $i++;
            }
            var_dump($arr);
            //echo"<br><=====><br>";
            //echo ""
            mysqli_query($con,"INSERT INTO `irl`.`trains` (`TrainNo`, `TrainName`, `Origin`, `DepartureTime`, `Destination`, `ArrivalTime`) 
                                VALUES ('$arr[0]', '$arr[1]', '$arr[2]', '$arr[3]', '$arr[4]', '$arr[5]');") or die(mysqli_error($con));
        }

}
else
echo "invalid DOMDocument <br>";

this inserts '0's in the first position(TrainNo.) in the database, even though arr[0] contains the right values(I checked the var_dump($arr) ), what is going on ? arr[] is populated with the right values in each iteration., database field is int with size 8

changing field to varchar does fix it, but if datatyoe is the problem , why does the first row(train number 2696) get inserted correctly

example:

arr[0]=> 09705
arr[1]=>JP DEE AC EXP 
arr[2]=>JAIPUR 
arr[3]=>07:55
arr[4]=>DELHI S ROHILLA    
arr[5]=>13:20

Ignore the first row, it contains the table headers, so it is understandble that it contains 0:0 for time, and 0 for train number., but the rows below it should be fine, in the highlighted row first position should have been 9705, but it holds 0 : EDIT: including screenshots for convenience :
screenshots of var_dump of $arr in loop, followed by db rows in phpmyadmin :
var_dump($arr)

screenie of db rows

  • 写回答

3条回答 默认 最新

  • dongmeng4742 2014-07-22 09:50
    关注

    In your $arr[0] variable, from your screenshot, there is a space before the integer value 09705. Try an intval($arr[0]) if it's an integer type in your database.

    If you want to preserve the leading 0, you must change your type to VARCHAR for this column, and insert with trim($arr[0]) to delete the space character anyway.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题