PHP代码不会拉数据库信息 [英] PHP code won't pull database information

查看:129
本文介绍了PHP代码不会拉数据库信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



到目前为止,我运行时没有错误。为每个页面打印的唯一的事情是表的标题。 客户页面也很好。所以产品(电视,手机,电脑)和交易页面是我当前的问题。



index.php文件(主程序)如下:

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

include('header.php'); // create top box
include('sidemenu.php'); //创建边菜单
//数据库连接
include('pdo_connect.php');

//读取数据类型
$ type =;
if(isset($ _ REQUEST ['action']))
$ type = $ _REQUEST ['action'];

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

switch($ type){
case'products'://显示客户端列表
//定义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);
//显示结果
displayProductList($ products);
break;
case'clients':// displaya电影列表
$ 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;
默认值:
defaultView();
break;
}
include('footer.php');

function defaultView(){
?>
<! - 添加页面内容 - >
< div id ='content'>
< h2>欢迎来到我们的电影商店< / h2>
< / div>
< div id ='image'>< / div>
< div id ='box'>
< p id ='text-box'>
我们感谢您对我们的产品和服务的关注。请参阅
所提供的链接,以查看我们每个客户的当前特价。
< / p>
< / div>
<?php
}

function displayProductList($ products){

echo< div id ='content'>
< h2>产品列表< / h2>;
echo< table id = clients>;
echo< tr>< td id ='title'>产品名称< / td>< td id ='title'>说明< / td>< td id ='title' ; Cost< / td>< / tr>;
//显示每个记录
for($ i = 0; $ 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>客户列表< / h2>;
echo< table id ='long'>;
// echo< table>;
echo< tr>< td id ='title'>名字< / td>< td id ='title'>姓氏< / td>< td id = >电子邮件< / td>< / tr>;
//显示每个记录
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>客户交易清单< / h2>;
echo< table id ='long'>;
// echo< table>;
echo< tr>< td id ='title'>名字< / td>< td id ='title'>姓氏< / td>< td id = >产品标题< / td>
< td id ='title'>产品说明< / td>< td id ='title'>费用< / td>< td id = '> Quantity< / td>< / tr>;
//显示每个记录
for($ i = 0; $ 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>;


}


函数getAll($ sql,$ values = null){

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

//方法4
//使用数组为命名的参数赋值
// $ values = array(':genre'=>'drama');
$ statm-> execute($ values);

//提取所有记录
$ result = $ statm-> fetchAll();
return $ result;
}

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

 < div id ='sidebar'> 
< h4>链接< / h4>
< ul id =navclass =text-left>
< li>< a href ='index.php'>首页< / a>< / li>
< li>< a href ='index.php?action = products& product_type = tv'>电视产品< / a>< / li>
< li>< a href ='index.php?action = products& product_type = cell'>手机产品< / a>< / li>
< li>< a href ='index.php?action = products& product_type = computer'>计算机产品< / a>< / li>
< li>< a href ='index.php?action = clients'>客户列表< / a>< / li>
< li>< a href ='index.php?action = transactions'>交易清单< / a>< / li>
<! - < li>< a href ='index.php?action = moviesFS& genre = sci-fi& date_out = 2009-12-15'>喜欢的科幻电影; / a>< / li> - >
< / ul>
< / div>

可以看到,如果我将我的sql键入我的数据库,它工作很好:



>

解决方案

尝试更改

  $ products = getAll ); 

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

case'products': p>

My current program, for some reason, won't register my database values even though my previous code(looks identical but from different database tables) works just fine.

So far I have no errors when it runs. The only thing that prints for each page is the table's title. Also the clients page works just fine. So the product(tv, cell, computer) and transaction page are my current issue.

My current index.php file (main program) is as follows:

<?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;
    }

The sidemenu.php file calls each one from links:

<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>

As you can see, if I type my sql into my database it works just fine:

解决方案

Try changing

$products = getAll($sql);

to

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

inside case 'products' :

这篇关于PHP代码不会拉数据库信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