I know this sounds like a duplicate, but I'm not looking to add multiple rows with a single query, I already have that working. The problem is that I need to only insert ONE row.
I have two pieces of code performing these actions. The first one - on the registration page:
$user = DB::getInstance()->insert('users', array(
'username' => 'Sam',
'password' => 'password',
'salt' => 'salt'
));
and then a the what actually performs the query in a separate php class:
public function insert($table, $fields = array()) {
$keys = array_keys($fields);
$values = '';
$x = 1;
foreach($fields as $field) {
$values .= "?";
if($x < count($fields)) {
$values .= ', ';
}
$x++;
}
$sql = "INSERT INTO {$table} (`" . implode('`, `', $keys) . "`) VALUES ({$values})";
if(!$this->query($sql, $fields)->error()) {
return true;
}
return false;
}public function query($sql, $params = array()) {
$this->_error = false;
if($this->_query = $this->_pdo->prepare($sql)) {
$x = 1;
if(count($params)) {
foreach($params as $param) {
$this->_query->bindValue($x, $param);
$x++;
}
}
if($this->_query->execute()) {
$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
$this->_count = $this->_query->rowCount();
} else {
$this->_error = true;
}
}
return $this;
}
Quite a bit there, but I just wanted to include everything. This is currently working, it enters records into my database, the only problem is that it enters 2 each time.
HERES A SCREENSHHOT OF MY DATABASE:
As you can see from the ID's I've been going over this code for a while now. I have a feeling something terrifically small is throwing it off but I just can't seem to find it and need help from a fresh pair of eyes.