INSERT语句在PHP脚本中以静默方式失败,但会在提示下工作

INSERT语句在PHP脚本中以静默方式失败,但会在提示下工作,php,postgresql,insert,Php,Postgresql,Insert,我在PostgreSQL 8.4.5中有下表: snake=> create table gps ( id bytea check(length(id) = 16), stamp timestamp DEFAULT current_timestamp, pos point not null); 我可以从psql提示符将记录插入其中: snake=> insert into gps (id, pos) values (decode(md5('x'), 'hex'), point(0,

我在PostgreSQL 8.4.5中有下表:

snake=> create table gps (
id bytea check(length(id) = 16),
stamp timestamp DEFAULT current_timestamp,
pos point not null);
我可以从psql提示符将记录插入其中:

snake=> insert into gps (id, pos) values (decode(md5('x'), 'hex'), point(0, 0));
INSERT 0 1
snake=> insert into gps (id, pos) values (decode(md5('x'), 'hex'), point(0, 0));
INSERT 0 1
但由于某些原因,INSERT在下面列出的PHP脚本中失败,其结果返回为0。有人知道哪里出了问题,或者如何获得更多信息吗?我很惊讶没有抛出异常

<?php

$id  = trim($_REQUEST['id']);
$lat = strtr(trim($_REQUEST['lat']), ',', '.');
$lon = strtr(trim($_REQUEST['lon']), ',', '.');

if (preg_match('/^[a-fA-F0-9]{32}$/', $id) &&
    preg_match('/^[+-]?[0-9.]+$/', $lat) &&
    preg_match('/^[+-]?[0-9.]+$/', $lon)) {

        try {
                $db = new PDO('pgsql:host=/tmp', 'snake', 'snake');
                $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

                $insert = $db->prepare("insert into gps (id, pos) values (decode(?, 'hex'), point(?, ?))");
                $res = $insert->execute($id, $lat, $lon);

                $select = $db->prepare("select encode(id, 'hex') as id, extract('epoch' from stamp) as stamp, pos[0] as lat, pos[1] as lon from gps");
                $select->execute();

                header('Content-Type: text/xml; charset=utf-8');
                print '<?xml version="1.0"?><gps>';
                while ($row = $select->fetch(PDO::FETCH_ASSOC)) {
                        printf('<pos id="%s" stamp="%u" lat="%f" lon="%f" />',
                            $row['id'], $row['stamp'], $row['lat'], $row['lon']);
                }
                printf('<res val="%d" />', $res);

                print '</gps>';
        } catch (Exception $e) {
                print 'Database problem: ' . $e->getMessage();
        }

} else {
        header('Content-Type: text/html; charset=utf-8');
        print '<html>
<body>
<form method="post">
<p>Id: <input type="text" name="id" size=32 maxlength=32 /></p>
<p>Latitude: <input type="text" name="lat" /></p>
<p>Longitude: <input type="text" name="lon" /></p>
<p><input type="submit" value="Save" /></p>
</form>
</body>
</html>
';

}

?>
fetch(PDO::fetch_ASSOC)){
printf(“”,
$row['id']、$row['stamp']、$row['lat']、$row['lon']);
}
printf(“”,$res);
打印“”;
}捕获(例外$e){
打印“数据库问题:”。$e->getMessage();
}
}否则{
标题('Content-Type:text/html;charset=utf-8');
印刷品
身份证:

纬度:

经度:

'; } ?>
我得到的输出表明结果为0:

<gps>
<pos id="0cc175b9c0f1b6a831c399e269772661" stamp="1287306960" lat="51.000000" lon="7.000000"/>
<pos id="0cc175b9c0f1b6a831c399e269772661" stamp="1287323377" lat="51.000000" lon="7.000000"/>
<pos id="92eb5ffee6ae2fec3ad71c777531578f" stamp="1287323381" lat="51.000000" lon="7.000000"/>
<pos id="92eb5ffee6ae2fec3ad71c777531578f" stamp="1287323442" lat="51.300000" lon="7.000000"/>
<pos id="92eb5ffee6ae2fec3ad71c777531578f" stamp="1287325610" lat="51.300000" lon="7.000000"/>
<pos id="92eb5ffee6ae2fec3ad71c777531578f" stamp="1287325612" lat="51.300000" lon="7.000000"/>
<pos id="9dd4e461268c8034f5c8564e155c67a6" stamp="1287325692" lat="0.000000" lon="0.000000"/>
<res val="0"/>
</gps>

问候,, 亚历克斯

PS:这是我目前的剧本,看起来不错-

<?php

$id  = trim($_REQUEST['id']);
$lat = strtr(trim($_REQUEST['lat']), ',', '.');
$lng = strtr(trim($_REQUEST['lng']), ',', '.');

if (preg_match('/^[a-fA-F0-9]{32}$/', $id) &&
    preg_match('/^[+-]?[0-9.]+$/', $lat) &&
    preg_match('/^[+-]?[0-9.]+$/', $lng)) {

        try {
                # enable persistent connections and throw exception on errors
                $options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
                                 PDO::ATTR_PERSISTENT => true);

                $db = new PDO('pgsql:host=/tmp dbname=snake', 'snake', 'snake', $options);

                #$db->exec('create table gps (id bytea check(length(id) = 16), stamp timestamp DEFAULT current_timestamp, pos point not null)');

                $insert = $db->prepare("insert into gps (id, pos) values (decode(?, 'hex'), point(?, ?))");
                $insert->execute(array($id, $lat, $lng));

                $select = $db->prepare("select encode(id, 'hex') as id, extract('epoch' from stamp) as stamp, pos[0] as lat, pos[1] as lng from gps");
                $select->execute();

                header('Content-Type: text/xml; charset=utf-8');
                print '<?xml version="1.0"?><gps>';
                while ($row = $select->fetch(PDO::FETCH_ASSOC)) {
                        printf('<pos id="%s" stamp="%u" lat="%f" lng="%f" />',
                            $row['id'], $row['stamp'], $row['lat'], $row['lng']);
                }
                print '</gps>';
        } catch (Exception $e) {
                print 'Database problem: ' . $e->getMessage();
        }

} else {
        header('Content-Type: text/html; charset=utf-8');
        print '<html>
<body>
<form method="post">
<p>Id: <input type="text" name="id" size=32 maxlength=32 /></p>
<p>Latitude: <input type="text" name="lat" /></p>
<p>Longitude: <input type="text" name="lng" /></p>
<p><input type="submit" value="Save" /></p>
</form>
</body>
</html>
';
}

