Suppose that I have the code php as below:
<?php
include("connectdb.php");
$check1 = "";
$check2 = "";
$check3 = "";
$check4 = "";
$check5 = "";
$query = "SELECT * FROM tblworkfaire";
$res = mysql_query($query) or die(mysql_error());
if($res){
while($data = mysql_fetch_array($res)){
if($data['1.DesTechnique'] == 1){
$check1 = "CHECKED";
echo $check1;
}
else if($data['1.DesTechnique'] == 2){
$check2 = " CHECKED";
}
else if($data['1.DesTechnique'] == 3){
$check3 = "CHECKED";
}
else if($data['1.DesTechnique'] == 4){
$check4 = "CHECKED";
}
else if($data['1.DesTechnique'] == 5){
$check5 = "CHECKED";
}
}
}
else{
echo "Fail";
}
?>
And Html code:
<form action="word2html.php" method="post">
<input type="radio" name="number1" value="1" checked="<? $check1; ?>">
</b><b>
<input name="number1" type="radio" value="2" checked="<? $check2; ?>">
</b><b>
<input name="number1" type="radio" value="3" checked="<? $check3; ?>">
</b><b>
<input name="number1" type="radio" value="4" checked="<? $check4; ?>">
</b><b>
<input name="number1" type="radio" value="5" checked="<? $check5; ?>">
</b>
What I need:
I have stored the radio box value in database,if I select value from database equal 1,it will check the radio box that have the value equal 1.
Problem:
I can only select value of radio box from database and it dose not work.How do I fix this? Anyone help me please, Thanks.
That’s some rather horrible code.
First problem: You’re doing a
while()loop to fetch results from your query. If you’re fetching multiple rows of data, the you’ll be setting all or some of those$checkXvariables, yet not actually doing anyhing with them during that particular loop iteration. Assuming you fetch enough rows, eventually ALL of those variables will becheckedand you end up selecting all of your radio buttons.If you’re expecting only a single row of data, then the while() is simply cargo cult programming.
Second problem. You’ve repeated a lot of code in there, and define a lot of variables, for something that could be done far easier/cleaner with a loop. e.g.
No $checkX variables, no repeated inputs. just a nice loop doing the output for you.