Note that there are some explanatory texts on larger screens.

plurals
  1. POMistake in my coding, but not sure how to correct it, wonder if anyone can give a hand?
    primarykey
    data
    text
    <p>Hi I encounter a problem with the foreach function in php... i used the function to input results of an array into the database. Forexample, If the Array (3,5,7), the system should input 3 different entries into the database. However, in my case, it only created 1 entry and totally ignored the other 2. May I know if anyone can spot my mistake?</p> <p>i have created the a form with checkboxes and trying retrieve the value and give each value a individual array for example:</p> <pre><code>&lt;form name="appoptions" id="applicationoptions" method="post" action="s_apply_now.php"&gt; &lt;table width="100%" class="apptable" border="0" cellspacing="0" cellpadding="0"&gt; &lt;tr&gt; &lt;td&gt;&lt;input type="checkbox" name="cat[]" value="1" /&gt; CAT 2PG&lt;/td&gt; &lt;td&gt;&lt;input type="checkbox" name="cat[]" value="2" /&gt; CAT 1OR&lt;/td&gt; &lt;td&gt;&lt;input type="checkbox" name="cat[]" value="3" /&gt; CAT 2TT&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;input type="checkbox" name="cat[]" value="4" /&gt; CAT 3PG &lt;/td&gt; &lt;td&gt;&lt;input type="checkbox" name="cat[]" value="5" /&gt; CAT 2OR&lt;/td&gt; &lt;td&gt;&lt;input type="checkbox" name="cat[]" value="6" /&gt; CAT 3TT&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;input type="checkbox" name="cat[]" value="7" /&gt; CAT 4PG&lt;/td&gt; &lt;td&gt;&lt;input type="checkbox" name="cat[]" value="8" /&gt; CAT 3OR&lt;/td&gt; &lt;td&gt;&amp;nbsp;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; </code></pre> <p>After clicking on confirm, it will then proceed to the processor page. In my processor page I tried to extract the value but i realise its in an array:</p> <pre><code>&lt;?php session_start(); include'Connections/database.php'; $conn = dbConnect (); if (! $conn) die("Couldn't connect to MySQL"); $user = $_SESSION['eid']; $query = "select MED from emp where EID = '$user'"; $result = mysql_query($query, $conn); $row = mysql_fetch_assoc($result); $med = $row['MED']; $user =$_SESSION['eid']; $class=$_POST['class']; $cat_arr=$_POST['cat']; $i = 0; /* for illustrative purposes only */ foreach ($cat_arr as $cat) { if ($med=='no') { if (! $conn) die("Couldn't connect to My SQL"); $query = "insert into permit (EID, PTYPE, STATUS) values ('$user,$cat, 'medical')"; $result = mysql_query($query,$conn); header ('Location:medical_question.php'); $i++; } else { if (! $conn) die("Couldn't connect to My SQL"); $query = "insert into permit (EID, PTYPE) values ('$user,$cat)"; $result = mysql_query($query,$conn); $i++; } } dbDisconnect($conn); ?&gt; </code></pre> <p>I am not everysure if for each is the mistake but I think its around there...</p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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