[RESOLVED] mysql_query is performing several queries instead of just one
Hi all,
I have been writing my website and have ended up starting a sort of MVC (model-view-controller) framework, which has made lots of sense to write.
The problem I have is in my basic model. When I want it to insert a new record, instead of inserting one row, it inserts about five, all with the same properties (except the IDs, which is dealt with by MySQL itself). I checked the SQL query, and it is correct. I tested it by writing it directly into PHPMyAdmin and it only inserted one row.
The model's method looks like this:
PHP Code:
function insert() {
$sql = "INSERT INTO " . $this->table . "
(";
foreach($this->fields as $attr) {
if(!$this->blacklisted($attr) && $attr != $this->primaryKey) {
$sql .= $attr . ", ";
}
}
$sql = substr_replace($sql, "", -2);
$sql .= ") VALUES (";
foreach($this->fields as $attr) {
if(!$this->blacklisted($attr) && $attr != $this->primaryKey) {
$sql .= "'" . $this->{$attr} . "', ";
}
}
$sql = substr_replace($sql, "", -2);
$sql .= ")";
$this->con->query($sql);
}
The connector class for the database has a query() method which looks as follows:
PHP Code:
function query($sql = null) {
$sql = str_replace(array("\"", "\\"), array("'", ""), $sql);
$this->result = mysql_query($sql) or $this->error("Unable to query database: " . mysql_error($this->con) . ".");
return $this->result;
}
The SQL produced (using die($sql)) looks like this:
Code:
INSERT INTO artworks (name, date, modified, type, category, collection, comment, public, keywords) VALUES ('test1', '2009-08-12', '2009-08-12', '1', '', '', 'This is a test.', '', 'test, first')
This really is quite confusing, any help would be much appreciated.