警告:从空值PHP

时间:2016-07-22 18:50:26

标签: php oop

我知道有几个类似我的问题。但我已经完成了大部分工作,但发现只有类似的答案无法解决我的情况:

我正在使用OOP创建一个PHP网站,并希望从数据库中编辑记录。以下是edit_short.php的链接:

<td><a href="edit_short.php?id=<?php echo $short->id; ?>">Edit</a></td>

此记录ID在edit_short.php中收到,如下所示:

<?php
require_once("short.php");
$id = null;
// $short = new stdClass(); **I was told to use this but it didn't work**

if (isset($_GET['id'])) {
  $id=$_GET['id'];
  $short = Short::find_by_id($id);
}

if (isset($_POST['edit_short_btn'])) { 
  $short->title = $_POST['title']; //the error is on this line
  $short->short_text = $_POST['short_text'];
}

以下是我Short课程的相关部分:

public static function find_by_id($id=0) {
  global $database;
  $result_array = self::find_by_sql("SELECT * FROM short WHERE id=$id LIMIT 1");
  return !empty($result_array) ? array_shift($result_array) : false;
}

stackoverflow和其他网站上的一些答案我被告知要将$short定义为stdClass()的一个对象,但这里不适用,因为我的$short对象必须是Short课程。decimal.Parse(...)

非常感谢你的帮助..

1 个答案:

答案 0 :(得分:0)

find_by_id(...)返回false时,您似乎没有检查可能导致该警告的情况。

相关问题