Hi,
My program works on WAMP (PHP Version 5.5.12) running on my machine. However, the same program will not run on remote server (PHP Version 5.4.32). Basically, I'm trying to insert values obtained from a form into a database. I've checked and rechecked the table I'm inserting into and I can't find anything wrong with it. The trouble is it's hard to debug a PHP program. Unlike, say, C++ which requires a compiler, you can trace the program line by line to look for errors. That's not possible with PHP and makes it so much harder to look for faults.
The relevant functions used to send data to database are shown below. insert() is returning false every time and I can't understand why! I wonder if anyone can suggest where things might be going wrong. I would be very grateful.
public function insert($table, $fields = array())
{ if(count($fields)) //true if $fields holds data
{ $keys = array_keys($fields);
$values = ''; //to put inside query
$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, $darams = array())
{
$this->_error = false; //reset error
if($this->_query = $this->_pdo->prepare($sql))
{ $x = 1;
if(count($darams))
{ foreach($darams 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 //an error has occured in SQL query
{ $this->_error = true;
}
}
return $this;
}
public function error()
{ return $this->_error;
}
public function count()
{ return $this->_count;
}