Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP Foreach loop problem with mySQL INSERT INTO
    text
    copied!<p>I am having a big issue. This is the first time I sue a foreach and I do not even know if it's the right thing to use. I have a textarea where my members can add some text. They also have all the accounts where to send the posted text. Accounts are of two types F and T. They are shown as checkboxes. So when a member types "submit" the text should be INSERTED in a specific table for EACH of the selected accounts. I thought php foreach was the right thing. But I am not sure anymore. Please take in mind I do not know anything about foreach and arrays. So please when helping me, consider to provide the modded code =D . Thank you so much!</p> <pre><code>&lt;?php require_once('dbconnection.php'); $MembID = (int)$_COOKIE['Loggedin']; ?&gt; &lt;form action="" method="post"&gt; &lt;p align="center"&gt;&lt;textarea id="countable1" name="addit" cols="48" rows="10" style="border-color: #ccc; border-style: solid;"&gt;&lt;/textarea&gt; &lt;br&gt; &lt;? $DB = new DBConfig(); $DB -&gt; config(); $DB -&gt; conn(); $on="on"; $queryF ="SELECT * FROM `TableF` WHERE `Active`='$on' AND `memberID`='$MembID' ORDER BY ID ASC"; $result=mysql_query($queryF) or die("Errore select from TableF: ".mysql_error()); $count = mysql_num_rows($result); if ($count &gt; 0) { while($row = mysql_fetch_array($result)) { ?&gt;&lt;div style="width:400px; height:100px;margin-bottom:50px;"&gt;&lt;? $rowid = $row['ID']; echo $row['Name'] . '&lt;/br&gt;'; $checkit = "checked"; if ($row['Main'] == "") $checkit = ""; if ($row['Locale'] =="") $row['Locale'] ="None" . '&lt;/br&gt;'; echo $row['Locale'] . '&lt;/br&gt;'; if ($row['Link'] =="") $row['Link'] ="javaScript:void(0);"; ?&gt; &lt;!-- HERE WE HAVE THE "F" CHECKBOXES. $rowid SHOULD BE TAKEN AND INSERTED IN THE FOREACH BELOW FOR ANY SELECTED CHECKBOX IN THE FIELD "Type".SEE BELOW --&gt; &lt;input type="checkbox" name="f[&lt;?php echo $rowid?&gt;]" &lt;?php echo $checkit;?&gt;&gt; &lt;/div&gt; &lt;? }//END WHILE MYSQL }else{ echo "you do not have any Active account"; } $DB = new DBConfig(); $DB -&gt; config(); $DB -&gt; conn(); $queryTW ="SELECT * FROM `TableT` WHERE `Active`='$on' AND `memberID`='$MembID' ORDER BY ID ASC"; $result=mysql_query($queryTW) or die("Errore select TableT: ".mysql_error()); $count = mysql_num_rows($result); if ($count &gt; 0) { while($row = mysql_fetch_array($result)) { ?&gt;&lt;div style="width:400px; height:100px;margin-bottom:50px;"&gt;&lt;? $rowid = $row['ID']; echo $row['Name'] . '&lt;/br&gt;'; $checkit = "checked"; if ($row['Main'] == "") $checkit = ""; if ($row['Locale'] =="") $row['Locale'] ="None" . '&lt;/br&gt;'; echo $row['Locale'] . '&lt;/br&gt;'; if ($row['Link'] =="") $row['Link'] ="javaScript:void(0);"; ?&gt; &lt;!-- HERE WE HAVE THE "T" CHECKBOXES. $rowid SHOULD BE TAKEN AND INSERTED IN THE FOREACH BELOW FOR ANY SELECTED CHECKBOX IN THE FIELD "Type".SEE BELOW --&gt; &lt;input type="checkbox" name="t[&lt;?php echo $rowid?&gt;]" &lt;?php echo $checkit;?&gt; &gt; &lt;/div&gt; &lt;? }//END 2° WHILE MYSQL }else{ echo "you do not have any Active account"; } ?&gt; &lt;input type="submit" value="submit"&gt; &lt;/form&gt; &lt;? //WHEN CHECKBOXES "F" ARE FOUND, FOR EACH CHECKBOX IT SHOULD INSERT INTO TableG THE VALUES BELOW, FOR EACH SELECTED "F" CHECKBOX if(!empty($_POST['addit']) &amp;&amp; !empty($_POST['f'])){ $thispostF = $_POST['f']; $f="F"; foreach ($thispostF as $valF) //THE MOST IMPORTANT FIELD HERE IS "Type". THE ARRAY INSERT "on", I NEED INSTEAD THE VALUE $rowid AS ABOVE $queryaddF="INSERT INTO TableG (ID,memberID,Type,IDAccount,Tuitting) VALUES (NULL,".$MembID.",'".$f."','".$valF."', '".$_POST['addit']."')"; $resultaddF=mysql_query($queryaddF) or die("Errore insert G: ".mysql_error()); } //WHEN CHECKBOXES "T" ARE FOUND, FOR EACH CHECKBOX IT SHOULD INSERT INTO TableG THE VALUES BELOW, FOR EACH SELECTED "T" CHECKBOX if(!empty($_POST['addit']) &amp;&amp; !empty($_POST['t'])){ $thispostT = $_POST['t']; $t="T"; foreach ($thispostT as $valF) //THE MOST IMPORTANT VALUE HERE IS "Type". THE ARRAY GIVES "on", I NEED INSTEAD THE VALUE $rowid AS ABOVE $queryaddT="INSERT INTO TableG (ID,memberID,Type,IDAccount,Tuitting) VALUES (NULL,".$MembID.",'".$t."','".$valF."', '".$_POST['addit']."')"; $resultaddF=mysql_query($queryaddF) or die("Errore insert G: ".mysql_error()); } ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload