2017-02-07 2 views
1

내 웹 사이트에 데이터베이스 테이블을 표시하는 데 어려움을 겪고 있습니다. 내 index.php 파일에있는 쿼리를 실행할 수 없지만 XAMPP를 사용하여 localhost에 테이블을 표시 할 수 있습니다. 파일에있는 MySQL 문 코드도 실행할 수 없습니다. 도와주세요! 내가 코드를 주석 한 수행 할 때라이브 웹 사이트의 PHP 오류

enter image description here

이것은 : 내가하지 주석 문 코드가있을 때

은 심지어 테이블에 데이터가 enter image description here

그 I 에 연결하려고합니다. 여기에 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(); 
} 
?> 

UPDATE : 종류의 영혼은 내 첫 번째 문제로 날 도움이 하지만 파일을 편집 할 때 문제는 여전히 명령문 내에 있습니다. 거의 사이트처럼 SQL 문을 제거하길 원하지만 제거하고 싶지는 않습니다. 그들은 사이트에 필수적입니다.

업데이트 2 : 나는 작업 PDO 예외를 얻기 위해 database.php 파일을 편집 한

. 그러나 이제는 목표에 가까워 질수록 액세스 거부 오류가 발생합니다.

enter image description here

UPDATE 3 :

나는 데이터베이스에 액세스 할 수 있었다. 도와 주셔서 감사합니다. 감사합니다. 방금 오타 및 데이터베이스 오타를 만들었습니다. 다시 한 번 도움 주셔서 감사합니다!

+0

'$의 hostname'은 무엇입니까 될 수

는 또한 bindValue는 PDO입니다? 너는 그것을 보여주지 않는다. 아마도 그것은 오류를 던지는 줄입니다. –

+0

$ categories 배열의 print_r을 시도 했습니까? 거기에 실제로 배열이 있는지 확인하려면? –

+0

@RobbieAverill, 그것은 database.php 파일의 일부입니다. 치명적인 오류가'$ db'에 있었을 때 그것은 잘못된 인수 였음을 보여줍니다. – HawkBlade124

답변

2

연결에 mysqli의 절차 함수를 사용하면 ->preparemysqli_prepare?이 아니어야합니다. 코드가

$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); 
+0

고마워 토마스, 나는 웹 사이트를 보여주는 것에 아주 가까이있다. 이것이 내가 지금보고있는 것입니다 : [link] (http://www.cit336.fullerview.net/exercises/ch04_ex1en/index.php) – HawkBlade124

+0

이것이 답이라면, OP는 이것을 대답으로 받아 들여야합니다. – Deep

+1

그럼 액세스가 거부되었다고합니다. 사용자가 잘못되었거나 암호 분실 또는 권한 문제가있을 수 있습니다. 포트가 맞습니까? 왜냐하면 그 서버에 대한 포트 3306의 응답이 보이지 않기 때문입니다. –

관련 문제