dongyan8896 2013-03-25 09:07
浏览 28
已采纳

PHP和SQL CheckBox

I have written a form that allows for checkbox inputs in my SQL tables I have made the checkboxes as Variables.

What i want is when i display the forms that it should show the checkbox with the value as inputted by the initial input yes or no again.

<input type="checkbox" name="basic_inter" id="basic_inter" value="<? echo   
$rows['basic_inter']; ?>">

If I use it this way it doesn't show the value of the checkbox. What is best way to display the checkbox is it due to the tables being Var or is it the method I echo it back to the screen?

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// get value of id that sent from address bar
$id=$_GET['id'];

// Retrieve data from database 
$sql="SELECT * FROM $tbl_name WHERE company_name='$query'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>
  <form name="form" method="post" action="control_adminupdateresellered.php">
  <input type="hidden" name="company_name" value="<?=$query?>" />

Then this is some of the echo code I use others not fully included as they just are text

<input type="checkbox" name="basic_inter" id="basic_inter" value="<? echo 
$rows['basic_inter']; ?>">
          </strong></div>              </td>
        <td width="81"><div align="right"><strong>LBS Add-on:<br>
          R500-00 P/M
        </strong></div></td>
        <td width="66"><div align="left"><strong>
          <input type="checkbox" name="lbs_inter" id="lbs_inter" value="<? echo  
$rows['lbs_inter']; ?>">
  • 写回答

3条回答 默认 最新

  • duanjiuhong5843 2013-03-25 09:24
    关注

    Please try this

       <input type="checkbox" name="lbs_inter" id="lbs_inter" value="<? echo  
    $rows['lbs_inter']; ?>" <?php if($rows['lbs_inter']=='yes'){ echo "checked";}?>>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?