-2

My ajax-call posts the data loadingaid1 along with some other data. In my php-file I then request the value and assign it to $loadingaid1. So far so good. What I know want to do is to call the function getProductId which selects the respective id out of my database and override the variable $loadingaid1 with that ID.

The code I tried is the following:

(...)
$loadingaid1 = $_REQUEST['loadingaid1'];
$loadingaid1 = getProductId($loadingaid1);
(...)

function getProductId($product) {
   $stmt = $conn->prepare('SELECT idproducts FROM products WHERE title = :product LIMIT 1');
   if ($stmt->execute(array(':product' => $product))) {
      $row = $stmt->fetch(PDO::FETCH_ASSOC);
      return $row['idproducts'];
   };
}

But somehow the code always fails as soon as I bring the function into the game. What am I doing wrong?

The prepared statement is correct - already compared it several times to my column & table names and working statements that basically do the same thing. And the requested value saved in loadingaid1 also exists in the table.

Dharman
  • 30,962
  • 25
  • 85
  • 135
iMorces
  • 11
  • 3
  • 1
    `$conn` is undefined – Dharman Nov 12 '21 at 12:53
  • Identifying products by their title is not a good idea to start with. Always use the product id. – KIKO Software Nov 12 '21 at 12:54
  • Try adding global $conn; as the first line in the function and the existing code under it. – PeteB Nov 12 '21 at 12:54
  • @Dharman $conn get's defined at the beginning within a connect.php file that opens the connection to the database. That's not the issue in this case. – iMorces Nov 12 '21 at 12:55
  • 1
    "the code always fails" - what does that mean? Is there any error message you can share? Also, be warned that `getProductId` does not return anything in all cases – Nico Haase Nov 12 '21 at 12:56
  • Then please tell us what the error is and show us where in that function `$conn` is defined. You should pass this value as an argument – Dharman Nov 12 '21 at 12:56
  • @iMorces it might be defined, but it's outside of the scope of your function. Add it as a global like I suggested. – PeteB Nov 12 '21 at 12:56
  • @KIKOSoftware The title gets posted by an autocomplete input field. There's really only one option the user can pick of a product. But yes, I agree it's not the best of ideas. – iMorces Nov 12 '21 at 12:56
  • Also remove `if` statement. `execute` should always return true unless you disable PDO error reporting. – Dharman Nov 12 '21 at 12:57
  • 1
    @PeteB that did the trick! Don't yet fully understand why but I'll definitely look into that! Thanks so much for everyones help guys! – iMorces Nov 12 '21 at 12:58
  • 1
    Now, please stop using `global`. It is a bad practice. Read the answer I linked and learn how to pass variables as arguments to your function. – Dharman Nov 12 '21 at 12:59
  • If you use an auto-complete input field, then there must be a trick to add in the product id as well. It would save you a whole query if you find it. – KIKO Software Nov 12 '21 at 13:00
  • @iMorces functions can only use the variables you pass to it or that you create within it. Variables created outside of a function are beyond the variables scope (out of reach). setting a global within the function allows a variable declared outside to be usable inside. – PeteB Nov 12 '21 at 13:02
  • @iMorces P.S. would appreciate it if you can upvote some of my comments, I need the points ;) – PeteB Nov 12 '21 at 13:04
  • 1
    @PeteB you don't get any points for upvoted comments, that only works for answers – Nico Haase Nov 12 '21 at 13:17

1 Answers1

0

$conn is undefined!!!!!!! In the scope of your function, $conn is undefined!!! Make it global if you want:

Declare it as global in your connection.php

global $conn = ......

And inside your function

function getProductId($product) {
   global $conn;

   $stmt = $conn->prepare('SELECT idproducts FROM products WHERE title = :product LIMIT 1');
   if ($stmt->execute(array(':product' => $product))) {
      $row = $stmt->fetch(PDO::FETCH_ASSOC);
      return $row['idproducts'];
   };
}

That's the simple way, but maybe you don't want to use global. Here you are another way: define a class with a method that returns your $conn object. I let this on your own. And your getProductId() function would be:

function getProductId($product) {
       $stmt = Db::getInstance()->prepare('SELECT idproducts FROM products WHERE title = :product LIMIT 1');
       if ($stmt->execute(array(':product' => $product))) {
          $row = $stmt->fetch(PDO::FETCH_ASSOC);
          return $row['idproducts'];
       };
    }
José Carlos PHP
  • 1,417
  • 1
  • 13
  • 20