Laravel未定义索引:

时间:2015-01-24 12:00:44

标签: php mysql arrays laravel laravel-4

我正在使用我的laravel中的函数存在问题,看起来这是一个数组问题但是因为两天我无法弄清楚错误信息是什么问题

Undefined index: eventID

我的功能

public static function DeleteRow($id){
    $data=DB::select("select * from InOutProducts where eventID=$id;"); 
    $eventID=$data['eventID'];
    $productID=$data['productID'];
    $username=Session::get('key');  
    DB::delete("delete from InOutProducts where eventID=$eventID");
    $row=DB::select("select sum(quantity) as total_quantity from  InOutProducts  where productID='$productID' and InAndOut='1'");
    $PhysicalStock=$row[0]->total_quantity;
    DB::update("update productsStock set physicalStock=$PhysicalStock,lastUpdateBy='$username' where productID=$productID;");`

}

2 个答案:

答案 0 :(得分:1)

DB::select返回一个数组。试试这个:

$data = DB::select("select * from InOutProducts where eventID=$id;"); 
$data = $data[0];
$eventID = $data->eventID;

答案 1 :(得分:0)

如果你var_dump $ data,它很可能是null,

您必须从查询sting中删除半colin。

$data=DB::select("select * from InOutProducts where eventID=$id;"); 

到这个

$data=DB::select("select * from InOutProducts where eventID=$id"); 

建议,您的DeleteRow功能正在执行

  1. 选择
  2. 删除
  3. 选择
  4. 更新
  5. 你觉得在单一功能中有点太多了吗? 也许你可以看看Laravel如何支持查询数据库? Laravel query the database