我环顾四周,注意到一些人遇到了同样的问题,但他们的疏忽似乎不适用于这里。
我有一个 PHP 函数可以将一组值添加到表中。它首先检查值是否为空,如果是,则将它们替换为NULL
, 以便将 anull
放在表字段中。我放入的每个字段都null
允许在其中放置null
a 0
。
这是一些代码:
public static function AddGame($array, $tId)
{
global $db; // Bring DB into scope
// Get IDs from particular names
$locId = $array['fLocation'];
// Ensure empty values are SQL null
$hTeamId = "'{$array['fHomeTeam']}'";
$vTeamId = "'{$array['fVisitTeam']}'";
$hScore = "'{$array['fHomeScore']}'";
$vScore = "'{$array['fVisitScore']}'";
$hHoldFor = "'{$array['fHomeHoldFor']}'";
$vHoldFor = "'{$array['fVisitHoldFor']}'";
// Prepare row for insertion
$row = "'','$tId','$locId',$hTeamId,$vTeamId,'{$array['fDate']}','{$array['fTime']}',$hScore,$vScore,'{$array['fGameType']}',$hHoldFor,$vHoldFor";
$stmt = $db->prepare("INSERT INTO `game` VALUES($row)");
if($stmt->execute()) return true;
else return false;
}
我已经在多行调试了这个函数并转储了 $row 字符串,它显示了这个,这是预期的:
'','1','1','21','21','10/10/12','10:30AM','NULL','NULL','pool','NULL','无效的'
然而,当我检查表格文本类型字段时,字面上的值NULL
不是我想要的,而且 int 字段也显示为0
. 如果我将值留空或作为 PHP 的 null ,则文本字段显示为空(或null
我想要的正确),但整数仍显示为0
.
我希望这只是由于我间接插入值的方式造成的。
这是显示创建表game
创建表`游戏`( `id` int(11) NOT NULL AUTO_INCREMENT, `tId` int(11) 非空, `盖子` int(11) 非空, `hTeamId` int(11) 默认为空, `vTeamId` int(11) 默认为空, `date` 文本不为空, `time` 文本不为空, `hScore` int(11) 默认为空, `vScore` int(11) 默认为空, `type` 文本不为空, `hHoldFor` 文本, `vHoldFor` 文本, 主键(`id`) ) 引擎=InnoDB AUTO_INCREMENT=17 默认字符集=latin1
更新:
插入“游戏”值('','1','1','','','10/09/12','9:30AM','','','pool','winner池 A','池 B 的获胜者')