Phpfr3ak Posted February 7, 2012 Share Posted February 7, 2012 Basically the following code works fine, exepct when it comes to the last result it inserts it twice, example: (3,17),(4,17),(5,17),(5,17) Any clues as to why? Thanks if(count($addIDs_ary) > 0) { $str = ""; foreach($addIDs_ary as $val) { $str .= "({$val},{$playerID}),"; if(end($addIDs_ary) == $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/256574-one-result-been-inserted-twice/ Share on other sites More sharing options...
Proletarian Posted February 7, 2012 Share Posted February 7, 2012 Just take your entire second IF clause out and it should work as intended. Quote Link to comment https://forums.phpfreaks.com/topic/256574-one-result-been-inserted-twice/#findComment-1315332 Share on other sites More sharing options...
Phpfr3ak Posted February 7, 2012 Author Share Posted February 7, 2012 Yea i moved it up one and it worked, maybe a silly question but do you have any clue how you can see if $addIDs_ary is already in the table hitlists under the column hit_id? Unsure as of how to check this in a loop, Thanks. if (isset($_POST["submit"]) && $_POST["submit"] == "Investigate Players") { //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 ($playerdata['Informants'] <= count($addIDs_ary)){ $attempt = count($addIDs_ary); echo "You are trying to investigate $attempt player(s) and only have $playerdata[informants] Informants available"; } else if(count($addIDs_ary) > 0) { $str = ""; foreach($addIDs_ary as $val) if(end($addIDs_ary) == $val) { $str .= "({$val},{$playerID})"; } else { $str .= "({$val},{$playerID}),"; if(end($addIDs_ary) == $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/256574-one-result-been-inserted-twice/#findComment-1315336 Share on other sites More sharing options...
Proletarian Posted February 7, 2012 Share Posted February 7, 2012 Instead of using a loop to make your string, just use array = implode(", ", array) and then append a "." to close the sentence. For that last question, you'd have to do a foreach loop. Foreach ID, select * from table where id = id, if query return true, then it's already in the list; if not, then you can add it to the list if that's what you intend. Quote Link to comment https://forums.phpfreaks.com/topic/256574-one-result-been-inserted-twice/#findComment-1315337 Share on other sites More sharing options...
Phpfr3ak Posted February 7, 2012 Author Share Posted February 7, 2012 Sorry really bad with this type of stuff, tried the following and got Parse error: syntax error, unexpected T_FOREACH in C:\Program Files\EasyPHP-5.3.3\www\public_html\PlayerRanks.php on line 20, exactly how am i going about that wrong, Thanks $sql = foreach"SELECT * FROM hitlist WHERE player_id = '$playerID' AND hit_id = '$addIDs_ary'"; $que = mysql_query($sql) or die(mysql_error()); if ($que['hit_id'] == '$addIDs_ary'){ $str = ""; } else Quote Link to comment https://forums.phpfreaks.com/topic/256574-one-result-been-inserted-twice/#findComment-1315344 Share on other sites More sharing options...
Proletarian Posted February 7, 2012 Share Posted February 7, 2012 Foreach is a PHP loop type, not a SQL method. Get the result first, then perform a foreach loop on the result. Quote Link to comment https://forums.phpfreaks.com/topic/256574-one-result-been-inserted-twice/#findComment-1315345 Share on other sites More sharing options...
Phpfr3ak Posted February 7, 2012 Author Share Posted February 7, 2012 Went with the following and still got an error, Warning: Invalid argument supplied for foreach() in C:\Program Files\EasyPHP-5.3.3\www\public_html\PlayerRanks.php on line 23, sorry not meaning to be a pain this is just blowing my mind. $sql = "SELECT * FROM hitlist WHERE player_id = '$playerID'"; $que = mysql_query($sql) or die(mysql_error()); $hitid = $que['hit_id']; foreach($hitid as $hits) if ($addIDs_ary == '$hits'){ } else Quote Link to comment https://forums.phpfreaks.com/topic/256574-one-result-been-inserted-twice/#findComment-1315347 Share on other sites More sharing options...
Proletarian Posted February 7, 2012 Share Posted February 7, 2012 You're getting that warning because $hitid is not an array. Use is_array() to verify this. To extract data from your mysql query, you need to use mysql_fetch_assoc() via a while loop. Inside the while loop is where you apply your foreach loop to extract individual records from each row. Example: $result = mysql_query($query); while ($row = mysql_fetch_assoc($result)) { foreach ($row as $column => $value) { echo $column . ": " . $value . "<br />"; } } Quote Link to comment https://forums.phpfreaks.com/topic/256574-one-result-been-inserted-twice/#findComment-1315365 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.