0

I am trying to code a basic forum system, were an admin types a message and it posts on the "news" page on the website. I set up the "admin panel" where the admins can type their message and send it to the database. However, when I hit submit, it does not show up in phpMyAdmin. I even tried troubleshooting the if statements that check for "blank fields" and "sql errors." Does anyone have any idea why this is?

My text form where the information is entered

<form action="/includes/news_post.php" method="post">
    <div class = "container">
        <textarea name="message" rows="10" cols="100"></textarea>
        <br><br>
        <button type="submit" name="news_post" class="postbtn">Post</button>
    </div>
</form>

My php code for sending the message to the database.

<?php

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

    require 'dbh.inc.php';
    
    $newspost = $_POST['message'];
    $uid = $_SESSION['userId'];
    
    if(empty($newspost)) {
        header("Location:../admin?error=emptyfield");
        exit(); 
    }   
    else {  
        $sql = "INSERT INTO news (user_id, post) VALUES (?, ?)";
        $stmt = mysqli_stmt_init($conn);
        if (!mysqli_stmt_prepare($stmt, $sql)) {
            header ("Location:../admin?error=sqlerror");
            exit();  
        }           
        else {          
            mysqli_stmt_bind_param($stmt, "ss", $uid, $newspost);
            mysqli_stmt_execute($stmt);
            header("Location:../admin?news_post=success");
            exit();
        }
    }
    
    mysqli_stmt_close($stmt);
    mysqli_close($conn);
    
}
else {
  header("Location: ../admin?error=unknown");
  exit();
}

?>
  • 1
    phpMyAdmin is a MySQL administration tool written in PHP, it is not a database itself. You are probably using MySQL or MariaDB as your DB. – Dharman Aug 15 '20 at 01:09
  • You need to stop manually checking for errors. Please read: [Should we ever check for mysqli_connect() errors manually?](https://stackoverflow.com/q/58808332/1839439) and [Should I manually check for errors when calling “mysqli_stmt_prepare”?](https://stackoverflow.com/q/62216426/1839439) – Dharman Aug 15 '20 at 01:09
  • My suggestion would be to remove all the `exit()` they don't appear to be required that would allow the close statements to run (although not strictly required). That won't be the problem though. The execute statement returns false on failure so maybe check for that and you could also check `mysqli_stmt_error` after the execute. – Mark Aug 15 '20 at 11:50

0 Answers0