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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.