【发布时间】:2015-10-01 05:19:30
【问题描述】:
您好,我有一个名为 tables 的表,其中包含 9 个字段(`id、第一、第二、第三、第四、第五、第六、第七、八),如果所有这些字段都不为空,我想要将其作为新记录插入到同一张表中。这是我现在的代码:
$db = new db();
$bind = array(
":tid" => "1"
);
$results = $db->select("tables", "tableID = :tid", $bind);
foreach ($results as $r) {
$tableID = $r['id'];
$l1_1 = $r['first'];
$l1_2 = $r['second'];
$l1_3 = $r['third'];
$l1_4 = $r['fourth'];
$l1_5 = $r['fifth'];
$l1_6 = $r['sixth'];
$l1_7 = $r['seventh'];
$l1_8 = $r['eight'];
}
if($l1_1 != "" AND $l1_2 != "" AND $l1_3 != "" AND $l1_4 != "" AND $l1_5 != "" AND $l1_6 != "" AND $l1_7 != "" AND $l1_8 != "")
{
$db->insert("tables", array(
"first" => $l1_1,
"second" => $l1_2,
"third" => $l1_3,
"fourth" => $l1_4,
));
//insert 2nd record
$db->insert("tables", array(
"first" => $l1_5,
"second" => $l1_6,
"third" => $l1_7,
"fourth" => $l1_8,
));
}
我还有一个很长的代码,它的作用是检查字段是否为空并将该数据插入那里,直到所有字段都被填满。输入最后一个数据后,将执行上面的代码。当输入最后一个数据时,它将执行上面的代码,检查所有字段是否为空并插入新数据。此代码有效,但最后插入的数据在插入的新记录中显示为空白。
我知道我的解释含糊不清,但请问我你想知道什么。
【问题讨论】:
-
不应该是 //insert 第二条记录 $db->insert("tables", array("fifth" => $l1_5, "sixth" => $l1_6, "seventh" => $l1_7, "八" => $l1_8, )); ?
-
为什么数据库中有一个空行?您可以检查查询中的字段是否为空。