Perfect, your solution has got me most of the way there. The issue I now have with the same code is on the update. Currently if there is no data in the table I run an insert. If the user has selections in the table I want to update this table. So currently I have an update query, the query runs fine, but the table is not getting updated with the new selection.
My code is below, but having spent some time reading over the last few days is whether I use an insert with a duplicate key update or to use REPLACE instead. Either way the code below is not updating the table which is what I need it to do so would be great to get the right solution - thanks!
$sql2="SELECT userid, category, faith, headlines, family, politics, film, music FROM content_selection WHERE '".$userid."'='".$_SESSION['userid']."'";
$result = mysql_query($sql2) or die('Request 2 failed: ' . mysql_error());
$row = mysql_fetch_assoc($result);
$userid2 = $row['userid'];
$media = $row['category'];
$faith = $row['faith'];
$headlines = $row['headlines'];
$family = $row['family'];
$politics = $row['politics'];
$film = $row['film'];
$music = $row['music'];
if(mysql_num_rows($result) == 1)
{
//$link next to affected rows and mysql query
$exists = 'true';
echo "<p>found stuff in db</p>";
echo "<p>headlines = $headlines</p>";
}
else
{
$exists = 'false';
echo "<p>oh dear nothing in db</p>";
}
//echo "<p>sql2 = $sql2</p>";
if (isset($_POST['faith'])){
$faith = "true";
}
else{
$faith = "";
}
////////HEADLINES/////////////////////////
if (isset($_POST['headlines'])){
$headlines = "true";
}
else{
$headlines = "";
}
//////////FAMILY/////////////////////////////
if (isset($_POST['family'])){
$family = "true";
}
else{
$family = "";
}
//////////POLITICS//////////////////////////
if (isset($_POST['politics'])){
$politics = "true";
}
else{
$politics = "";
}
///////////BUSINESS//////////////////////////
if (isset($_POST['business'])){
$business = "true";
}
else{
$business = "";
}
//////////FILM///////////////////////////////
if (isset($_POST['film'])){
$film = "true";
}
else{
$film = "";
}
//////////MUSIC//////////////////////////////
if (isset($_POST['music'])){
$music = "true";
}
else{
$music = "";
}
?>
<html>
<head>
<title></title>
</head>
<body>
<form method="post" action="#">
<input type="checkbox" name="faith" value="true" <?php if($_POST['faith']=="true") echo "checked=checked"; ?>/>Faith<br />
<input type="checkbox" name="headlines" value="true" <?php if($_POST['headlines']=="true") echo "checked=checked"; ?> /> Headlines<br />
<input type="checkbox" name="family" value="true" <?php if($_POST['family']=="true") echo "checked=checked"; ?> /> Family<br />
<input type="checkbox" name="politics" value="true" <?php if($_POST['politics']=="true") echo "checked=checked"; ?> /> Politics<br />
<input type="checkbox" name="film" value="true" <?php if($_POST['film']=="true") echo "checked=checked"; ?> /> Film<br />
<input type="checkbox" name="music" value="true" <?php if($_POST['music']=="true") echo "checked=checked"; ?> /> Music<br />
<input type="submit" value="submit" name="submit">
</form>
<?php
if (!isset($_POST['submit'])) { // if page is not submitted to itself echo the form
} else {
if ($exists === 'true'){
$query = "UPDATE content_selection SET faith='$faith', headlines='$headlines', family='$family', politics='$politics', film='$film', music='$music' where 'userid'='$userid'";
// $query = "INSERT INTO content_selection (faith, headlines, family, politics, film, music) VALUES('$faith', '$headlines', '$family', '$politics', '$film', '$music') ON DUPLICATE KEY UPDATE category = '$media'";
$result = mysql_query($query) or die('Update failed: ' . mysql_error());
}
else{
$query = "INSERT INTO content_selection (faith, headlines, family, politics, film, music, userid) VALUES( '$faith', '$headlines', '$family', '$politics', '$film', '$music', '$userid')";
$result = mysql_query($query) or die('Insert failed: ' . mysql_error());
}
echo "<p>$query</p>";
echo "<p>headlines=$headlines</p>";
}
?>
</body>
</html>