2014-03-05 44 views
0

更復選框值,這是我的HTML代碼檢索1或從數據庫

<input type='checkbox' name='cbox[]' value='Jaywalking' /> 
Jaywalking<br> 

         <input type='checkbox' name='cbox[]' value='Littering' /> 
Littering<br> 


         <input type='checkbox' name='cbox[]' value='Illegal Vendor' /> 
Illegal Vendor 

這是我的PHP代碼

if(isset($_POST['save'])) 
{ 
    $license_save=$_POST['license']; 
    $stickerno_save=$_POST['stickerno']; 
    $fname_save=$_POST['fname']; 
    $mname_save=$_POST['mname']; 
    $lname_save=$_POST['lname']; 
    $no_save=$_POST['no']; 
    $street_save=$_POST['street']; 
    $city_save=$_POST['city']; 
    $bdate_save=$_POST['bdate']; 


    $violationplace_save=$_POST['violationplace']; 
    $dd_save=$_POST['dd']; 
    $mm_save=$_POST['mm']; 
    $yy_save=$_POST['yy']; 
    $hh_save=$_POST['hh']; 
    $min_save=$_POST['min']; 
    $ampm_save=$_POST['ampm']; 

    if(is_array($_POST['cbox'])) $violation_save=implode(',',$_POST['cbox']); else $violation_save=$_POST['cbox']; 

mysql_query("UPDATE tblcitizen SET license ='$license_save', stickerno ='$stickerno_save', fname ='$fname_save', mname ='$mname_save', lname ='$lname_save', no ='$no_save', street ='$street_save', city ='$city_save', bdate ='$bdate_save', violationplace ='$violationplace_save', dd ='$dd_save', mm ='$mm_save', yy ='$yy_save', hh ='$hh_save', min ='$min_save', ampm ='$ampm_save', violation ='$violation_save', type ='$type_save', officer ='$officer_save', date ='$date_save', ttime ='$ttime_save' WHERE id = '$id'") 
       or die(mysql_error()); 
    echo "<script>alert('Successfully Edited')</script>"; 
     header("Location: citizen.php"); 

}

我想編輯一些帳戶註冊,怎麼能我從數據庫中檢索一個或多個複選框值。

回答

0

編輯以更好的答案

if(is_array($_POST['cbox'])) $violation_save=implode(',',$_POST['cbox']); else $violation_save=$_POST['cbox']; 

您的查詢採取CBOX陣列,將其變成用逗號一個字符串,如果它是一個數組(如果超過一個複選框被選中),否則將只是一個複選框沒有逗號的價值。

要得到的值了,只是讀取的字符串,然後將它與一個PHP strpos()

<? 
    // Get the checkboxes that were previously selected 
    $result = mysql_query("SELECT violation FROM tblcitizen WHERE id = '$id'") or die(mysql_error()); 
     while ($row = mysql_fetch_assoc($result)) { 
     // put the string into a variable 
     $mystring = $row['violation']; 
     } 

    // use a ternary to determine if this checkbox exists 
    // if so, make the variable $checked to check the checkbox, otherwise leave it blank 
    $checked = (strpos($mystring, 'Jaywalking')) ? 'checked' : ''; 
?> 
    // write the checkbox to the page. and echo the contents of $checked 
    // if it was found with strpos() then it will be checked, otherwise it will not be 
    // <?= ?> is a quick way to echo a variable without the echo command 
    <input type='checkbox' name='cbox[]' value='Jaywalking' <?=$checked;?> /> 
+0

我如何檢索我的複選框? – user3374936

+0

請參考我的代碼。我會在哪裏放置該代碼? – user3374936

+0

不確定你在問什麼。你想獲得選定的值嗎?你想在UPDATE之前檢查複選框嗎?注意:這不是整個代碼,我沒有添加數據拉(獲取數據並將其放入一個變量) – Tateyaku

0

我不太明白你的「我怎樣才能找回1個或多個複選框一句比較值來自數據庫「,但由於我能看到你的更新語句,我假設你想從複選框中取一個或多個值並更新數據庫中的表。

  1. 您將收到該複選框值數組的形式
  2. 您通過陣列需要循環,並在一個變量中獲得的價值
  3. 商店價值,你是好去保存它在數據庫

    foreach ($cbox as $val) { 
        $Violation .= $val.","; 
    } 
    

您可以通過使用方法PHP子總是刪除最後一個逗號。乾杯,希望這可以幫助你和任何人。謝謝。

+0

我在哪裏可以放那個代碼先生? – user3374936

+0

刪除'if(is_array($ _ POST ['cbox']))...'這一行,並將這一行 'foreach($ cbox as $ val){$ Violation。= $ val。「,」; }'並回顯$ Violation的值,以查看變量中的含義。 – UserProg