I am trying to insert into a table with multiple columns using phpgrid, but in php I only show a few of these columns..
However one of these columns that I am not showing in the grid cannot be null.
So I am using
$e[“on_insert”] = array(“add_field”, null, true);
$g->set_events($e);
and then
function add_field($data)
{
global $g;
$query = “INSERT INTO `field` (`FieldFullName`, `FieldShortName`, `FieldTypeID`,`FieldCategory`,`SYSTEMCODE`,`StringLength`,`IntegerPlaces`,`DecimalPlaces`,`Notes`, `Status`, `InsertDate`, `UpdateDate`, `InsertUserID`, `UpdateUserID`, `Deleted`) VALUES (“;
$query = $query.”‘{$data[“params”][“FieldFullName”]}’,”;
$query = $query.”‘{$data[“params”][“FieldShortName”]}’,”;
$query = $query.”‘{$data[“params”][“FieldTypeID”]}’,”;
$query = $query.”‘1’,”;
$query = $query.”NULL,”;
$query = $query.”‘{$data[“params”][“StringLength”]}’,”;
$query = $query.”‘{$data[“params”][“IntegerPlaces”]}’,”;
$query = $query.”‘{$data[“params”][“DecimalPlaces”]}’,”;
$query = $query.”‘{$data[“params”][“Notes”]}’,”;
$query = $query.”‘{$data[“params”][“Status”]}’,”;
$query = $query.”‘2012-01-01 01:01:01.000000’,”;
$query = $query.”‘2012-01-01 01:01:01.000000’,”;
$query = $query.”‘1’,”;
$query = $query.”‘1’,”;
$query = $query.”‘0’)”;
$g->execute_query($query);
When I try to insert using the + button above the grid, I enter fields and then press submit and I get this error:
Add Record
Couldn’t execute query. Field ‘FieldCategory’ doesn’t have a default value – INSERT INTO field (`FieldFullName`,`FieldShortName`,`FieldTypeID`,`StringLength`,`IntegerPlaces`,`DecimalPlaces`,`Notes`,`Status`) VALUES (‘textA’,’textB’,’1′,’1′,’2′,’3′,’notes’,’1′)
However the record is added correctly to the DB, but I can’t get rid of this error.
I also tried to set a default value for the column FieldCategory directly in the DB, for example say ‘3’, and if I do so I don’t get the error anymore but the columns is not set to ‘3’ instead of ‘0 that I need.
It looks like, the insert query is running twice. One by your function and one by grid itself … because you have set third param to true. Make it false and it will only run your function code and skip internal insert.
Pasting from docs:
[If you pass last argument as true, functions will act as a filter and insert/update in ->table
will be performed by grid after your function. If last argument is set to false, only your function handler will be executed.