Forum Moderators: coopster

Message Too Old, No Replies

updating a form with checkbox query

checkbox value

         

giiik

2:00 pm on Feb 11, 2010 (gmt 0)

10+ Year Member



i want to update a form and i hav used 1 checkbox
when i check da checkbox the value in table shd be 1 and if unchecked then the value should be zero

now when i update the form then the checkbox has to be checked if previously it was checked and vice versa..
but its not...


here's the code:


for inserting values:


<?php
$con=mysql_connect("localhost","root","");
if(!$con)
{
die("could not connect:".mysql_error());
}
mysql_select_db("db1",$con);

if(isset($_POST['submit']))
{
$x=$_POST['a'];
$y=$_POST['b'];
$z=$_POST['c'];
$u=$_POST['d'];
mysql_query("INSERT INTO data(name,class,rollno,value) VALUES('$x','$y','$z','$u')");
}

?>
<html>
<body>
<form method="post">
name:<input type="text" name="a"><br />
class :<input type="text" name="b"><br />
rollno:<input type="text" name="c"><br />
value:<input type="checkbox" name="d" value="1"><br />
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>

<?php
$sql="SELECT * from data";
$result=mysql_query($sql);

echo "<table border='1'>
<tr>
<th>id</th>
<th>name</th>
<th>class</th>
<th>rollno</th>
<th>value</th>
<th>edit</th>
<th>delete</th>
</tr>";

while($row=mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>".$row['id'] ."</td>";
echo "<td>".$row['name'] ."</td>";
echo "<td>".$row['class']."</td>";
echo "<td>".$row['rollno']."</td>";
echo "<td>".$row['value']."</td>";
?>
<td><a href="updatee.php?id=<?php echo $row['id'];?>">edit</a></td>

<?php
echo "</tr>";
}

echo "</table>";
mysql_query($sql,$con);
mysql_close($con);
?>



for updating form:

<?php
$id=$_GET['id'];

$con=mysql_connect("localhost","root","");
if(!$con)
{
die("could not connect".mysql_error());
}
mysql_select_db("db1",$con);
$result=mysql_query("SELECT * from data WHERE id='$id'");
while($row=mysql_fetch_array($result))
{
$x=$row['name'];
$y=$row['class'];
$z=$row['rollno'];
$u=$row['value'];


}

if(isset($_POST['submit']))

{
$x=$_POST['a'];
$y=$_POST['b'];
$z=$_POST['c'];
$u=$_POST['d'];

mysql_query("UPDATE data SET name='$x',class='$y',rollno='$z',value='$u' WHERE id='$id'");
header("location:insert.php");
}


?>
<html>
<body>
<form method="post">
name:<input type="text" name="a" value="<?php echo $x;?>"><br />
class :<input type="text" name="b" value="<?php echo $y;?>"><br />
rollno:<input type="text" name="c" value="<?php echo $z;?>"><br />
value:<input type="checkbox" name="d" value="<?
if ($u==1)
{
echo '$d="checked"';

}
else
{
echo '$d="uncheck"';
}

?>"><br />
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>

Matthew1980

3:20 pm on Feb 11, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there giiik,

Welcome to the forum,

Just having a quite glance at the code (i'm on a break!) i notice that:-


value:<input type="checkbox" name="d" value="<?


is missing the php tag:-


value:<input type="checkbox" name="d" value="<?php


Whether that will cure the problem or not, I'm not sure, but its good coding practise to always use the full tags, just thought I would point it out, especially if short tags are not enabled in your ini file.

The only other thing I can advise is to sanitise the $_POST globals, especially if you are using them in your sql query, strip_tags() & mysql_real_escape_string() are about the best you can use, not having them on means as the database/code your writing is wide open to malicous attacks from the hackers of the internet.


mysql_real_escape_string(strip_tags($_POST['your_value']));

Cheers,

MRb

dreamcatcher

8:46 am on Feb 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is invalid syntax:

echo '$d="checked"';

should be:

echo 'checked="checked"';

dc