0

I am trying hard to learn how to create this shop cart using php and seems to me that I got stuck once more. This time I just can not make the "additional conditioning to amount available in stock" to work well. Can anybody help me to figure out what is wrong with my php code below?

if (isset($_GET['add'])) {
    $con = mysqli_connect("localhost", "noivaemd_etalhes", "password", "***") or die (mysqli_error());
    $quantity = mysqli_query($con, "SELECT id, quantity FROM Products WHERE id=".mysqli_real_escape_string($con, $_GET['add']));
    echo '<p>'.$quantity_row['quantity'];
    while($quantity_row = mysqli_fletch_assoc ($quantity)) {
        if ($quantity_row['quantity']!=$_SESSION['cart_'.$_GET['add']]) {
            $_SESSION['cart_'.$_GET['add']]+='1';
        }
    }
}
0

1 Answer 1

1

Many things.

  1. You are using mysqli, which is a nice step up from mysql, but you're still injecting values into your query. Note that escape_string does NOT help here because you did not put quotes around the value, meaning you're still wide open to injections. Learn about parameterised queries.

  2. You misspelled fetch as fletch.

  3. You are using += with a string '1'. While PHP will correct this as an integer 1 via type coercion, it's still not a good thing to be writing because it makes it look like you lack understanding.

These are just the things visibly wrong with the code. If fixing these issues doesn't resolve the problem, then it would be useful to know any error messages you're getting.

Sign up to request clarification or add additional context in comments.

1 Comment

I am new on php. All feedback you guys giive me is helping me a lot. It is incredable how fast you guys can see my mistakes while I am taking hours trying to understand the codes and trying to figure out what is missing/wrong. Thank you for your feedback. Roberto.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.