Jump to content

foreach implode problems


billgod

Recommended Posts

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

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. 

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

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.