Can anybody help or point me in the right direction what is missing on my code.
The postcode is a mysql table and one field stores user's choice of postcodes in the form of array(a,b,c,d and etc....)
Now I want to load selectedPostcode in checkboxes for user_id = 2 to be able to edit and then save changes.
mysql table has 3 fields
id, user_id, selectedPostedcode
id 1, user_id 2, selectedPostedcode('NW9,TW1,W1,HA8,NW1,SW1')
My code does not show any checked or unchecked postcodes only shows blank checkboxes whenever I run it.
I can not figure out what is wrong.
Any help or advice will be appreciated.
<?PHP
include("connect.php");
error_reporting(-1);
?>
<html>
<HEAD>
</HEAD>
<BODY>
<h1>Post Code / zip code </h1>
<?php
/* this query will load user's selected postcodes for updating the user_id = 2 */
$query = "SELECT `id`, `user_id`, `selectedPostcode` FROM `postcode` WHERE postcode.user_id = 2 ;";
$result = mysql_query($query,$link);
if(! $result ){ die('Could not connect to Server: ' . mysql_error());}
?>
<!-- form will be used for updating and saving their changes-->
<form action="<?php echo $_SERVER['PHP_SELF'];?>" name="form" method="post" >
<?php
while ($fetch = mysql_fetch_assoc($result))
{
$selectedPostcode = $fetch['selectedPostcode'];
//$a = stripslashes("".$row['selectedPostcode']."");for debugging purpose
$selected = (explode(",",$selectedPostcode)); //array
//echo count($selected); for debugging purpose
$max = sizeof($selected);
//echo $max; for debugging purpose
for( $i=0; $i <$max; $i++ )
{
if (in_array($i, $selected))
{
echo '<input type="checkbox" name="postcode[]" value="'.$selected[$i].'" checked="checked"/>'.$selected;
}
else
{
echo '<input type="checkbox" name="postcode[]" value="'.$selected[$i].'"/>'.$selected;
}
}//close for loop
}//close while loop
?>
<input type="submit" value="Submit">
</form>