繁体   English   中英

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

[英]PHP code won't pull database information

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

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

我当前的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,则效果很好:

在此处输入图片说明

尝试改变

$products = getAll($sql);

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

case 'products' :

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM