Jump to content

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.

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.