【问题标题】:I am getting Warning:count():Parameter error我收到警告:计数():参数错误
【发布时间】:2021-03-15 06:51:10
【问题描述】:

我在下面的车辆更新.php 中收到此错误,并且我还附上了我的车辆/索引.PHP 代码。它出现在我的 phpmotors/index.php 页面上的标题之前。

警告:count(): 参数必须是数组或对象,在 C:\xammp_124\htdocs\phpmotors\vehicles\index.php 第 142 行实现 Countable 显示错误:if(count($invInfo)

<?php
/***********************************************
 * Vehicles  Controller
 *********************************************/

// Create or access a Session
session_start();

// Get the database connection file
require_once '../library/connections.php';
// Get the database connection file
require_once '../library/functions.php';
// Get the PHP Motors model for use as needed
require_once '../model/main-model.php';
// Get the accounts model
// require_once '../model/classification-model.php';
// Get the accounts model
require_once '../model/vehicles-model.php';


 // Get the array of classifications
 $classifications = getClassifications();

// var_dump($classifications);
// exit;
// Build a navigation bar using the $classifications array
 $navList = '<ul>';
 $navList .= "<li><a href='/index.php' title='View the PHP Motors home page'>Home</a></li>";
 foreach ($classifications as $classification) {
  $navList .= "<li> <a href='/vehicles/?action=classification&classificationName="
    .urlencode($classification['classificationName']).
    "' title='View our $classification[classificationName] lineup of vehicles'>$classification[classificationName]</a> </li>";
 }
 $navList .= '</ul>';

 $action = filter_input(INPUT_GET, 'action');
 if($action == NULL) {
     $action = filter_input(INPUT_POST, 'action');
 }

 switch ($action) {
    case 'add-classification':
      if (isset($_SESSION['loggedin']) && $_SESSION['clientData']['clientLevel']>1){
        include '../view/add-classification.php';
      } else {
       header('Location: /index.php');
      }
      break;
      
    case 'add-vehicle':
      if (isset($_SESSION['loggedin']) && $_SESSION['clientData']['clientLevel']>1){
        include '../view/vehicles-update.php';
      } else {
       header('Location: /index.php');
      }
      break;

    case 'adding-classification':
        // Filter and store the data
        $classificationName = filter_input(INPUT_POST, 'classificationName', FILTER_SANITIZE_STRING);
      // Check for missing data
      if(empty($classificationName)){
        $message = '<p>Please provide information for all empty form fields.</p>';
        include '../view/add-classification.php';
        exit;
      }
      
      // Send the data to the model
      $regOutcome = regClassification($classificationName);
      
      // Check and report the result
      if($regOutcome === 1){
        $message = "<p>Thanks for registering $classificationName.</p>";
        // Get the array of classifications
        $classifications = getClassifications();
        // Build a navigation bar using the $classifications array
        $navList = '<ul>';
        $navList .= "<li><a href='/index.php' title='View the PHP Motors home page'>Home</a></li>";
        foreach ($classifications as $classification) {
          $navList .= "<li> <a href='/vehicles/?action=classification&classificationName="
            .urlencode($classification['classificationName']).
            "' title='View our $classification[classificationName] lineup of vehicles'>$classification[classificationName]</a> </li>";
         }
        $navList .= '</ul>';
        include '../view/add-classification.php';
        exit;
      } else {
        $message = "<p>Sorry $classificationName, but the registration failed. Please try again.</p>";
        include '../view/add-classification.php';
        exit;
      }

      
      break;
     case 'adding-vehicle':
         // Filter and store the data
        $invMake = filter_input(INPUT_POST, 'invMake', FILTER_SANITIZE_STRING);
        $invModel = filter_input(INPUT_POST, 'invModel', FILTER_SANITIZE_STRING);
        $invDescription = filter_input(INPUT_POST, 'invDescription', FILTER_SANITIZE_STRING);
        $invImage = filter_input(INPUT_POST, 'invImage', FILTER_SANITIZE_STRING);
        $invThumbnail = filter_input(INPUT_POST, 'invThumbnail', FILTER_SANITIZE_STRING);
        $invPrice = filter_input(INPUT_POST, 'invPrice', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
        $invStock = filter_input(INPUT_POST, 'invStock', FILTER_SANITIZE_NUMBER_INT);
        $invColor = filter_input(INPUT_POST, 'invColor', FILTER_SANITIZE_STRING);
        $classificationId = filter_input(INPUT_POST, 'classificationId', FILTER_SANITIZE_NUMBER_INT);
        // Check for missing data
        if(empty($invMake)|| empty($invModel)||empty($invDescription)||empty($invImage)||empty($invThumbnail)||empty($invPrice)||empty($invStock)||empty($invColor)||empty($classificationId) ){
          $message = '<p>Please provide information for all empty form fields.</p>';
          include '../view/add-vehicle.php';
          exit;
        }
        
        // Send the data to the model
        $regOutcome = regVehicle($invMake, $invModel, $invDescription, $invImage, $invThumbnail, $invPrice, $invStock, $invColor, $classificationId);
        
        // Check and report the result
        if($regOutcome === 1){
          $message = "<p>Thanks for registering $invModel.</p>";
          include '../view/add-vehicle.php';
          exit;
        } else {
          $message = "<p>Sorry $classificationName, but the registration failed. Please try again.</p>";
          include '../view/add-vehicle.php';
          exit;
        }
        break;
    case 'vehicle':
      if (isset($_SESSION['message'])) {
        $message = $_SESSION['message'];
       }
      if (isset($_SESSION['loggedin'])  && $_SESSION['clientData']['clientLevel']>1){
        $classificationList = buildClassificationList($classifications);
        include '../view/vehicle-man.php';
      } else {
       header('Location: /index.php');
      }
      break;
    
    case 'mod':
      $invId = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
      $invInfo = getInvItemInfo($invId);
      if(count($invInfo)<1){
        $message = 'Sorry, no vehicle information could be found.';
      }
      include '../view/vehicle-update.php';
      exit;
      break;

    /* * ********************************** 
    * Get vehicles by classificationId 
    * Used for starting Update & Delete process 
    * ********************************** */ 
    case 'getInventoryItems': 
      // Get the classificationId 
      $classificationId = filter_input(INPUT_GET, 'classificationId', FILTER_SANITIZE_NUMBER_INT); 
      // Fetch the vehicles by classificationId from the DB 
      $inventoryArray = getInventoryByClassification($classificationId); 
      // Convert the array to a JSON object and send it back 
      echo json_encode($inventoryArray); 
      break;
    
    case 'updateVehicle':
      $classificationId = filter_input(INPUT_POST, 'classificationId', FILTER_SANITIZE_NUMBER_INT);
      $invMake = filter_input(INPUT_POST, 'invMake', FILTER_`enter code here`SANITIZE_STRING);
      $invModel = filter_input(INPUT_POST, 'invModel', FILTER_SANITIZE_STRING);
      $invDescription = filter_input(INPUT_POST, 'invDescription', FILTER_SANITIZE_STRING);
      $invImage = filter_input(INPUT_POST, 'invImage', FILTER_SANITIZE_STRING);
      $invThumbnail = filter_input(INPUT_POST, 'invThumbnail', FILTER_SANITIZE_STRING);
      $invPrice = filter_input(INPUT_POST, 'invPrice', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
      $invStock = filter_input(INPUT_POST, 'invStock', FILTER_SANITIZE_NUMBER_INT);
      $invColor = filter_input(INPUT_POST, 'invColor', FILTER_SANITIZE_STRING);
      $invId = filter_input(INPUT_POST, 'invId', FILTER_SANITIZE_NUMBER_INT);
      
      if (empty($classificationId) || empty($invMake) || empty($invModel) || empty($invDescription) || empty($invImage) || empty($invThumbnail) || empty($invPrice) || empty($invStock) || empty($invColor)) {
      $message = '<p>Please complete all information for the updated item! Double check the classification of the item.</p>';
      include '../view/vehicle-update.php';
      exit;
      }
      $updateResult = updateVehicle($invMake, $invModel, $invDescription, $invImage, $invThumbnail, $invPrice, $invStock, $invColor, $classificationId, $invId);
      if ($updateResult) {
        $message = "<p class='notify'>Congratulations, the $invMake $invModel was successfully updated.</p>";
        $_SESSION['message'] = $message;
        header('location: /vehicles/index.php?action=vehicle');
        exit;
       } else {
        $message = "<p>Error. The vehicle was not updated.</p>";
        include '../view/vehicle-update.php';
        exit;
      }
      break;
  case 'del':
    $invId = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
    $invInfo = getInvItemInfo($invId);
    if (count($invInfo) < 1) {
        $message = 'Sorry, no vehicle information could be found.';
      }
      include '../view/vehicle-delete.php';
      exit;
      break;
  case 'deleteVehicle':
    $invMake = filter_input(INPUT_POST, 'invMake', FILTER_SANITIZE_STRING);
    $invModel = filter_input(INPUT_POST, 'invModel', FILTER_SANITIZE_STRING);
    $invId = filter_input(INPUT_POST, 'invId', FILTER_SANITIZE_NUMBER_INT);
    
    $deleteResult = 'deleteVehicle'($invId);
    if ($deleteResult) {
      $message = "<p class='notice'>Congratulations the, $invMake $invModel was   successfully deleted.</p>";
      $_SESSION['message'] = $message;
      header('location: /vehicles/index.php?action=vehicle');
      exit;
    } else {
      $message = "<p class='notice'>Error: $invMake $invModel was not
    deleted.</p>";
      $_SESSION['message'] = $message;
      header('location: /vehicles/index.php?action=vehicle');
      exit;
    }
    break;
    case 'classification':
      $classificationName = filter_input(INPUT_GET, 'classificationName', FILTER_SANITIZE_STRING);
      $vehicles = getVehiclesByClassification($classificationName);
      if(!count($vehicles)){
       $message = "<p class='notice'>Sorry, no $classificationName could be found.</p>";
      } else {
       $vehicleDisplay = buildVehiclesDisplay($vehicles);
      }
      include '../view/classification.php';
      break;
    case 'vehicleDetails':
      $invId = filter_input(INPUT_GET, 'invid', FILTER_VALIDATE_INT);
      $vehicleData = getInvItemInfo($invId);
      $_SESSION['vehicleData'] = $vehicleData;
      include '../view/vehicle-detail.php';
      break;
    default:
      $classificationList = buildClassificationList($classifications);
      include '../view/vehicle-man.php';
      break;
   
}

【问题讨论】:

  • 欢迎来到 Stack Overflow。对未来的一点建议:阅读how to ask 以撰写质量问题。您已将大量代码粘贴到与问题无关的问题中。您应该只显示发生错误的几行代码以及获取值的函数 (getInvItemInfo)。话虽如此,错误很简单——你从那个函数得到的不是一个数组,所以count在抱怨。

标签: php sql arrays database


【解决方案1】:

看看你的函数getInvItemInfo。如果找不到记录,该函数会返回什么?如果它返回 NULL,您将收到一个错误,因为您正在尝试 count(NULL)

尝试使用函数is_null() 而不是count()

【讨论】:

    猜你喜欢
    • 2012-06-20
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 2017-05-28
    • 2011-11-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    相关资源
    最近更新 更多