1

顧客の e コマース バックエンドを構築しようとしています。私はこれまで何度もこのようなことをしてきましたが、自分自身をphpとmysqlの「新しい」とは考えていませんが、立ち往生していて何が悪いのかわかりません。

特定の場所にある mysql 行の内容を表示したいだけです (「WHERE」コマンドを使用)。

しかし、ページをロードすると、コンテンツ部分 (テーブル内) が空になります。その場所のテーブル内には間違いなくコンテンツがあり、ページ上の他のすべては実際の customerResults を除いて表示されます。

これが私のコードです:

<head>
<title>Customer Summary</title>
<?php  
    session_start();
    require 'database_connect.php';
    $customerTable = "customer";

    if(isset($_GET['customer_click'])){
        $customerId = $_GET['customer_click']; 
    }
?>
</head>
<h3>Customer <?php echo"$customerId"?></h3>
<table align="center" width="600px">
    <tr>
        <td><a href="index.php">Summary</a></td>
        <td><a href="personal.php">Personal</a></td>
        <td><a href="billing.php">Billing</a></td>
        <td><a href="order_history.php">Order History</a></td>
    </tr>
</table>
<table align="center" width="400px">
    <tr>
        <?php 
            $customerSelect = "SELECT * FROM $customerTable WHERE id = '$customerId' ";
            $customerResult = mysql_query($customerSelect);
            if (!$customerResult){
                echo "No results, but why?!?!? </br/>";

            }
            if  (mysql_num_rows($customerResult)==0){
                echo "Results are empty...but why!?!?!";

            }

            while ($customerData = mysql_fetch_assoc($customerResult)){ 
                echo $customerData['id'];
                echo $customerData['email'];
            }

        ?>      
    </tr>
</table>

私は単純なものを見落としている可能性がありますが、これを理解することはできません

4

1 に答える 1

4

どれどれ:

  • 27 行目: 未定義の変数'customerSelct'
  • 行 41: 未定義の変数'customerDdata'
  • 43 行目: 未定義の変数'result'

さらに、新しいコードで関数を使用しないでくださいmysql_*。それらはもはや維持されておらず、非推奨プロセスが開始されています。赤いボックスが見えますか? 代わりにプリペアド ステートメントについて学び、 PDOまたはMySQLiを使用してください。この記事は、どちらを決定するのに役立ちます。PDO を選択する場合は、ここに良いチュートリアルがあります。

PDO を使用したコード例:

<?php
    try {
        session_start();
        if (!isset($_GET['customer_click'])) {
            throw new Exception('Customer ID not provided.');
        }
        //Assuming the ID must be a number.
        if (!is_numeric($_GET['customer_click'])) {
            throw new Exception('Customer ID must be numeric.');
        }
        $customerID = $_GET['customer_click'];
        $db         = new PDO("mysql:host=localhost;dbname=database_name_here", "user", "pass");
        //Have PDO to not emulate prepared statements by default.
        //Instead use MySQL's native prepare engine.
        $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        //PDO will throw PDOExceptions on every error.
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $query = "SELECT * FROM `customer` WHERE `id` = :id";
        $stmt  = $db->prepare($query);
        //Bind ID as a number and not as string.
        $stmt->bindValue(":id", $customerID, PDO::PARAM_INT);
        $stmt->execute();
        //Fetch all results into $result.
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
    }
    catch (PDOException $e) {
        //A database error has occurred!
        die("Database Error occurred! " . $e->getMessage());
    }

    catch (Exception $e) {
        //General error occurred!
        die("Error! " . $e->getMessage());
    }
?>
<!DOCTYPE HTML>
<html lang="en-US">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>

    <pre>
        <?php print_r($result); ?>
    </pre>

</body>
</html>
于 2012-07-26T18:48:44.730 に答える