错误:未定义的变量

时间:2016-02-19 07:05:44

标签: php

这些是我得到的错误。

Notice: Undefined variable:
product_id in /Applications/XAMPP/xamppfiles/htdocs/www2/www.website2.com/admin_area/viewpost.php on line 74

Notice: Undefined variable:
cat_id in /Applications/XAMPP/xamppfiles/htdocs/www2/www.website2.com/admin_area/viewpost.php on line 75

Notice: Undefined variable: 
date in /Applications/XAMPP/xamppfiles/htdocs/www2/www.website2.com/admin_area/viewpost.php on line 76

Notice: Undefined variable:
product_title in /Applications/XAMPP/xamppfiles/htdocs/www2/www.website2.com/admin_area/viewpost.php on line 77

Notice: Undefined variable:
product_img1 in /Applications/XAMPP/xamppfiles/htdocs/www2/www.website2.com/admin_area/viewpost.php on line 78

Notice: Undefined variable: 
product_price in /Applications/XAMPP/xamppfiles/htdocs/www2/www.website2.com/admin_area/viewpost.php on line 79

Notice: Undefined variable:
product_desc in /Applications/XAMPP/xamppfiles/htdocs/www2/www.website2.com/admin_area/viewpost.php on line 80

Notice: Undefined variable: 
product_keywords in /Applications/XAMPP/xamppfiles/htdocs/www2/www.website2.com/admin_area/viewpost.php on line 81

这是代码

<form method="get" action="viewpost.php">
        <input type="text" name="search" placeholder="Search Product Here" style="border-color: #0CF; border-radius:5px;">
        <input class="btn btn-default" type="submit" value="Search Product" name="sub" style="border-color:#09F">
</form>

        <!--</div>
        </div>
        </div>
         -->


     <?php
    include("includes/connect.php");

    if(isset($_GET['sub'])){


    $search = $_GET['search'];

    $query ="select * from products where product_keywords='$search' ";

    $run=mysql_query($query);

    while($row=mysql_fetch_array($run)){
        $product_id= $row['product_id'];
        $cat_id= $row['cat_id'];
        $date= $row['date'];
        $product_title= $row['product_title'];
        $product_img1= $row['product_img1'];
        $product_price= $row['product_price'];
        $product_desc= substr($row['product_desc'],0,100);
        $product_keywords= $row['product_keywords'];
        }

       ?>
       <!--<table class="table-responsive">-->
    <tr>
    <td><?php echo $product_id; ?></td>
    <td><?php echo $cat_id; ?></td>
    <td><?php echo $date; ?></td>
    <td><?php echo $product_title; ?></td>
    <td><?php echo $product_img1; ?></td>
    <td><?php echo $product_price; ?></td>
    <td><?php echo $product_desc; ?></td>
    <td><?php echo $product_keywords; ?></td>
     </tr>

    </table>
    <?php } ?>

2 个答案:

答案 0 :(得分:0)

你最好这样做:

<?php    
  include("includes/connect.php");

  if(isset($_GET['sub'])){


  $search = $_GET['search'];

  $query ="select * from products where product_keywords='$search' ";

  $run=mysql_query($query);
?>
   <table class="table-responsive">
    <tr>
    <?php
        while($row=mysql_fetch_array($run)){
            echo '<td>'. $row['product_id']. '</td>';
            echo '<td>'. $row['cat_id']. '</td>';
            echo '<td>'. $row['date']. '</td>';
            echo '<td>'. $row['product_title']. '</td>';
            echo '<td>'. $row['product_img1']. '</td>';
            echo '<td>'. $row['product_price']. '</td>';
            echo '<td>'. substr($row['product_desc'],0,100). '</td>';
            echo '<td>'. $row['product_keywords']. '</td>';
            } 
     ?>
   </tr>
   </table>

答案 1 :(得分:0)

现在这只是猜测,但你的$ search可能是空的。这将导致查询&#34; SELECT * FROM产品WHERE product_keywords LIKE&#39;&#39;&#34;,返回0行。

修复将是:

<?php    
include("includes/connect.php");

if(isset($_GET['sub'])){


$search = $_GET['search'];
if (isset($search)){

  $query ="select * from products where product_keywords='$search' ";

  $run=mysql_query($query);
?>
<table class="table-responsive">
<tr>
<?php
    while($row=mysql_fetch_array($run)){
        echo '<td>'. $row['product_id']. '</td>';
        echo '<td>'. $row['cat_id']. '</td>';
        echo '<td>'. $row['date']. '</td>';
        echo '<td>'. $row['product_title']. '</td>';
        echo '<td>'. $row['product_img1']. '</td>';
        echo '<td>'. $row['product_price']. '</td>';
        echo '<td>'. substr($row['product_desc'],0,100). '</td>';
        echo '<td>'. $row['product_keywords']. '</td>';
        } 
     }
 ?>

   

试试这个,看它是否消失。如果没有,那就出现了其他问题。