Jump to content

Not inserting for all


Phpfr3ak

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/256513-not-inserting-for-all/
Share on other sites

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

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

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.

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.