-1

注意してください、私はコーディング n00b で、基本的な PHP の割り当てを受けました。これを開こうとすると、このエラーメッセージが表示され続けます

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /home/jpl8ce/public_html/php_grocery1.php on line 20

これが、私がやっていることと一致する私の .php および .html ファイルです。基本的には、ユーザーがフォームに入力したアイテムを含む店の営業時間、住所、名前を印刷しようとしている食料品データベースです。

<html>
<head>
<title>Grocery Results!</title>
</head>
<body>
<h1>
<?php
$connect = mysqli_connect("localhost","jpl8ce","jpl8ce@siedb","grocery");
$item_name = $_post["input1"];

$query_String = " SELECT Stores.Name, Price, Address, Open_Time, Close_Time
FROM (Stores JOIN Carry ON Stores.Store_ID = Carry.Store_ID) JOIN Items ON Carry.Product_ID = Items.Product_ID
WHERE Items.name = $item_name
ORDER BY Close_Time DESC";


$result = mysqli_query($connect, $query_String);

while ($row = mysqli_fetch_assoc($result)) 
{

echo '<P>'.$row["Stores.Name"].', '.'</P>'; 
//echo '$' number_format($row["Price"],2).', ';
echo '<P>'.$row["Address"] .', '.'</P>';
echo '<P>'.$row["Open_Time"] .', '.'</P>';
echo '<P>'.$row["Close_Time"].'</P>';
}

mysqli_close($connect);  
?>
</h1>
</body>
</html>

ここに私のHTMLコードがあります

<html>
<head>
<title>Grocery Database Search</title>
</head>
<body>
<H1> Grocery Database Search! </H1>
<IMG SRC='grocery_cart_2.jpg'/>
<P> Use this Search Engine to input a name of an item.
After clicking the Search button, the price of the item, as well as the address and hours of the store 
the item is located at will be displayed. </P>

<form action="php_grocery1.php" method="POST">
<p>Item: <input type="text" name="input1"/></p>
<p><input type="submit" value="Search!"/></p>
</form>
</body>
</html>

みんなありがとう!

4

1 に答える 1

0

あなたのコードにはいくつかの問題があります:

  • すべてのスーパーグローバルは大文字でなければなりません。$_post["input1"];と置き換えます$_POST["input1"];
  • クエリは、SQL インジェクション攻撃に対して脆弱です。ユーザーが送信したデータをクエリに配置する場合は常に、次のようなエスケープ関数を使用しますmysql_real_escape_string()( PHP ドキュメントはこちら を参照してください)。
  • また、SQL クエリに渡される文字列は、一重引用符で囲む必要があります (以下の\'エスケープされた引用符に注意してください)。
  • 問題 (おそらく発生している) を正確に検出するには、次のようなエラー処理コードを追加する必要があります。

    <html>
        <head>
            <title>Grocery Results!</title>
        </head>
        <body>
            <?php
                if(!$connect = mysqli_connect("localhost","jpl8ce","jpl8ce@siedb","grocery"))
                    die('Error connecting to DB!: ' . mysql_error());
    
                $item_name = $_POST["input1"];
    
                $query_String = 'SELECT Stores.Name, Price, Address, Open_Time, Close_Time
                    FROM (Stores JOIN Carry ON Stores.Store_ID = Carry.Store_ID) JOIN Items ON Carry.Product_ID = Items.Product_ID
                    WHERE Items.name = \'' . mysql_real_escape_string($item_name) . '\' ORDER BY Close_Time DESC';
    
                if(!$result = mysqli_query($connect, $query_String))
                    die('Error on query: ' . mysql_error($connect));
    
                while ($row = mysqli_fetch_assoc($result)) {
                    echo '<P>' . $row["Stores.Name"] . ', ' . '</P>';
                    //echo '$' number_format($row["Price"],2).', ';
                    echo '<P>' . $row["Address"] . ', ' . '</P>';
                    echo '<P>' . $row["Open_Time"] . ', ' . '</P>';
                    echo '<P>' . $row["Close_Time"] . '</P>';
                }
    
                mysqli_close($connect);
            ?>
        </body>
    </html>
    

お役に立てれば。

于 2012-04-09T06:06:28.717 に答える