PHP代码不会提取数据库信息

时间:2015-04-17 20:58:19

标签: php html sql database

由于某些原因,我的当前程序不会注册我的数据库值,即使我之前的代码(外观相同但来自不同的数据库表)也可以正常工作。

到目前为止,我运行时没有错误。每页打印的唯一内容是表格的标题。 此外,客户端页面工作正常。因此,产品(电视,手机,计算机)和交易页面是我当前的问题。

我当前的index.php文件(主程序)如下:

<?php
//url /index.php?action=clients

    include('header.php'); // create top box
    include('sidemenu.php'); // create side menu
    //database connection
    include('pdo_connect.php');

    //Read data type
    $type = "";
    if (isset($_REQUEST['action']))
     $type = $_REQUEST['action'];

    //echo 'Action: {$type}';

    switch($type) {
            case 'products' : //display a list of clients
                    //define sql
                    $sql = "SELECT product_type, product_title, product_description, unit_price FROM products
                            WHERE product_type = :product_type";
                    $values = array(':product_type'=>$_GET['product_type']);
                    $products = getAll($sql);
                    //display result
                    displayProductList($products);
                    break;
            case 'clients' : //displaya list of movies
                    $sql = "SELECT first_name, last_name, email FROM p_clients";
                    $clients = getAll($sql);
                    displayClientList($clients);
                    break;
            case 'transactions' :
                    $sql = "SELECT products.product_title, products.product_description, products.unit_price, p_clients.first_name,
                            p_clients.last_name, sales.quantity FROM p_clients INNER JOIN sales, products ON p_clients.client_id = sales.client_id
                                                                                                            AND products.product_id = sales.product_id";
                    $transactions = getAll($sql);
                    displayTransactionList($transactions);
                    break;
            default:
                    defaultView();
                    break;
    }
    include('footer.php');

function defaultView() {
?>
<!-- add page content -->
<div id='content'>
    <h2>Welcome to our movie store</h2>
</div>
    <div id = 'image'></div>
    <div id = 'box'>
    <p id = 'text-box'>
    We appreciate your interest in our products and services. Please reference
    the the links provided to see our current specials for each of our clients.
    </p>
    </div>
<?php
}

    function displayProductList($products) {

    echo "<div id='content'>
            <h2>List of Products</h2>";
    echo "<table id = clients>";
    echo "<tr><td id = 'title'>Product Name</td><td id= 'title'>Description</td><td id = 'title'>Cost</td></tr>";
    //display each record
    for ($i = 0; $i < count($products); $i++){

    echo "<tr><td>{$products[$i]['product_title']} </td><td> {$products[$i]['product_description']} </td><td>
                    {$products[$i]['unit_price']} </td></tr>" ;

    }
    echo "</table>";
    echo "</div>";


    }

    function displayClientList($clients) {

    echo "<div id='content'>
            <h2>List of Clients</h2>";
    echo "<table id = 'long'>";
//        echo "<table>";
 echo "<tr><td id = 'title'>First Name</td><td id= 'title'>Last Name</td><td id = 'title'>Email</td></tr>";
    //display each record
    for ($i = 0; $i < count($clients); $i++){

    echo "<tr><td>{$clients[$i]['first_name']}</td><td> {$clients[$i]['last_name']}
    </td><td> {$clients[$i]['email']} </td></tr>";


    }

    echo "</table>";
    echo "</div>";


    }


    function displayTransactionList($transactions) {

    echo "<div id='content'>
            <h2>List of Client Transactions</h2>";
    echo "<table id = 'long'>";
//        echo "<table>";
    echo "<tr><td id = 'title'>First Name</td><td id= 'title'>Last Name</td><td id = 'title'>Product Title</td>
    <td id = 'title'>Product Description</td><td id = 'title'>Cost</td><td id = 'title'>Quantity</td></tr>";
    //display each record
    for ($i = 0; $i < count($transactions); $i++){

    echo "<tr><td>{$transactions[$i]['first_name']}</td><td> {$transactions[$i]['last_name']}
    </td><td> {$transactions[$i]['product_title']} </td><td> {$transactions[$i]['product_description']} </td><td>
    {$transactions[$i]['unit_price']} </td><td> {$transactions[$i]['quantity']} </td></tr>";


    }

    echo "</table>";
    echo "</div>";


    }


    function getAll($sql, $values =null){

    global $db;
    $statm = $db->prepare($sql);

    //Method 4
    //assign a value to named parameters using an array
    //$values= array(':genre'=>'drama');
    $statm->execute($values);

    //Fetch all records
    $result = $statm->fetchAll();
    return $result;
    }

sidemenu.php文件从链接调用每个文件:

<div id = 'sidebar'>
<h4>Links</h4>
<ul id = "nav" class= "text-left">
<li><a href='index.php'>Home</a></li>
<li><a href='index.php?action=products&product_type=tv'>TV Products</a></li>
<li><a href='index.php?action=products&product_type=cell'>Cell Phone Products</a></li>
<li><a href='index.php?action=products&product_type=computer'>Computer Products</a></li>
<li><a href='index.php?action=clients'>List of Customers</a></li>
<li><a href='index.php?action=transactions'>List of Transactions</a></li>
<!--<li><a href='index.php?action=moviesFS&genre=sci-fi&date_out=2009-12-15'>List of Favorite Sci-Fi Movies</a></li>-->
</ul>
</div>

正如您所看到的,如果我将我的sql输入到我的数据库中,它可以正常工作:

enter image description here

1 个答案:

答案 0 :(得分:1)

尝试更改

$products = getAll($sql);

$products = getAll($sql,$values);

case 'products' :