you should build a better database design first. Because these interest usually comes from an interests table, you have users - you need a users table, and need a connector table where you save the interest_id and user_id primary keys.
CREATE TABLE `users_interests_conn_table` (
`user_id` int(11) NOT NULL,
`interes_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Table structure for (second) table `interests`
--
CREATE TABLE `interests` (
`interes_id` int(11) NOT NULL auto_increment,
`interests` varchar(255) NOT NULL,
PRIMARY KEY (`interes_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
--
-- Table structure for table `users`
--
CREATE TABLE `users` (
`user_id` int(11) NOT NULL auto_increment,
`email` varchar(255) NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
Then you need to have a login page where you authenticate the user with the primary key number in SESSION. Then make a link, and list the interests in checkboxes, and build the query which deletes the old interests and insert the new selection into the connector table:
<?php
session_start();
if ( empty( $_SESSION["user_id"] ) ) {
header( "Location: login.php" );
exit();
}
include("connect.php"); // connect to the database
$filename = $_SERVER["PHP_SELF"];
/* many relations*/
$idfield2 = "interes_id";
$table2 = "interests";
$fieldname_2 = "interests";
/* connector table to handle many properties*/
$conntablename = "users_interests_conn_table";
$connector_id1 = "user_id";
$connector_id2 = "interes_id";
if ( !empty( $_POST["update_interest"] ) ) {
$sql = sprintf( "delete from $conntablename WHERE $connector_id1=%d" , $_SESSION["user_id"] );
$res = mysql_query( $sql );
if ( !empty( $_POST["ch"] ) ) {
/*
Insert the collected checkboxes into the connector table
*/
foreach( $_POST["ch"] AS $ids ) {
$sql = sprintf( "insert into $conntablename ($connector_id1,$connector_id2) VALUES('%d','%d')" , $_SESSION["user_id"] , $ids );
$res = mysql_query( $sql ) or die( mysql_error() . $sql . "<br />" );
echo $sql;
}
}
unset( $sql );
// header( sprintf( "Location: $filename?edit=%d&ok=1", $_POST["id"] ) );
}
if ( isset( $_GET["ok"] ) )
print "Updated succesfully<br />";
$sql2 = sprintf( "
SELECT `$idfield2`,`$fieldname_2`,if($table2.$idfield2 IN (select $connector_id2 from $conntablename where $conntablename.$connector_id1 =%d),1,0) AS is_in FROM `$table2`
;
" , $_SESSION["user_id"] );
// print $sql2."<br />";
print '<form method="post" action="' . $filename . '">';
print "<input type=\"hidden\" name=\"update_interest\" value=\"1\">";
$result = mysql_query( $sql2 );
while ( $rows = mysql_fetch_assoc( $result ) ) {
$checked = empty( $rows["is_in"] )?"":" checked=\"checked\"";
echo "<input type=\"checkbox\" name=\"ch[]\" value=\"{$rows["$idfield2"]}\"$checked>{$rows["$fieldname_2"]}<br />";
}
print '<input type="submit" name="submit" value="submit">';
print "</form>";
?>