0

I create simple add to cart system using php mysqli when user click that button ( add to cart ) they will be redirected to view cart page. Where is the problem? My system working nice, but on final page ( view single article ) i have displayed several buttons ( add to cart ). If i have in database 2 products, on final page ( product page ) i wil get 2 buttons ( add to cart ) and that is what i don't need. I need to display only button of that product. Example:

I have 2 products in 2 diferent categories and their link is:

  1. https://website.com/en/product-category/product.php?id=23
  2. https://website.com/en/product-category/product.php?id=35

When user open product with id 23 on that page will display button from article 23 and article 35. And i don't need that. I need on product with id 23 display only button for that article.

On top page code:

//initialize cart if not set or is unset
    if(!isset($_SESSION['cart'])){
        $_SESSION['cart'] = array();
    }

    //unset qunatity
    unset($_SESSION['qty_array']);

This is my code:

<?php
$conn = new mysqli('localhost', 'root', 'password', 'database');
$sql = "SELECT * FROM post";
$query = $conn->query($sql);
$inc = 4;
while($row = $query->fetch_assoc()){
$inc = ($inc == 4) ? 1 : $inc + 1; 
if($inc == 1) echo "<div class='row text-center'>";  
?>
<a href="../../../en/add_cart.php?post_id=<?php echo $row['post_id']; ?>" target="">Add to cart</a>
<?php
}
if($inc == 1) echo "<div></div><div></div><div></div></div>"; 
if($inc == 2) echo "<div></div><div></div></div>"; 
if($inc == 3) echo "<div></div></div>";
//end product row 
?>

Where is problem and how to fix to display only single button ( add to cart ) not all 2 or ? buttons?

Nick
  • 138,499
  • 22
  • 57
  • 95
  • If you're just getting started with PHP and want to build applications, I'd strongly recommend looking at various [development frameworks](https://www.cloudways.com/blog/best-php-frameworks/) to see if you can find one that fits your style and needs. They come in various flavors from lightweight like [Fat-Free Framework](https://fatfreeframework.com/) to far more comprehensive like [Laravel](http://laravel.com/). These give you concrete examples to work from and guidance on how to write your code and organize your project's files. – tadman Nov 12 '19 at 22:06
  • You don't seem to be filtering the results anywhere by an id. You need to implement that. – Dharman Nov 12 '19 at 22:59

1 Answers1

-2

Edit this line of code:

$sql = "SELECT * FROM post";

to:

$sql = "SELECT * FROM post WHERE post_id = '".$_GET['id']."'";
  • This employs SQL injection which is super bad, even for examples. Placeholder values are a necessary thing. – tadman Nov 12 '19 at 22:06
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Nov 12 '19 at 22:57