【问题标题】:PHP variables undefined when passed to SQL statement [duplicate]传递给SQL语句时未定义PHP变量[重复]
【发布时间】:2017-05-27 18:58:47
【问题描述】:

在尝试整合这个网站时,我在编写一个允许版主添加产品的函数时遇到了问题。我已附上代码和收到的错误消息。

功能(来自 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; 


错误消息:

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


我不知道为什么变量会未定义,因为我在同一个文件中使用了具有相同语法的类似函数,这些函数运行良好。
我在代码中做错了什么?

【问题讨论】:

  • 函数是特定于范围的。
  • @chris85 我明白,但功能在范围内
  • 函数内部的变量不是。 $invName 从未在 insPro 等中定义。
  • 您需要将这些变量作为参数传递。
  • @chris85 “函数是特定于范围的” - 添加了第二个副本以覆盖所有基础。

标签: php mysql sql apache


【解决方案1】:

您的变量未定义,因为该函数不知道这些变量的存在。解决该问题的一种方法是将变量作为参数传递给您的函数。例如:

function insPro(在这里传递你的变量){你的代码在这里}

如果该方法在一个类中并且这些变量在类范围内可用,那么您不必将它们作为参数传递。您可以将变量修改为如下所示:

$this->invName

等等。

【讨论】:

  • 嗯,我觉得自己像个白痴...我不敢相信我没有把我的变量 insPro($all, $up, $in, $here){}
    谢谢.
  • 不客气。不要对自己那么苛刻。错误发生了。
猜你喜欢
  • 2021-09-28
  • 2023-03-25
  • 2013-07-15
  • 1970-01-01
  • 2015-08-08
  • 2014-08-22
  • 1970-01-01
  • 2014-01-11
  • 2016-02-23
相关资源
最近更新 更多