0

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>
2
Contributors
4
Replies
34
Views
3 Years
Discussion Span
Last Post by Ehsan_3
0

Your postcode datatype is string but you check integer $i in array of string in line 36.
You put array as label of checkbox in lines 38 and 42 label.
I recommend replace lines 34-44 with this:

    for( $i=0; $i <$max; $i++ )
    {
        $checked = ( ( /* put correct test here */ ) ? " checked=\"checked\"" : "" );  
            echo '<input type="checkbox" name="postcode[]" value="'.$selected[$i].'"'.$checked.' id="c_'.$i.'" /><label for="c_'.$i.'">'.$selected[$i].'</label>';
    }//close for loop

Edited by AndrisP

0

Thank you very much Andris for your time.
Your solution works perfectly after putting the correct test

0 as ==1 did work me.

Now trying to get the update done by uncheck 2 checkboxes for user_id=2
and submit the form it does not update the table!

debug info show $_POST works fine something must be wrong in my query!
Any idea on how to fix this?

echo '<pre>';print_r($_POST);echo '</pre>';

/*
Array
(
    [postcodes] => Array
        (
            [0] => TW1
            [1] => WD1
        )

    [Submit] => Save changes
)
*/

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

    if(count($_POST) > 0)
    {
        $selected = $_POST['postcodes'];
        $selected = mysql_real_escape_string(implode(',',  $_POST['postcodes']));

        $sql="UPDATE postcode SET selectedPostcode=`$postcodes` WHERE postcode.user_id =2" ;

        $result = mysql_query($sql,$link) or trigger_error(mysql_error()." in ".$sql);

    } //$link connects to db
}

Edited by Ehsan_3

0

In sql query ` (ASCII-96) and ' (ASCII-39) not the same sense!
Use (ASCII-96) for table names and field names but (ASCII-39) for values (line 24)

Edited by AndrisP

0

Thank you very much Andris.

Now it works, still I left with one more puzzle to solve. After I remove all checkboxes, only one checkbox remains checked with no name no value. I tried f statements still shows one checkbox checked
with no name and no value, despite db field is blank and empty.
`

$max = sizeof($selected);

if (($max < 0)||($max== "")) {echo "No postcode was found! "; break;}end;
if(!empty($row['user_id'])) {echo "No postcode was found! ";break;}end;
for( $i=0; $i <$max; $i++ )
{
      {if (!empty($selected))
     {
     //$checked = (( $selected==1) ? " checked=\"checked\"" : "" );
        $checked = (($selected>=0) ? "checked=\"checked\"" : "");
        echo '<input type="checkbox" name="postcodes2[]" value="'.$selected[$i].'"'.$checked.' id="c_'.$i.'" /><label for="c_'.$i.'">'.$selected[$i].'</label>';
     }
      else 
     {
         echo "No post code found"; 
         break;
     }
}//close for loop

`

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.