实时网站上的PHP错误

时间:2017-02-07 21:02:03

标签: php mysql

我很难在我的网站上显示数据库表。我无法在index.php文件中执行查询,但我可以使用XAMPP在localhost上显示该表。我甚至无法执行文件中的MySQL语句代码。请帮忙!

这是我没有注释掉语句代码的时间:

enter image description here

这是我确实已注释掉的代码:enter image description here

我甚至在我尝试连接的表中有数据。这是index.php的代码:

    <?php
require_once('database.php');

//Get Category Id
$category_id = filter_input(INPUT_GET, 'category_id', FILTER_VALIDATE_INT);
if ($category_id == NULL || $category_id == false) {
    $category_id = 1;
}

// Get name for selected category
$queryCategory = 'SELECT * FROM categories WHERE categoryID = :category_id';
$statement1 = $link->prepare($queryCategory);
$statement1->bindValue(':category_id', $category_id);
$statement1->execute();
$category = $statement1->fetch();
$category_name = $category['categoryName'];
$statement1->closeCursor();

//Get all categories
$queryAllCategories = 'SELECT * FROM categories   ORDER BY categoryID';
$statement2 = $link->prepare($queryAllCategories);
$statement2->execute();
$categories = $statement2->fetchAll();


//Get products fpr selected category
$queryProducts = 'SELECT * FROM products WHERE categoryID = :category_id ORDER BY productID';
$statement3 = $link->prepare($queryProducts);
$statement3->bindValue(':category_id', $category_id);
$statement3->execute();
$products = $statement3->fetchAll();
$statement3->closeCursor();
?>

<!DOCTYPE html>
<HTML>
    <head>
        <title>My Guitar Shop</title>
          <link rel="stylesheet" type="text/css" href="style.css"/>
    </head>
    <body>
        <header><h1>Product Manager</h1></header>
        <main>
            <hr>
            <h1>Product List</h1>
            <aside>
                <h2>Categories</h2>
                <nav>
                    <ul>
                        <?php foreach ($categories as $category) : ?>
                            <li>    
                                <a href=".?category_id=<?php echo $category['categoryID']; ?>">    
                                    <?php echo $category['categoryName']; ?>
                                </a>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                </nav>
            </aside>
            <section>
                <!-- display a table of products -->
                <h2><?php echo $category_name; ?></h2>
                <table>
                    <tr>
                        <th>Code</th>
                        <th>Name</th>
                        <th class="right">Price</th>
                        <th>&nbsp;</th>
                    </tr>

                    <?php foreach ($products as $product) : ?>

                        <tr>
                            <td><?php echo $product['productCode']; ?></td>
                            <td><?php echo $product['productName']; ?></td>
                            <td><?php echo $product['listPrice']; ?></td>
                            <td><form action="delete_product.php" method="post">
                                    <!-- Delete Product -->
                                    <input type="hidden" name="product_id" value="<?php echo $product['productID']; ?>">
                                    <input type="hidden" name="category_id" value="<?php echo $product['categoryID']; ?>">
                                    <input type="submit" value="Delete">

                                </form>                    
                            </td>
                            <!-- Edit Product -->                    
                            <td><form action="edit_product_form.php" method="post">
                                    <input type="hidden" name="product_id" value="<?php echo $product['productID']; ?>">
                                    <input type="hidden" name="category_id" value="<?php echo $product['categoryID']; ?>">
                                    <input type="submit" value="Edit">

                                </form>                    
                            </td>

                        </tr>
                    <?php endforeach; ?>
                </table>
                <p><a href="add_product_form.php">Add Product</a></p>
                <p><a href="category_list.php">List Product</a></p>
            </section>
        </main>
        <hr>
        <footer><p>&copy; <?php echo date("Y"); ?> My Guitar Shop Inc</p></footer>
    </body>
</html>

出于安全目的,在database.php中删除了我的用户名和密码的php:

    <?php

$dsn = 'mysql:host=mysql.cit336.fullerview.net;dbname=cit336my_guitar_shop1';


try {
  $db = new PDO($dsn, $username, $password);
} catch (PDOException $e) {
  $error_message = $e->getMessage();
  include('database_error.php');
  exit();
}
?>

更新: 一个善良的灵魂帮助我解决了我的第一个问题,但是由于我编辑了文件,问题仍然存在于陈述中。它几乎就像网站要我删除SQL语句但我不想删除它们。它们对网站至关重要。

更新2:

我编辑了database.php文件以使PDO异常正常工作。但是现在,随着我越来越接近我的目标,我得到了拒绝访问的错误。

enter image description here

更新3:

我能够访问数据库。谢谢大家的帮助,非常感谢。我刚刚输入了密码输入错误和数据库错字,再次感谢您的帮助!

1 个答案:

答案 0 :(得分:2)

由于你使用mysqli的程序功能来连接->prepare不应该是mysqli_prepare?

bindValue也适用于PDO,您的代码可能是

$statement1 = mysqli_prepare($link, "SELECT * FROM categories WHERE categoryID =?");
mysqli_stmt_bind_param($statement1, "s", $category_id);
mysqli_stmt_execute($statement1);
mysqli_stmt_bind_result($statement1, $category);
mysqli_stmt_fetch($statement1);