?>
fetch(PDO::fetch_ASSOC)){
printf(“”,
$row['id']、$row['stamp']、$row['lat']、$row['lng']);
}
打印“”;
}捕获(例外$e){
打印“数据库问题:”。$e->getMessage();
}
}否则{
标题('Content-Type:text/html;charset=utf-8');
印刷品
身份证:

纬度:

经度:

'; } ?>
我认为您需要使用参数数组而不是多个参数执行
PDOStatement->insert()

$res = $insert->execute($id, array($lat, $lon));

请参阅。

您是否收到任何错误消息?您是否使用相同的用户名表单命令行和脚本?这不会导致您的问题,但我想指出此错误。在数字匹配中,您有[0-9.],该点(.)表示“任何字符”。您需要将其转义为[0-9\.],因此它只匹配一个句点。@除了
]\^-
之外的明显元字符都是字符类中的常规字符,所以这在这里不是问题。@lonesomeday:Hrm,过去阅读手册的建议与我相反。请注意,在“Version8正则表达式”下,它讨论了在字符类附近转义,但没有指出这样的异常。但这一行代码表明我错了:perl-e'$foo=“0a9”;如果$foo=~/^[0-9.]+$/;”,则打印“点是神奇的”\n--没有输出。谢谢你帮我澄清!是的,我是一个长期的Perl用户,我非常确定,在regex char类中可以使用类似[-+0-9^.]的东西。例如:perl-e'print(“^”=~/[-+0-9^.]/”),不过还是感谢您查看我的代码!就这样,谢谢你!奇怪的是,没有出现异常有一条评论:“将PDO::ATTR_ERRMODE属性设置为PDO::ERRMODE_异常只适用于Mysql 4.x-我花了一些时间才弄明白;-)”也许这就是原因?