-1

In trying to throw together this website, I've ran into a problem when writing a function to allow moderators to add products. I have attached the code and the error messages I'm getting.

FUNCTION (from product-model.php):

function insPro(){

// Create a connection object using the acme connection function
   $db = acmeConnect();
// The SQL statement
   $sql = 'INSERT INTO inventory (invName, invDescription, invImage, invThumbnail, invPrice, invStock, invSize, invWeight, invLocation, categoryId, invVendor, invStyle)
       VALUES (:invName, :invDescription, :invImage, :invThumbnail, :invPrice, :invStock, :invSize, :invWeight, :invLocation, :categoryId, :invVendor, :invStyle)';
// Create the prepared statement using the acme connection
   $stmt = $db->prepare($sql);
   $stmt->bindValue(':invName', $invName, PDO::PARAM_STR);
   $stmt->bindValue(':invDescription', $invDescription, PDO::PARAM_STR);
   $stmt->bindValue(':invImage', $invImage, PDO::PARAM_STR);
   $stmt->bindValue(':invThumbnail', $invThumbnail, PDO::PARAM_STR);
   $stmt->bindValue(':invPrice', $invPrice, PDO::PARAM_STR);
   $stmt->bindValue(':invStock', $invStock, PDO::PARAM_STR);
   $stmt->bindValue(':invSize', $invSize, PDO::PARAM_STR);
   $stmt->bindValue(':invWeight', $invWeight, PDO::PARAM_STR);
   $stmt->bindValue(':invLocation', $invLocation, PDO::PARAM_STR);
   $stmt->bindValue(':categoryId', $categoryId, PDO::PARAM_STR);
   $stmt->bindValue(':invVendor', $invVendor, PDO::PARAM_STR);
   $stmt->bindValue(':invStyle', $invStyle, PDO::PARAM_STR);
// Insert the data
   $stmt->execute();
// Ask how many rows changed as a result of our insert
   $rowsChanged = $stmt->rowCount();
// Close the database interaction
   $stmt->closeCursor();
// Return the indication of success (rows changed)
   return $rowsChanged; 


ERROR MESSAGES:

Notice: Undefined variable: invName in C:\CIT336\htdocs\acme\model\product-model.php on line 37

Notice: Undefined variable: invDescription in C:\CIT336\htdocs\acme\model\product-model.php on line 38

Notice: Undefined variable: invImage in C:\CIT336\htdocs\acme\model\product-model.php on line 39

Notice: Undefined variable: invThumbnail in C:\CIT336\htdocs\acme\model\product-model.php on line 40

Notice: Undefined variable: invPrice in C:\CIT336\htdocs\acme\model\product-model.php on line 41

Notice: Undefined variable: invStock in C:\CIT336\htdocs\acme\model\product-model.php on line 42

Notice: Undefined variable: invSize in C:\CIT336\htdocs\acme\model\product-model.php on line 43

Notice: Undefined variable: invWeight in C:\CIT336\htdocs\acme\model\product-model.php on line 44

Notice: Undefined variable: invLocation in C:\CIT336\htdocs\acme\model\product-model.php on line 45

Notice: Undefined variable: categoryId in C:\CIT336\htdocs\acme\model\product-model.php on line 46

Notice: Undefined variable: invVendor in C:\CIT336\htdocs\acme\model\product-model.php on line 47

Notice: Undefined variable: invStyle in C:\CIT336\htdocs\acme\model\product-model.php on line 48

Fatal error: Uncaught PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'invName' cannot be null in C:\CIT336\htdocs\acme\model\product-model.php:50 Stack trace: #0 C:\CIT336\htdocs\acme\model\product-model.php(50): PDOStatement->execute() #1 C:\CIT336\htdocs\acme\products\index.php(158): insPro('Baseball', 'A round ball wi...', 'no-image.png', 'no-image.png', '5', '24', '6', '6', 'San Jose', '15', 'Diamond', 'Nylon') #2 {main} thrown in C:\CIT336\htdocs\acme\model\product-model.php on line 50


I don't know why the variables would be undefined because I am using similar functions with the same syntax in the same file that are working fine.
What am I doing wrong in the code?

5
  • Functions are scope specific. Commented May 27, 2017 at 19:01
  • @chris85 I understand, but the functions are within scope Commented May 27, 2017 at 19:02
  • The variables inside the function are not. $invName is never defined in insPro, etc. Commented May 27, 2017 at 19:02
  • You need to pass those variables as parameters. Commented May 27, 2017 at 19:03
  • @chris85 "Functions are scope specific" - To which a second duplicate was added in order to cover all the bases. Commented May 27, 2017 at 19:24

1 Answer 1

1

Your variables are undefined because that function has no knowledge that those exist. One way to fix that issue would be to pass the variables as parameters to your function. For example:

function insPro(pass your variables here){your code goes here}

If that method is within a class and those variables are available within the class scope then you do not have to pass those as parameters. You would modify the variables to read like this:

$this->invName

and so on.

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

2 Comments

Well I feel like a moron...I can't believe I didn't put my variables insPro($all, $up, $in, $here){}<br> Thank you.
You are welcome. Don't be so hard on yourself. Mistakes happen.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.