0
我有大約32個複選框,每個複選框都有自己的名稱值(它們的名稱是數字值:1,2,3等)。如果我選中了一個複選框,我們說複選框5,它將與複選框1一起插入到數據庫中,儘管我沒有選中複選框1.即使未檢查,插入數據庫值的複選框
我一直在努力弄清楚爲什麼會發生這種情況小時......這可能很簡單,我只是忽略它。
我使用Laravel 3.
我不包括值:因爲他們沒有在我的數據庫中3,4,14,15和25。
$exclude = array(3,4,14,15,25);
for ($r = 1; $r <= 37; $r++)
{
$exists = 0;
if (in_array($r, $exclude)) continue;
$hasRating = UserRating::where('uid', '=', $uid)->where('rid', '=', $r)->count();
if($hasRating)
$exists = 1;
// rating doesn't exist and we're adding it
if(Input::has($r) && !$exists)
{
$rating = new UserRating;
$rating->uid = $uid;
$rating->rid = $r;
$rating->save();
}
// remove rating if unchecked
if(!Input::has($r) && $exists)
UserRating::where('rid', '=', $r)->where('uid', '=', $uid)->delete();
}
下面是複選框代碼:
public static function showratings($uid, $rid, $rating)
{
$check = UserRating::where('rid', '=', $rid)->where('uid', '=', $uid)->count();
if($check)
{
$check = " checked";
$name = "<strong>".$rating."</strong>";
}
else
{
$check = "";
$name = $rating;
}
echo "<input type=\"checkbox\" name=\"$rid\" $check /> $name";
}