无法弄清楚为什么插入db不起作用?

时间:2013-03-10 14:26:18

标签: php class methods insert

我有一个像这样的查询方法:

class classOne {
  // Relevant variables
  protected $db, $result;

  public function query ($sql) {
    $this->result = $this->db->query($sql);
    // So, run mysql query provided by the argument
  }
}

然后是这样的插入方法:

class classTwo extends classOne {
  public function insertStuff ($id, $text) {
    $this->query("INSERT INTO Tablename (id, text, time) VALUES ($id, $text, NOW())");
  }
}

然后我实例化类$c = new classTwo;并调用方法(两者都在不同的php文件中,但与类文件位于同一文件夹中)$c->insertStuff(1, "This is a test");

我没有错误,表没有任何字段,甚至没有空字段。但是直接使用该方法使用硬编码值,如:

public function insertStuff ($id, $text) {
    $this->query("INSERT INTO Tablename (id, text, time) VALUES (1, "Some text", NOW())");
 }

它会起作用。然后我尝试用参数逐个替换硬编码值以查看它何时停止工作,它接受NOW()字段并成功插入新行,但是一旦我使用$text引用在我的方法调用另一个文件,它停止,不添加任何东西。

所有必需的文件都是必需的,如果有些文件没有,则应该抛出错误。我很困惑。

1 个答案:

答案 0 :(得分:1)

您需要在查询中添加'前后字符串

修复代码:

$this->query("INSERT INTO Tablename (id, text, time) VALUES ($id, '$text', NOW())");
相关问题