1

EDIT: I'm not sure what to really say. The comments was what helped my issue out right now. I didn't remember the fact to show an error, and that was what helped me. I had pretty much just tried something that obviously wasn't working due to wrongly placed variables and such,and displaying the error code let me know what variables was the faulty ones. Thank you for all your help, and this issue is now resolved!

So long story short, the title. I have no idea why this code refuses to input the inserted data into the database, and I've tried a bunch of things that haven't resulted any better.

Connection:

<?php
session_start();

$host = 'host';
$dbusername = 'username';
$dbpassword = 'password';

$anslutning = mysqli_connect($host, $dbusername, $dbpassword) or die("<b>Could not connect to database server</b>");

$anslutning->select_db('databasename') or die("<b>Could not connect to the specified database</b>");

?>

Form to grab data from :

echo '
                <h2><center>Post a topic</center></h2>
                <br /><br />
                <div class="indexform">
                    <form action="index.php" method="POST">
                        Title: <input type="text" name="title"> <br /> <br />
                        Content: <textarea name="content" class="content"> </textarea> 
                        <br /> <br />
                        <input type="submit" name="postTopicOnGeneral">
                    </form>
                </div>
                ';

PHP code to insert it into database

if(isset($_POST['postTopicOnGeneral'])) {

                $username = $_POST['username'];
                $title = $_POST['title'];
                $content = $_POST['content'];
                $general = $_POST['general'];

                $addPostOnGeneral = $anslutning->prepare('INSERT INTO tblPosts(title, content, author, category) VALUES(?, ?, ?, ?)');
                $addPostOnGeneral->bind_param("ssss", $title, $content, $username, $general);
                $addPostOnGeneral->execute();

                echo "<center>Post created!</center>";
                sleep(2);
                echo "<script> window.location.href = 'index.php?general=1' </script>";

            }
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
Xariez
  • 759
  • 7
  • 24
  • Do you recieve any error outputs? When no, try to activate error reporting on top of your php file with `error_reporting(E_ALL)`, and test it again. What do you recieve? – fabpico Oct 04 '15 at 10:50
  • Do you get *any* output? Enable `error_reporting`, check [`mysqli::$error`](http://stackoverflow.com/questions/22662488/how-to-get-mysqli-error-in-different-environments). Remove all `isset`s for testing. – mario Oct 04 '15 at 10:51
  • @FabianPicone Undefined index:username and Undefined:general is what I get from that. Trying to fiddle around with those issues right now.. – Xariez Oct 04 '15 at 10:58
  • @Xariez Check if it works now? – DirtyBit Oct 04 '15 at 11:16
  • @HawasKaPujaari It does work now. See my edit. – Xariez Oct 06 '15 at 13:16

2 Answers2

1

Undefined index:username and Undefined:general is what I get from that.

You are bound to get that error since you are not fetching $username and $general from anywhere in your form.

Change your form this:

echo '
  <h2><center>Post a topic</center></h2>
  <br /><br />
  <div class="indexform">
  <form action="index.php" method="POST">
  Title: <input type="text" name="title"> <br /> <br />
  Content: <textarea name="content" class="content"> </textarea> 
  <br /> <br />
  <input type="submit" name="postTopicOnGeneral">
  </form>
  </div>';

to this:

echo '
  <h2><center>Post a topic</center></h2>
  <br /><br />
  <div class="indexform">
  <form action="index.php" method="POST">
  Title: <input type="text" name="title"> <br /> <br />
  Content: <textarea name="content" class="content"> </textarea> 
  <br /> <br />
  Username:
  <input type="text" name="username">
  General:
  <input type="text" name="general">
  <input type="submit" name="postTopicOnGeneral">
  </form>
  </div>';

And then in your index.php:

 if(isset($_POST['postTopicOnGeneral'])) {
    echo   $username = $_POST['username'];
    echo   $title = $_POST['title'];
    echo   $content = $_POST['content'];
    echo  $general = $_POST['general']; 
    // rest of your code
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
0

Try to change

$username = $_POST['username'];
$title    = $_POST['title'];
$content  = $_POST['content'];
$general  = $_POST['general'];

With this:

$username = isset($_POST['username']) ? $_POST['username'] : '';
$title    = isset($_POST['title'])    ? $_POST['title']    : '';
$content  = isset($_POST['content'])  ? $_POST['content']  : '';
$general  = isset($_POST['general'])  ? $_POST['general']  : '';
fabpico
  • 2,628
  • 4
  • 26
  • 43