使用未定义的常量类别/非法字符串偏移量

时间:2017-06-03 04:25:12

标签: php apache

所以我试图从我的数据库中的一系列类别中为PHP网站构建一个动态下拉列表,它正在工作,我得到7个确切类型的错误(7个是我拥有的类别数量)。这是代码:

<?php 
$categories = getCategories();

$catList = '<select name="categoryId" id="categoryId">';
$catList .= '<option>Choose a Category</option>';

  foreach ($categories as $category){
    $catList .= "<option value='$category[categoryId]'"; 
      if(isset($categoryId)){
        if(category['categoryId'] === "$categoryId"){
          $catList .= ' selected ';
        } 
      }
    $catList .= ">$category[categoryName]</option>";
    }
$catList .= "</select>";
?>

以下是错误消息:

Notice: Use of undefined constant category - assumed 'category' in 
C:\my\path\to\the\addproduct.php on line 8

Warning: Illegal string offset 'categoryId' in 
C:\my\path\to\the\addproduct.php on line 8


作为参考,第8行是if(category['categoryId'] === $categoryId){

2 个答案:

答案 0 :(得分:1)

第8行有拼写错误。您忘记在类别前添加$。记住它是一个php变量。将其更改为以下

   if($category['categoryId'] === "$categoryId"){

答案 1 :(得分:1)

更改

if(category['categoryId'] === "$categoryId"){

if($category['categoryId'] === "$categoryId"){