简体   繁体   中英

PHP using Xampp : POST is not allowed error

when it try to submit the form content to the database i get the following error : {"code":"MethodNotAllowedError","message":"POST is not allowed"}

I am using Dreamweaver cc 2017 and xampp v3.2.2 to run Apache and Mysql on windows 10.

html form :

  <form action="register.php" id="form1" name="form1" method="post"> 

and register.php content is :

<?php
$hostname_conn = "localhost";
$database_conn = "hotels";
$username_conn = "root";
$password_conn = "";
$conn = mysqli_connect($hostname_conn, $username_conn, $password_conn) or trigger_error(mysqli_error(),E_USER_ERROR); 

$db=mysqli_select_db($conn,$database_conn);
$q1= "insert into users(username,email,password,number)values('$_POST[textfield]','$_POST[email]','$_POST[password]','$_POST[number]')";
$q2=mysqli_query($conn,$q1);

if(mysqli_query($conn,$q1))
 {
    $msg = 'User information saved successfully.';
 }
 else
 {
    $msg = 'Error: We encountered an error while inserting the new record.';
 }
echo $msg;
mysqli_close($conn);
?>

</body>
</html>

using PDO:

        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "hotels";
        $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
        $stmt = $conn->prepare("INSERT INTO users (username, email, password, number) 
        VALUES (:username, :email, :password, :number)");
        $stmt->bindParam(':username', $username);
        $stmt->bindParam(':email', $email);
        $stmt->bindParam(':password', $password);
        $stmt->bindParam(':number', $number);
        $username = $_POST['textfield'];
        $email = $_POST['email'];
        $password = $_POST['password'];
        $number = $_POST['number'];
        $stmt->execute();

The problem is for syntax. Anyway you should to use good practices. Check this post How can I prevent SQL injection in PHP?

$username = $_POST["textfield"]; //unsafe
$username = mysql_real_escape_string($username); //safe

$email = mysql_real_escape_string($_POST["email"]);
$password = mysql_real_escape_string($_POST["password"]);
$number = mysql_real_escape_string($_POST["number"]);

$db=mysqli_select_db($conn,$database_conn);
$q1= "insert into users(username,email,password,number)values('".$username ."','".$email."','".$password."','".$number."')";
$q2=mysqli_query($conn,$q1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM