2013-08-27 48 views
1

我將值從複選框發佈到用戶配置文件的數據庫中。當用戶去編輯他/她的個人資料時,我希望他們之前選擇的複選框被檢查,以便他們在更新他們的個人資料後不會丟失該信息。我嘗試了許多不同的解決方案,但沒有運氣。檢查數據庫值中的複選框

複選框的值被輸入到名爲members_teachers的名爲焦點的列中,並通過逗號分隔,例如藝術,數學,舞蹈等。我不確定我在完成我的目標時距離多遠或多遠,但我極大地滿足您可以提供的任何幫助或建議。非常感謝你提前

我的代碼,試圖檢查值是

<?php 
$focusQuery = mysql_query("SELECT focus FROM members_teachers WHERE id = $member_id") or die; 

while ($new_row = mysql_fetch_assoc($focusQuery)){ 

$focusRow = $row['focus']; 

$focusValue = explode(',', $focusRow); 

foreach($focusValue as $newFocus){ 

//echo $newFocus; 

//echo "<br/>"; 

$result = mysql_query("SELECT focus FROM members_teachers WHERE focus LIKE '%$focusRow%'") or die; 

if(mysql_num_rows($result) > $newFocus){ 

$checked = 'checked="checked"'; 

} 

else{ 

$checked = ''; 

} 

} 

} 
?> 

這是我的HTML

<label for="art-focus">Art</label> 
         <input name="focus[]" type="checkbox" value="Art" <?php echo $checked ?>> 

<label for="math-focus">Mathematics</label> 
          <input name="focus[]" type="checkbox" value="Mathematics" <?php echo $checked ?>> 

<label for="dance-focus">Dance</label> 
          <input name="focus[]" type="checkbox" value="Dance" <?php echo $checked ?>> 

回答

3
<?php 
// Create connection 
$con=mysqli_connect("hostname","username","pass","dbname"); 

// Check connection 
if (mysqli_connect_errno($con)) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 
$result = mysqli_query($con,"SELECT focus FROM members_teachers WHERE id = $member_id"); 
while($row = mysqli_fetch_array($result)) 
    { 
    $focus=explode(",",$row['focus']); 

?> 
<input type="checkbox" name="focus[]" value="Art" <?php if(in_array("Art",$focus)) { ?> checked="checked" <?php } ?> > 
<input type="checkbox" name="focus[]" value="Mathematics" <?php if(in_array("Mathematics",$focus)) { ?> checked="checked" <?php } ?> > 
<input type="checkbox" name="focus[]" value="Dance" <?php if(in_array("Dance",$focus)) { ?> checked="checked" <?php } ?> > 
<?php 
} 
?> 
+0

非常感謝你這是完全的工作!我過於複雜了。這更有意義。再次感謝您的幫助 – bilcker

+0

您的答案真的有用謝謝您先生... – Hirdesh

+0

謝謝您的代碼正常工作 –

0
<?php 

$focusedValues = array(); 

$focusQuery = mysql_query("SELECT focus FROM members_teachers WHERE id = $member_id") or die; 

while ($row = mysql_fetch_assoc($focusQuery)){ 

    $focusedValues = explode(',', $row['focus']); 
} 
?> 

<label for="art-focus">Art</label> 
         <input name="focus[]" type="checkbox" value="Art" <?php echo in_array('Art', $checked) ?>> 

<label for="math-focus">Mathematics</label> 
          <input name="focus[]" type="checkbox" value="Mathematics" <?php echo in_array('Mathematics', $checked) ?> 

<label for="dance-focus">Dance</label> 
          <input name="focus[]" type="checkbox" value="Dance" <?php echo in_array('Dance', $checked) ?> 

我不知道爲什麼你SELECT ING這是第二次,這是毫無意義的,你已經知道檢查了什麼,因爲它在$focusedValues。此外,在您的代碼中,如果沒有檢查到任何內容,$checked將爲空,否則爲checked="checked"。你顯然需要每個輸入的變量,不是?

0
<?php $hobby = $row['hobbies']; 
    $hobbies = explode (' ', $hobby); 
?> 
<input type="checkbox" name="hobbies[]" value="cricket" <?php echo in_array('cricket', $hobbies?'checked':'') ?> >cricket 
<input type="checkbox" name="hobbies[]" value="singing" <?php echo in_array('singing' , $hobbies ?'checked': '') ; ?> >singing 
<input type="checkbox" name="hobbies[]" value="football" <?php echo in_array('football', $hobbies ?'checked': '') ; ?> >footballl 
相關問題