0

嗨,我想知道你是否可以帮助我?我正在尝试通过使用存储在名为 items 的数组中的参考号并在表中显示行来从数据库中获取行,但我只能获取要在页面上显示的参考号.. .任何帮助真的很感激吗?

</head>
<h1>Shopping Cart</h1>

<body>

<?php $con = pg_connect("blah blah");
if (!$con){
    die('Could not connect: ' . pg_error());
}

if (isset($_POST['items'])) {
    $n = count($_POST['items']);
    for($i=0; $i < $n; $i++)
        echo $_POST['items'][$i]; 
    }

    if(!$_SESSION["selectingrows"]== 0){
        $result = pg_query ($con, "SELECT title, platform, description, price FROM CSGames     WHERE 'refnumber' = 'items[]'");

    echo "<table>
            <tr>
            <th>Title</th>
            <th>Platform</th>
            <th>Description</th>
            <th>Price</th>
            </tr>";

    while($rows = pg_fetch_result($result));{
        echo"<tr>"; echo "<td>" . $rows['title'] . "</td>"; echo "<td>" . $rows['platform'] . "</td>"; echo "<td>" . $rows['description'] . "</td>"; echo "<td>" . $rows['price'] . "</td>";
        echo"</tr>";      
    }
    echo"</table>";
}
4

3 回答 3

1

您正在阻止您的 while 语句:

while($rows = pg_fetch_result($result));{

应该是:

while($rows = pg_fetch_result($result)){

注意分号不见了。

编辑:似乎有更多的语法错误(这是完整的):

$con = pg_connect("blah blah");
if (!$con){
    die('Could not connect: ' . pg_error());
}

if (isset($_POST['items'])) {
    $n = count($_POST['items']);
    for($i=0; $i < $n; $i++){
        echo $_POST['items'][$i]; 
    }

    if(!$_SESSION["selectingrows"]== 0){
        $result = pg_query ($con, "SELECT title, platform, description, price FROM CSGames     WHERE 'refnumber' = 'items[]'");

    echo "<table>
            <tr>
            <th>Title</th>
            <th>Platform</th>
            <th>Description</th>
            <th>Price</th>
            </tr>";

    while($rows = pg_fetch_result($result));{
            echo"<tr>"; echo "<td>" . $rows['title'] . "</td>"; echo "<td>" . $rows['platform'] . "</td>"; echo "<td>" . $rows['description'] . "</td>"; echo "<td>" . $rows['price'] . "</td>";
            echo"</tr>";      
        }
        echo"</table>";
    }
}
于 2012-11-29T00:14:12.837 回答
0

我想你的问题在这里:

$result = pg_query ($con, "SELECT title, platform, description, price FROM CSGames     WHERE 'refnumber' = 'items[]'");

items[]不是任何类型的有效变量引用。您可能需要执行以下操作:

$items = array();
foreach ($_POST['items'] as $item) {
    $items[] = pg_escape_string($con, $item);
}

$item_string = "'" . implode("','", $items) . "'";

$result = pg_query ($con, "SELECT title, platform, description, price FROM CSGames     WHERE 'refnumber' IN ($item_string)");

注意我已经转义了发布的数据以用于查询,我已经构建了一个包含所有值的字符串,然后在 SQLWHERE 'refnumber' IN子句中使用了该字符串。

于 2012-11-29T00:19:40.717 回答
0
</head>
<h1>Shopping Cart</h1>

<body>

<?php $con = pg_connect("blah blah");
if (!$con){
    die('Could not connect: ' . pg_error());
}

if (isset($_POST['items'])) {
    $n = count($_POST['items']);
    $items = array();
    for($i=0; $i < $n; $i++)
        array_push($items,$_POST['items'][$i]; // stored referrral_no in array
    }
    $items = implode("','",$items); // implode to query use
    if(!$_SESSION["selectingrows"]== 0){
        $result = pg_query ($con, "SELECT title, platform, description, price FROM CSGames     WHERE 'refnumber' IN ('$items')"); // revise query to IN

    echo "<table>
            <tr>
            <th>Title</th>
            <th>Platform</th>
            <th>Description</th>
            <th>Price</th>
            </tr>";

    while($rows = pg_fetch_result($result)){
        echo"<tr>"; echo "<td>" . $rows['title'] . "</td>"; echo "<td>" . $rows['platform'] . "</td>"; echo "<td>" . $rows['description'] . "</td>"; echo "<td>" . $rows['price'] . "</td>";
        echo"</tr>";      
    }
    echo"</table>";
}

试试这个我首先将你的项目再次存储在数组中,然后将其内爆以用于查询

于 2012-11-29T00:23:20.813 回答