0

There were many times I faced this problem. The format is correct (.php), connection to the localhost server seems to be fine, cause some of the .php insertions are working.

For example(here .php insertion is working),

...(some html)...<div class="menu">
                <ul>
                    <li><a href="#" class="active">Главная</a></li>
                    <li><a href="#">Товары</a></li>
                    <li><a href="#">Контакты</a></li>
                    <li><a href="#">FAQ</a></li>
                    <li><a href="#"><i class="fas fa-shopping-cart"></i></a></li>
                    <li><a href="registration.php"><i class="fas fa-user"></i>
                        <?php
                        echo($_COOKIE['client']);
                        ?>
                    </a></li>
                </ul>
            </div>...(some html)..

echo is working

The same file when I'm trying other .php insertion stops loading the page and finishes right before <?php...

To see what I'm talking about I made some screenshots browser trnascript full html file

As you see, browser ignores and leaves empty all the code after and including <?php...

To be exact:

<div class="container">
        <div class="row row-eq-height text-center py-5 px-4">
            <?php
                $result = $database->getData();
                while ($row = mysqli_fetch_assoc($result)){
                component($row['product_name'], $row['product_price'], $row['product_img'], $row['id']);
                }
            ?>
        </div>  
    </div>

Browser reads this file until the start of php(<?php) and doesn't display anything after

I'm a beginner trying to build a website, maybe there is something I missed Please help!!!

getData should return all the information from the table

// get product from the database
//tablename refers to my own existing table with products
        public function getData(){
        $sql = "SELECT * FROM $this->tablename";

        $result = mysqli_query($this->con, $sql);

        if(mysqli_num_rows($result) > 0){
            return $result;
        }
    }

Then I'm using loop shown above to fill the product sample

<?php
function component($productname, $productprice, $productimg, $productid){
    $element = "
    
    <div class=\"col-md-3 col-sm-6 my-3 my-md-0\">
                <form action=\"index.php\" method=\"post\">
                    <div class=\"card shadow\">
                        <div>
                            <img src=\"$productimg\" alt=\"Image1\" class=\"img-fluid card-img-top\">
                        </div>
                        <div class=\"card-body\">
                            <h5 class=\"card-title\">$productname</h5>
                            <h6>
                                <i class=\"fas fa-star\"></i>
                                <i class=\"fas fa-star\"></i>
                                <i class=\"fas fa-star\"></i>
                                <i class=\"fas fa-star\"></i>
                                <i class=\"far fa-star\"></i>
                            </h6>
                            <p class=\"card-text\">
                                Some quick example text to build on the card.
                            </p>
                            <h5>
                                <small><s class=\"text-secondary\">$519</s></small>
                                <span class=\"price\">$$productprice</span>
                            </h5>
                            <button type=\"submit\" class=\"btn btn-warning my-3\" name=\"add\">Add to Cart <i class=\"fas fa-shopping-cart\"></i></button>
                             <input type='hidden' name='product_id' value='$productid'>
                        </div>
                    </div>
                </form>
            </div>
    ";
    echo $element;
}
?>
aynber
  • 22,380
  • 8
  • 50
  • 63
KabdeshK
  • 19
  • 6
  • 1
    You're probably getting some sort of internal server error. Check your server error logs to verify. – aynber Aug 25 '21 at 12:44
  • `doesn't display anything after`...probably the PHP crashed at this point. Enable PHP error reporting (or logging, if you prefer) and check for errors. https://stackify.com/php-error-logs-guide/ has a guide to enabling error handling. You also should enable mysqli's specific error handling option - see https://stackoverflow.com/a/22662582/5947043 for instructions. Beyond that, http://www.phpknowhow.com/basics/basic-debugging/ has a guide to debugging PHP code more generally. Debugging / error handling is a basic skill you should be learning alongside actually learning to write code. – ADyson Aug 25 '21 at 12:45
  • @ADyson, thanks! I turned on the logs and found a mistake in $sql request) – KabdeshK Aug 28 '21 at 14:33

0 Answers0