Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Whats wrong with My code?
I tried to update quantity for each product in shopping cart, but it's not working for every product in list separately.

PHP
<?php
  global $con;
  
  include("includes/db.php");
  
  $ip = getIp();

  if(isset($_POST['update_cart'])){
  
  $qty = $_POST['qty'];

  $update_qty = "update cart set qty='$qty' where ip_add='$ip'";
  
  $run_qty = mysqli_query($con, $update_qty);

  $_SESSION['qty']=$qty;

  $total = $total*$qty;
  
  }
?>


What I have tried:

PHP
<?php				
  global $con;
					
  include("includes/db.php");
					
  $ip = getIp();

  if(isset($_POST['update_cart'])){
					
    $qty = $_POST['qty'];

    $update_qty = "update cart set qty='$qty' where ip_add='$ip'";
					
    $run_qty = mysqli_query($con, $update_qty);

    $_SESSION['qty']=$qty;

    $total = $total*$qty;
					
  }
?>
Posted
Updated 21-Sep-17 15:02pm
v2
Comments
eddieangel 21-Sep-17 16:05pm    
Did you query the database to ensure that all of the items in the cart actually have the correct ip? If so is the database being updated correctly but not your session variable?
Member 13422296 22-Sep-17 7:49am    
You are right.
So please suggest me the right code.
I will be thankful to you.

And thanks for responding
Mohibur Rashid 21-Sep-17 21:20pm    
Your question is not clear enough. And Your code does how you wrote it, if ip_add is unique, it's not going to update more than one item.
<
mysqli_query($con, $update_qty); will return number rows those are being updated.
Member 13422296 22-Sep-17 7:48am    
So please suggest me the right code.
I will be thankful to you.
Member 13422296 22-Sep-17 7:49am    
And thank you very much for responding.💐

1 solution

Quote:
I tried to update quantity for each product in shopping cart, but it's not working for every product in list separately.

As I understand your query, it update the quantity of all items in cart because you don't say which item (in cart) you want to update.
-----
Question: you use the user public ip as identification, but what happen when 2 users working in same company have same public ip ?
-----
PHP
$update_qty = "update cart set qty='$qty' where ip_add='$ip'";

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
Share this answer
 
Comments
Member 13422296 22-Sep-17 7:46am    
So please suggest me the right code.
I will be thankful to you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900