billgod Posted January 2, 2009 Share Posted January 2, 2009 I have this code that works. foreach ($_POST as $key => $val) { if (stristr($key, "charid")) { $ids[] = $val; } } $ids = implode(", ", $ids); mysql_query("update $tbl_name set selectedby = '$_SESSION[myusername]', available = 'no' WHERE id IN(" . $ids . ")");; I am trying the same thing instead of an update do an insert and its failing. can anyone find the typo or what I am doing wrong? foreach ($_POST as $key => $val) { if (stristr($key, "charid")) { $ids[] = $val; } } $ids = implode(", ", $ids); mysql_query("insert into teamlist (userid, gameid, characterid) values ('$_SESSION[userid]', '$_POST[gameid]', IN(" . $ids . ")");; I tried this just to output the sql.. foreach ($_POST as $key => $val) { if (stristr($key, "charid")) { $ids[] = $val; } } $ids = implode(", ", $ids); $sql="insert into teamlist (userid, gameid, characterid) values ('$_SESSION[userid]', '$_POST[gameid]', IN(' . $ids . ')"; $result=mysql_query($sql); echo $sql; The output looked like this insert into teamlist (userid, gameid, characterid) values ('1', '11', IN(' . 137, 4, 11 . ') Link to comment https://forums.phpfreaks.com/topic/139143-foreach-implode-problems/ Share on other sites More sharing options...
trq Posted January 2, 2009 Share Posted January 2, 2009 $sql="insert into teamlist (userid, gameid, characterid) values ('$_SESSION[userid]', '$_POST[gameid]', IN(" . $ids . ")"; Link to comment https://forums.phpfreaks.com/topic/139143-foreach-implode-problems/#findComment-727725 Share on other sites More sharing options...
DarkWater Posted January 2, 2009 Share Posted January 2, 2009 IN() makes no sense randomly appearing in the VALUES() part of an INSERT query. In fact, it makes no sense at all in an INSERT query. >_> Link to comment https://forums.phpfreaks.com/topic/139143-foreach-implode-problems/#findComment-727731 Share on other sites More sharing options...
billgod Posted January 2, 2009 Author Share Posted January 2, 2009 how would I do the same thing for an insert as I did for the update? I will be having a random number of variables coming in from the POST. Link to comment https://forums.phpfreaks.com/topic/139143-foreach-implode-problems/#findComment-727735 Share on other sites More sharing options...
billgod Posted January 2, 2009 Author Share Posted January 2, 2009 I have a form that is passing a variable. there is a random number of variables. I have no idea how to get the variable into the database. here is a snippet from my form <td width="25"><? $myid=$rows['charid']; $number++; echo "<input name='charid$number' value='$myid' type='checkbox' id='$myid'>"; ?></td> so when you check the check box it passes charid1, charid2 charid3 and so on. I need to insert into table (userid, gameid, charid) values ($_SESSION[variable], $_SESSION[variable], myvalue passed from form; can anyone tell me how to do this. As I said before I was doing an update with a foreach loop with implode but it does not appear I can do that with an insert statement. Link to comment https://forums.phpfreaks.com/topic/139143-foreach-implode-problems/#findComment-727774 Share on other sites More sharing options...
trq Posted January 2, 2009 Share Posted January 2, 2009 Sorry, I didn't even really look at your query. You will actually need to run multiple queries, eg: foreach ($_POST as $key => $val) { if (stristr($key, "charid")) { $val = mysql_real_escape_string($val); $sql = "INSERT INTO teamlist (userid, gameid, characterid) VALUES ('{$_SESSION['userid']}', '{$_POST['gameid']}', '$val'"; mysql_query($sql); } } Link to comment https://forums.phpfreaks.com/topic/139143-foreach-implode-problems/#findComment-728451 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.