Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Ok, lets start by clearing up your code. Instead of having to create each independent variable in your <code>if ($_SESSION['admin'] == 'loged')</code> method, you can use the function <code>extract();</code>. The <code>extract()</code> method creates a variable for each key in the provided array. Say you have the key <code>name</code> in the array <code>$_POST</code>, the extract method will create a variable named <code>name</code> for you. To retrieve the value, all you need to do is access the variable <code>$name</code>.</p> <pre><code>if ($_SESSION['admin'] == 'loged'){ extract($_POST); } </code></pre> <p>Secondly, you don't use the word <code>and</code> if you want to check more than one thing in an if statement, you use the following operand '&amp;&amp;'. </p> <pre><code>if (isset($_POST['title']) &amp;&amp; isset($_POST['main-poster']) &amp;&amp; isset($_POST['type']) &amp;&amp; isset($_POST['year']) &amp;&amp; isset($_POST['language']) &amp;&amp; isset($_POST['platform']) &amp;&amp; isset($_POST['publisher']) &amp;&amp; isset($_POST['size']) &amp;&amp; isset($_POST['graphics']) &amp;&amp; isset($_POST['little-info']) &amp;&amp; isset($_POST['full-info']) &amp;&amp; isset($_POST['posters']) &amp;&amp; isset($_POST['screenshots']) &amp;&amp; isset($_POST['trailers']) &amp;&amp; isset($_POST['gameplays']) &amp;&amp; isset($_POST['author'])) </code></pre> <p>Instead of manually checking to see if each object has been set in the <code>$_POST</code> array, you can just iterate through <code>$_POST</code>.</p> <p>Create an array of the variables that you need to be set:</p> <pre><code>$req_fields = array( 'title', 'main-poster', 'type', 'year', 'language', 'platform', 'publisher', 'size', 'graphics', 'little-info', 'full-info', 'posters', 'screenshots', 'trailers', 'gameplays', 'author' ); </code></pre> <p>Then create an array for the elements that haven't been set:</p> <pre><code>$notset = array(); </code></pre> <p>Finally, iterate through <code>$_POST</code> checking to see if each value is set. If not, add it to the array.</p> <pre><code>foreach ($req_fields as $key) { if (!isset($_POST[$key]) { $notset[] = $key; } } </code></pre> <p>Then check to see if any values have not been set and redirect the user, else, load the model and echo the post:</p> <pre><code>if (count($notset) &gt; 0) { $this-&gt;load-&gt;view('postnews'); } else { $this-&gt;load-&gt;model('Gamesmodel'); echo $this-&gt;Gamesmodel-&gt;PostArticle($title, $main_poster, $type, $year, $language, $platform, $publisher, $size, $graphics, $little_info, $full_info, $posters, $screenshots, $trailers, $gameplays, $autor, $date); } </code></pre> <p>Presumably the actual reason behind the insert not working is because it isn't actually called. The reason behind this would be that some of the keys were not actually set. </p> <p>Iterate through the <code>$notset</code> array to see if this is the case:</p> <pre><code>foreach ($notset as $unsetField) { echo "Field {$unsetField} is not set. &lt;br /&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