Note that there are some explanatory texts on larger screens.

plurals
  1. POBasic prepared statement not INSERTING
    primarykey
    data
    text
    <p>I am for the first time trying to build a simple form to insert data to a MySQL table. It is not working however.</p> <p>This is the form:</p> <pre><code>&lt;form action="" method="post"&gt; Select Park : &lt;select name="park_id"&gt; &lt;option value=""&gt;Select Park&lt;/option&gt; &lt;?php foreach ($res as $row) { printf('&lt;option value="%s"&gt;%s&lt;/option&gt;' . PHP_EOL, $row['park_id'], $row['name']); } ?&gt; &lt;/select&gt; Headline: &lt;input type="text" name="headline"&gt; &lt;br&gt; Story: &lt;input type="textbox" name="story"&gt; &lt;br&gt; Date: &lt;input type="date" name="date"&gt; Password :&lt;input type="password" value="" name="password" id="password"&gt; &lt;input type="submit"&gt; &lt;/form&gt; </code></pre> <p>and this is the php:</p> <pre><code>if(isset($_POST['submit'])){ $stmt = $db-&gt;prepare("INSERT INTO tpf_newstest(headline,story,date,park_id) VALUES(:headline,:story,:date,:park_id)"); $stmt-&gt;execute(array(':headline' =&gt; $headline, ':story' =&gt; $story, ':date' =&gt; $date, ':park_id' =&gt; $park_id)); $affected_rows = $stmt-&gt;rowCount(); } </code></pre> <p>It it all within the same index.php file. Any ideas as to what I've done wrong? Thanks</p> <p>EDIT: if it helps this is the full page</p> <pre><code>try { $pdo = new PDO('mysql:host=localhost;dbname=*', '*','******'); $pdo-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $pdo-&gt;exec('SET NAMES "utf8"'); } catch (PDOException $e) { $output = 'Unable to connect to the database server.'; include 'output.html.php'; exit(); } $output = 'Theme Park Database initialized'; $qry = "select park_id, name from tpf_parks order by name"; $res = $pdo-&gt;query($qry); if(isset($_POST['submit'])){ $stmt = $db-&gt;prepare("INSERT INTO tpf_newstest(headline,story,date,park_id) VALUES(:headline,:story,:date,:park_id)"); $stmt-&gt;execute(array(':headline' =&gt; $headline, ':story' =&gt; $story, ':date' =&gt; $date, ':park_id' =&gt; $park_id)); $affected_rows = $stmt-&gt;rowCount(); } ?&gt; &lt;form action="" method="post"&gt; Select Park : &lt;select name="park_id"&gt; &lt;option value=""&gt;Select Park&lt;/option&gt; &lt;?php foreach ($res as $row) { printf('&lt;option value="%s"&gt;%s&lt;/option&gt;' . PHP_EOL, $row['park_id'], $row['name']); } ?&gt; &lt;/select&gt; Headline: &lt;input type="text" name="headline"&gt; &lt;br&gt; Story: &lt;input type="textbox" name="story"&gt; &lt;br&gt; Date: &lt;input type="date" name="date"&gt; Password :&lt;input type="password" value="" name="password" id="password"&gt; &lt;input type="submit"&gt; &lt;/form&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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