Yerassyl Zhassuzakhov
Yerassyl Zhassuzakhov

Reputation: 11

PHP, get the button by name which includes script

I have a problem while doing php session cart. I get the list of products from the database and each has own button "Add to cart". The problem is how to get any of them by button click to add into session?

<?php session_start();</br>
include_once 'dbconnect.php';
$_SESSION['cart']= array();
$sql="SELECT * FROM products ORDER BY name ASC";
$query=mysql_query($sql);?>
<a href="cart-display.php">Go To Cart</a>

I start a loop to output all products from database

<?php
while($row=mysql_fetch_array($query)){
?>

<form method="get">
<tr><td><?php echo $row['name'] ?></td>
<td><?php echo $row['description'] ?></td>
<td><?php echo $row['price'] ?></td>
<td><?php echo $elId= $row['id_product']?></td>
<td><button type="submit" name="btn-submit.<?php echo $elId;?>">Add To Chart</button></td>
</tr><br>
</form>

Then I try to get any of them by $_GET[], then try to push into array session. But it adds only the ast product because of the loop. Help please

<?php }

if(isset($_GET['btn-submit.$elId'])){
array_push($_SESSION['cart'], $elId);}?>

Upvotes: 1

Views: 45

Answers (1)

verjas
verjas

Reputation: 1793

You have to rethink the script:

  • on line 3 you completely empty the $_SESSION[cart] on every page load!
  • you use a deprecated function: mysql
  • you use unnecessary 'form' tags
  • you check for $_GET input only on the last product, in a bad way

Here is a start:

<?php
session_start();
// initialize session var only ONE TIME
if (!isset($_SESSION["cart"])) {
    $_SESSION["cart"]= array();
}
// store a new element ID
$elId="";
// check $_GET input and validate it
if(isset($_GET["elId"])){
    // !! filter $_GET data somehow (e.g. make sure it is_numeric)
    $elId=preg_replace("/[^0-9]/","",$_GET["elId"]);
    // you could check in DB if the $elId is a valid value
}
// update SESSION if elId is not empty after validating $_GET
if (!empty($elId)) {
    // if not already in array, initialize field
    /* 
    use a multidimensional array to count how many products 
    of one type are in the cart, eg: $_SESSION[cart][343]=3; 
    aka Three products with id: 343
    */
    if (!isset($_SESSION["cart"][$elId])) {
        $_SESSION["cart"][$elId]=0;
    }
    // update cart
    $_SESSION["cart"][$elId]++;
    // ! user should be alerted, e.g:
    echo "<p>New product added to cart!</p>";
}
// DB access
//include_once 'dbconnect.php';
require "dbconnect.php";
$sql="SELECT * FROM products ORDER BY name ASC";
// fetch results
// $query=mysql_query($sql);
/* !! USE mysqli INSTEAD !! in file dbconnect.php */
$conn = dbconnect();
$results = $conn->query($sql);
// start products' HTML
echo "<a href='cart-display.php'>Go To Cart</a>";
echo "<table>";
// again, use mysqli
// while($row=mysql_fetch_array($query)){
while($row = $results->fetch_array()) {
    // * <form> tags are unnecessary, <a> tags are sufficient
    $elId=$row["id_product"];
    echo "
        <tr>
        <td>$row[name]</td>
        <td>$row[description]</td>
        <td>$row[price]</td>
        <td>$row[id_product]</td>
        <td><a href='?elId=$elId'>Add to cart</a></td>
        </tr>
    ";
}
echo "</table>";

?>

And the dbconnect.php:

<?php
function dbconnect() {
    static $conn;
    if (!isset($conn)) {
        $conn = mysqli_connect('localhost','username','password','dbname');
    }
    if ($conn === false) {
        return mysqli_connect_error(); 
    }
    return $conn;
}
?>

Upvotes: 1

Related Questions