1

I have a query method like this:

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
  }
}

Then an insert method like this:

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

Then I instantiate the class $c = new classTwo; and call the method(both are in a different php file, but in the same folder as the class files) $c->insertStuff(1, "This is a test");.

I get no erros, the table doesn't get any fields, not even empty ones. However using the method directly with hardcoded values like:

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

It will work. Then I tried to replace the hardcoded values 1 by one with the argument to see when it stops working, it accepts the NOW() field and successfully inserts a new row, but as soon as I use $text which references to my method call in the other file, it stops, doesn't add anything.

All the required files are required and it should throw an error if some werent. I'm confused.

4

1 回答 1

1

You need to add ' before and after strings in the query

fix code:

$this->query("INSERT INTO Tablename (id, text, time) VALUES ($id, '$text', NOW())");
于 2013-03-10T14:30:56.653 回答