0

i created a simple message form and whenever i test it, the form action doesnt seem to work. it just leads me to an error page

<div class="form">
    <form action="connect.php" method="post">
        <input id="name" name="name" type="text" class="form-control" placeholder="Your Name" required>
        <br>
        <textarea name="message" id="message" class="form-control message" placeholder="Message" rows="4" required></textarea> <br>
        <input type="submit" class="form-control submit" value="SEND MESSAGE" name="submit">
    </form>
</div>

here is the php

<?php

    $name = $_POST['name'];
    $message = $_POST['message'];

    $conn = new mysqli ('localhost','root','','messg');
    if($conn->connect_error){
        die('Connection Failed : ' .$conn->connect_error);

    }else{

        $stmt = $conn->prepare("insert into mesages(name, message) values(?, ?)");
        $stmt->bind_param("ss",$name, $message);

        $stmt->execute();

        echo "Message Sent!";

        $stmt->close();
        $conn->close();
    }

?>  
Professor Abronsius
  • 33,063
  • 5
  • 32
  • 46
jioxx
  • 11
  • 6
  • 1
    What does the error page say? Also found [Should we check mysql errors manually](https://stackoverflow.com/questions/58808332/should-we-ever-check-for-mysqli-connect-errors-manually) useful myself. – Eugene Anisiutkin Feb 24 '20 at 11:36
  • Please post your error too, so that we can understand better – 5eeker Feb 24 '20 at 11:37
  • it leads to a page that says this: This page isn’t workingIf the problem continues, contact the site owner. HTTP ERROR 405 – jioxx Feb 24 '20 at 11:39
  • Turn PHP errors on to get the actual error: `error_reporting(E_ALL); ini_set('display_errors', 1);` – Brett Gregson Feb 24 '20 at 11:40
  • 1
    `HTTP 405` is *Method Not Allowed* ... which seems like you're server doesn't accept POST requests ... which would be very, very weird. – CD001 Feb 24 '20 at 11:41
  • Tested the same code on my localhost and it works fine. Seems like there's problem from your server configuration settings. – Kapil Karangeeya Feb 24 '20 at 12:06

1 Answers1

2

The HyperText Transfer Protocol (HTTP) 405 Method Not Allowed response status code indicates that the request method is known by the server but is not supported by the target resource.

The server MUST generate an Allow header field in a 405 response containing a list of the target resource's currently supported methods.

How to Fix the 405 Method Not Allowed

It may sound a little too simple but the most common cause of a 405 Method Not Allowed error is entering the wrong URL. Most web servers are tightly secured and designed to disallow access to improper URLs to prevent users from visiting the wrong pages (or trying to access pages that don’t exist).

Before going any further, therefore, double-check to ensure that you’ve entered the URL of the file you wish to access correctly. It’s easier than you might think to make mistakes – such as forgetting a letter or misspelling a word. You may also find that simply refreshing the page could prompt it to load correctly.

connect.php sounds like a database connection page which is not accessable or not set correctly, check if you are redirection form to correct page.

Mysqli Connection should look like this :

$host = '127.0.0.1';
$db   = 'test';
$user = 'root';
$pass = '';
$charset = 'utf8mb4';

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try {
    $mysqli = new mysqli($host, $user, $pass, $db);
    $mysqli->set_charset($charset);
} catch (\mysqli_sql_exception $e) {
     throw new \mysqli_sql_exception($e->getMessage(), $e->getCode());
}

And query like this :

$stmt = $mysqli->prepare("insert into mesages(name, message) values(?, ?)");
$stmt->bind_param("ss",$name, $message);
if($stmt->execute()){
echo "Message Sent!";
}
$stmt->close();
$conn->close();

See here for further details

To The Allow header lists the set of methods supported by a resource.

  • This is a very good response, but even though the connection strings OP has used aren't entirely following MySQLi php docs, I can't really see why it wouldn't work - It may be worth noting some cheap / free hosting sites disallow POST requests - which to quote a comment is really, really odd. – Jack Wright Feb 24 '20 at 14:01
  • Yes, it can be that too! The cause of a 405 Method Not Allowed reponse can be difficult to track down and fix. With a potential pool of over 50 status codes that represent the complex relationship between the client, a web application, a web server, and often multiple third-party web services, determining the cause of a particular status code can be a challenge under the best of circumstances. OP need to debug application code or scripts, Look through the server-side logs, Confirm server’s configuration etc.. to me its happening because of mixed connection mysqli OOP and procedural and query. –  Feb 24 '20 at 14:14