如何在PHP脚本中接受PostgreSQL数据库数量?

时间:2019-03-03 16:46:52

标签: php psql

这是我要为学校项目编辑的简单代码段。据我了解,问题在于它正确地获取了数量,但可能没有将其存储在变量$ quantity中。我尝试调试它没有成功。同样,简单的加法也给出了奇怪的结果。 请帮忙。代码如下

<?php
    include("DB.php");

    $id = $_POST["flower_id"];
    $add_quantity = $_POST["quantity"];

    echo "FlowerID";
    echo $id;
    echo "<br>";
    echo "Add Quantity";
    echo $add_quantity;
    echo "<br>";

    $con = pg_connect("host=localhost dbname=kruttika user=kruttika");
    $quantity = pg_query("select quantity from inventory where flower_id='$id'");
    $new = $quantity;
    echo "Fetched Quantity";
    echo $new;
    echo "<br>";

    if(!$con)
      echo "Error<br>"; 
    else
    {
        $new_quantity = $new + $add_quantity;
        echo "Total Quantity";
        echo $new_quantity;
        echo "<br>";
        $sql = "update inventory set quantity=$new_quantity where flower_id='$id'";
        echo "SQL injected";
        echo $sql;
        echo "<br>";
        $result=pg_query($sql);
    }
    echo $result;
    echo "<br>";
    //echo "<script>location.href=\"adminindex.html\"</script>";
    ?>

1 个答案:

答案 0 :(得分:0)

我认为问题是:

$quantity = pg_query("select quantity from inventory where flower_id='$id'");

pg_query()不返回实际值,而是返回pg_fetch_result()

您必须获取结果以获取期望的值。请参阅上面链接的文档中的示例。