I am trying to copy a column of data (which is numeric) to a different column (VARCHAR) in the same table. Seems straight forward to just run an update query and set the varchar column to whatever the $_POST column will be. The problem is numeric columns will contain 5 trailing zeros. I need to reformat it to be an INT type in the copy as well. Here is a copy of the original query in PHP:
try{
$q = "UPDATE table1 SET text1 = " . $_POST["htmlinputfield"] .
" WHERE account_id = :aid AND fieldapplication_id = :fid";
$stmt = $this->app->db->prepare($q);
$stmt->execute(array(":fid"=>$this->app->fid,":aid"=>$this->app->aid));
} catch(exception $e){
$this->app->logErroredQuery($e,$q);
return $e->getMessage();
}
I tried changing the datatype using php in an if statement then sticking into the query. Couldn't get that to work either. Here is what that looked like:
$htmlfield = $_POST["htmlinputfield"];
if(is_numeric($htmlfield)){
(int)$htmlfield;
}else{
(string)$htmlfield;
}
try{
$q = "UPDATE table1 SET text1 = :htmlfield
WHERE account_id = :aid AND fieldapplication_id = :fid";
$stmt = $this->app->db->prepare($q);
$stmt->execute(array(":fid"=>$this->app->fid,
":aid"=>$this->app->aid,
":htmlfield"=>$htmlfield));
} catch(exception $e){
$this->app->logErroredQuery($e,$q);
return $e->getMessage();
}
This resulted in changing all of the results to be the name of the column instead of a copy of the data. The original query works fir everything but numeric situations.
I imagine that this might be a CAST situation but my SQL abilities are not very good. The end result would be that whatever the database column that is being selected in the $_POST will be formatted to an int or a varchar. Any help would be much appreciated.