Phpfr3ak Posted February 6, 2012 Share Posted February 6, 2012 Hey, Basically the script runs fine except for the fact its just inserting one user instead of multiple users, anyone have a clue as to why? would be a huge hand, Cheers <?php if (isset($_POST["submit"]) && $_POST["submit"] == "Recall Selected Informants") { //Force POST values to be INTs $addIDs_ary = array_map('intval', $_POST['chkInv']); //Remove any 'false' value $addIDs_ary = array_filter($addIDs_ary); //Check that there was at least one valid value if(count($addIDs_ary)) { //Create comma separated string of the IDs $addIDs_str = implode(',', $addIDs_ary); //Create and run one query to perform all the adds (of the user) $query = "INSERT INTO hitlist SET hit_id = '($addIDs_str)' AND player_id = '$playerID'"; $sql = "UPDATE players SET Informants = Informants - 1 WHERE id = '$playerID'"; mysql_query($sql) or die(mysql_error()); if(mysql_query($query)) { $selectedCount = count($addIDs_ary); $adddCount = mysql_affected_rows(); echo "{$adddCount} of {$selectedCount} Investigated player(s) were successfully added."; }else{ echo "There was a problem running the query.<br>" . mysql_error(); } }else{ echo "Invalid ID data passed."; } } Quote Link to comment https://forums.phpfreaks.com/topic/256513-not-inserting-for-all/ Share on other sites More sharing options...
zeodragonzord Posted February 6, 2012 Share Posted February 6, 2012 When you say it's just inserting one user, do you mean it's only inserting one row? Also, $playerID is not set anywhere before it is used in your INSERT statement. Quote Link to comment https://forums.phpfreaks.com/topic/256513-not-inserting-for-all/#findComment-1315020 Share on other sites More sharing options...
Phpfr3ak Posted February 6, 2012 Author Share Posted February 6, 2012 Playerid is set in the header file, it works fine, and no, i mean its not inserting multiple people (its meant to its a checkbox multi adder) Quote Link to comment https://forums.phpfreaks.com/topic/256513-not-inserting-for-all/#findComment-1315091 Share on other sites More sharing options...
AyKay47 Posted February 6, 2012 Share Posted February 6, 2012 why would you expect multiple rows to be added? In your code, you are inserting an imploded string into the database once. Quote Link to comment https://forums.phpfreaks.com/topic/256513-not-inserting-for-all/#findComment-1315101 Share on other sites More sharing options...
Phpfr3ak Posted February 6, 2012 Author Share Posted February 6, 2012 Ahh sorry i guess i'm unsure as to how to go about an insert the code bwlow this deletes multiple results, id just like to add them instead: $query = "DELETE FROM hitlist WHERE hit_id IN ($deleteIDs_str) AND player_id = '$playerID'"; if(mysql_query($query)) Any clue how i could make that an insert? as when ive tried something along the lines of: $query = "INSERT INTO hitlist SET hit_id = IN ($deleteIDs_str) AND player_id = '$playerID'"; if(mysql_query($query)) I've error'd out any help would be great thanks Quote Link to comment https://forums.phpfreaks.com/topic/256513-not-inserting-for-all/#findComment-1315104 Share on other sites More sharing options...
AyKay47 Posted February 6, 2012 Share Posted February 6, 2012 for reference on the syntax for inserting multiple rows, refer here We are going to iterate through the array and piece a string together that we are going to use in the query. Since I have no idea where $playerID is coming from, i am going to assume that it is static. if(count($addIDs_ary) > 0) { $str = ""; foreach($addIDs_ary as $val) { $str .= "({$val},{$playerID}),"; if(end($arr) == $val) { $str .= "({$val},{$playerID})"; } } echo $str; // (val,val), (val,val), (val,val) etc.. $query = "INSERT INTO hitlist (hit_id,player_id) values $str"; } Quote Link to comment https://forums.phpfreaks.com/topic/256513-not-inserting-for-all/#findComment-1315120 Share on other sites More sharing options...
zeodragonzord Posted February 6, 2012 Share Posted February 6, 2012 Multi-row deletes work because you put a condition on rows that exist using the WHERE clause, "Delete from the table that matches these conditions." With basic inserts, you don't have a condition, you're just adding a row of data. You could however insert multiple rows at once using this format and run this statement once: INSERT INTO hitlist(hit_id, player_id) VALUES (field1data1, field2data1), (field1data2, field2data2); AyKay47's code accomplishes this. Quote Link to comment https://forums.phpfreaks.com/topic/256513-not-inserting-for-all/#findComment-1315157 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.