Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, the following seems odd to me:</p> <pre><code>//sanitizes input and prevents sql injection attacks foreach($_POST as $key =&gt; $value) { $data[$key] = filter($value); $_SESSION[$key] = $value; } </code></pre> <p>Does this mean that you're saving everything in <code>$_POST</code> to your <code>$_SESSION</code>? That can't be what you want, since if you're using your session in another part of your site for login information, and for instance store the username in <code>$_SESSION['username']</code>, then someone can override this with submitting your form with a 'username' parameter. So you probably don't want to do that; I'd suggest just checking for the POST variables you actually want and sanitize them to where you want them.</p> <p>I'd suggest changing your code to something like:</p> <pre><code>$sports = array(); if (isset($_POST['sport']) &amp;&amp; is_array($_POST['sport'])) { foreach ($_POST['sport'] as $sport) { if (is_numeric($sport)) // example sanitation, since only numeric ids are allowed $sports[] = $sport; } $_SESSION['sports'] = $sports; } elseif (isset($_SESSION['sports'])) { $sports = $_SESSION['sports']; } </code></pre> <p>This will allow the user to override the sports in the session if they submit any sports. If you want to allow an empty sports array in the session, you'd want to change the check in the first if-statement to something like <code>if (isset($_POST['submit'])</code>, to check if the form was submitted (and your submit button is named 'submit').</p> <p>If the first condition fails (i.e., there's no new sports array passed), the sports array that was stored in the session will be used.</p> <p>Then, your second part of the code to check if a sport was checked, would be something like:</p> <pre><code>$checked = in_array($id, $sports); $content .= '&lt;td&gt;&lt;input type="checkbox" name="sport[]" value="'.$id.'" id="sport'.$id.'" '.($checked?'checked="checked':'').'&gt;'.$name.'&lt;/td&gt;'; </code></pre> <p>(Assuming that <code>$id</code> and <code>$name</code> are set properly.)</p> <p><strong>Update:</strong> Also check whether the sport array from <code>POST</code> is actually an array.</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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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