Jump to content

Updating via a form [checkboxes]


josephman1988

Recommended Posts

So I have various checkbox fields in my form that inputs data into my db.

These boxes are 'subseries' to the 'game' being edited, so say if i tick 5 boxes, them 5 box values are associated to this game via a 3rd table.

 

I have done this so when updating, the already ticked boxes before the update are ticked.:

$result = mysql_query( "SELECT * FROM subseries LEFT JOIN subseriesgamesconnect ON (sId=subseriessId AND gamesgId=$gId)" );
while ( $row = mysql_fetch_assoc( $result ) ) {
     if ( !empty( $row["gamesgId"] ) ) { $checked = " checked=\"checked\""; } else { $checked = ""; }
     print "<input type=\"checkbox\" name=\"subseries[".$row["sId"]."]\" ".$checked." /> ".$row["sName"]."<br />";
}

 

The way I want to do it, is instead of using UPDATE, im wanting to DELETE the data that is null, or 'not ticked' and INSERT if the box is ticked, however, the INSERT part doesnt seem to be working .. the DELETE part is working fine though.:

mysql_query( "DELETE FROM subseriesgamesconnect WHERE gamesgId=$gId" );
foreach( $_POST['subseries'] as $subseries => $checked ) {
    if ( $checked == 1 )
        mysql_query("INSERT INTO subseriesgamesconnect VALUES ( $subseries, $gId )");
	}

What am I overlooking?

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/121619-updating-via-a-form-checkboxes/
Share on other sites

if(empty($_POST['subseries']))
{
   $result = mysql_query( "SELECT * FROM subseries LEFT JOIN subseriesgamesconnect ON (sId=subseriessId AND gamesgId=$gId)" );
   while ( $row = mysql_fetch_assoc( $result ) ) {
        if ( !empty( $row["gamesgId"] ) ) { $checked = " checked=\"checked\""; } else { $checked = ""; }
        print "<input type=\"checkbox\" name=\"subseries[".$row["sId"]."]\" ".$checked." /> ".$row["sName"]."<br />";
   }
}
else
{
   foreach( $_POST['subseries'] as $subseries => $checked ) {
       if ( $checked == 1 )
           mysql_query("DELETE FROM subseriesgamesconnect WHERE gamesgId=$gId" );
           mysql_query("INSERT INTO subseriesgamesconnect VALUES ( $subseries, $gId )");
   }
}

 

inside of the statement for if the $_POST['subseries'] is empty you would also need to be the beginning to the form and the end of it

if(empty($_POST['subseries']))
{
   $result = mysql_query( "SELECT * FROM subseries LEFT JOIN subseriesgamesconnect ON (sId=subseriessId AND gamesgId=$gId)" );
   while ( $row = mysql_fetch_assoc( $result ) ) {
        if ( !empty( $row["gamesgId"] ) ) { $checked = " checked=\"checked\""; } else { $checked = ""; }
        print "<input type=\"checkbox\" name=\"subseries[".$row["sId"]."]\" ".$checked." /> ".$row["sName"]."<br />";
   }
}
else
{
   foreach( $_POST['subseries'] as $subseries => $checked ) {
       if ( $checked == 1 )
           mysql_query("DELETE FROM subseriesgamesconnect WHERE gamesgId=$gId" );
           mysql_query("INSERT INTO subseriesgamesconnect VALUES ( $subseries, $gId )");
   }
}

 

inside of the statement for if the $_POST['subseries'] is empty you would also need to put the beginning to the form and the end of it

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.