Jump to content

One result been inserted twice


Phpfr3ak

Recommended Posts

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";

Link to comment
https://forums.phpfreaks.com/topic/256574-one-result-been-inserted-twice/
Share on other sites

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";

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.

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

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

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 />";
   }
}

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.