Note that there are some explanatory texts on larger screens.

plurals
  1. POForm not posting to database - PHP
    primarykey
    data
    text
    <p>I'm fairly new to PHP and designing a small blog. I'm trying to submit a form to create a new post but it isn't working and I can't figure out why. I've compared it to my working code for the register form and it seems the same. I'm not getting any errors, it's just reloading the page and not posting to the database. </p> <p>View - v_newpost.php</p> <pre><code> &lt;article&gt; &lt;?php if (!isset ($_SESSION['username'])) { ?&gt; &lt;span class="alert"&gt;Please login to create a post.&lt;/span&gt; &lt;?php } else { ?&gt; &lt;form class="newpost" action="" method="post"&gt; &lt;fieldset&gt; &lt;legend&gt;Submit a new post&lt;/legend&gt; &lt;?php if ($error['alert'] != '') { echo "&lt;span class='alert'&gt;".$error['alert']."&lt;/span&gt;";} ?&gt; &lt;ul&gt; &lt;li&gt; &lt;label for="title"&gt;Title:&lt;/label&gt; &lt;input type="text" name="title" value="&lt;?php echo $input['title']; ?&gt;" required autofocus&gt; &lt;/li&gt; &lt;li&gt; &lt;label for="content"&gt;Content:&lt;/label&gt; &lt;textarea id="content" name="content" rows=6 value="&lt;?php echo $input['content']; ?&gt;"&gt;&lt;/textarea&gt; &lt;/li&gt; &lt;/ul&gt; &lt;/fieldset&gt; &lt;fieldset&gt; &lt;button type="submit" class=postbutton&gt;Publish&lt;/button&gt; &lt;/fieldset&gt; &lt;/form&gt; &lt;/div&gt; &lt;?php } ?&gt; &lt;/article&gt; </code></pre> <p>newpost.php</p> <pre><code> &lt;?php require_once 'includes/connection.php'; $error['alert'] = ''; $input['title'] = ''; $input['content'] = ''; if (isset($_POST['submit'])) { if ($_POST['title'] == '' || $_POST['content'] == '') { $error['alert'] = 'Please give your post a title and content.'; $input['title'] = $_POST['title']; $input['content'] = $_POST['content']; include_once('v_newpost.php'); } else { $input['title'] = htmlentities($_POST['title'], ENT_QUOTES); $input['content'] = htmlentities($_POST['content'], ENT_QUOTES); if ($stmt = $mysqli-&gt;prepare("INSERT INTO posts (title, content) VALUES (?,?)")) { $stmt-&gt;bind_param("ss", $input['title'], $input['content']); $stmt-&gt;execute(); $stmt-&gt;close(); $error['alert'] = ''; $input['title'] = ''; $input['content'] = ''; header('Location: index.php'); } else { $error['alert'] = 'Failed to create post'; } } } else { include_once('v_newpost.php'); } ?&gt; </code></pre> <p>I'm sure it's probably something stupid, but I've looked over it so many times and can't understand why it isn't working...</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