josephman1988 Posted August 27, 2008 Share Posted August 27, 2008 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 More sharing options...
BlueSkyIS Posted August 27, 2008 Share Posted August 27, 2008 need to see more code. Link to comment https://forums.phpfreaks.com/topic/121619-updating-via-a-form-checkboxes/#findComment-627353 Share on other sites More sharing options...
D3v 7oW Posted August 27, 2008 Share Posted August 27, 2008 By ticked, do you mean checked? Link to comment https://forums.phpfreaks.com/topic/121619-updating-via-a-form-checkboxes/#findComment-627357 Share on other sites More sharing options...
josephman1988 Posted August 27, 2008 Author Share Posted August 27, 2008 By ticked, do you mean checked? Yep. Blue, what more code do you want? Thats basically it, other then that there is the actual form itself .. I haven't added the INSERT querys for other fields as of yet but I don't think that's the problem. Link to comment https://forums.phpfreaks.com/topic/121619-updating-via-a-form-checkboxes/#findComment-627361 Share on other sites More sharing options...
D3v 7oW Posted August 27, 2008 Share Posted August 27, 2008 If those two codes are on the same page or in the same statement, that is also incorrect. Link to comment https://forums.phpfreaks.com/topic/121619-updating-via-a-form-checkboxes/#findComment-627363 Share on other sites More sharing options...
josephman1988 Posted August 27, 2008 Author Share Posted August 27, 2008 If those two codes are on the same page or in the same statement, that is also incorrect. It is on the same page ... I wanted, upon submission for it to update .. how would I do it otherwise then? Link to comment https://forums.phpfreaks.com/topic/121619-updating-via-a-form-checkboxes/#findComment-627365 Share on other sites More sharing options...
D3v 7oW Posted August 27, 2008 Share Posted August 27, 2008 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 Link to comment https://forums.phpfreaks.com/topic/121619-updating-via-a-form-checkboxes/#findComment-627373 Share on other sites More sharing options...
D3v 7oW Posted August 27, 2008 Share Posted August 27, 2008 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 Link to comment https://forums.phpfreaks.com/topic/121619-updating-via-a-form-checkboxes/#findComment-627379 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.