首页 > 解决方案 > 无法从本地托管的网页将表单发布到本地 mysql 服务器

问题描述

我正在尝试将表单的内容发布到本地托管的 MySQL 服务器,并且在运行以下 php 脚本时收到 405 Method Not Allowed 错误。

<?php

$host = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbname = "test";

$connect = mysqli_connect($host, $dbUsername, $dbPassword, $dbname);

mysqli_query($connect, "INSERT INTO data (nickname, location, crop, rowspacing, plantspacing, orientation)
VALUES ('$_POST[nickname-input]', '$_POST[location-input]', '$_POST[crop-input]', '$_POST[row-input]', '$_POST[plant-input], '$_POST[orientation-input]')");

?>

我启用了 SQL 服务器主机,并启用了所有权限,如此处所示。 在此处输入图像描述

php 脚本在以下 html 代码中运行,它在其中获取正确的值

<form name="main" action="insert.php" method="POST">
      <div class="grid-x">
      <div class="medium-2 cell">
        <label for="nickname" class="middle">Nickname</label>
      </div>
      <div class="medium-10 cell">
        <input type="text" id="nickname-input" required placeholder="Give your field a nickname for easier reference">
      </div>
    </div>
    <div class="grid-x">
      <div class="small-2 cell">
        <label for="location" class="middle">Location</label>
      </div>
      <div class="small-10 cell">
        <input type="text" id="location-input" required placeholder="Tell us where you are so we can obtain location based information">
      </div>
    </div>
    <div class="grid-x">
      <div class="small-2 cell">
        <label for="crop-type" class="middle">Crop Type</label>
      </div>
      <div class="small-10 cell">
        <select id="crop-input">
            <option value="" hidden disabled selected required>Supported Crops Shown here</option>
            <option value="pistachio">Pistachios</option>
            <option value="almond">Almonds</option>
        </select>
        </div>
        <div class="small-2 cell">
        <label for="row-spacing" class="middle">Row Spacing (ft)</label>
      </div>
      <div class="small-10 cell">
        <input type="number" id="row-input" required>
      </div>
      <div class="small-2 cell">
        <label for="plant-spacing" class="middle">Plant Spacing (ft)</label>
      </div>
      <div class="small-10 cell">
        <input type="number" id="plant-input" required>
      </div>
      <div class="small-2 cell">
      <label for="row-orientation" class="middle">Row Orientation</label>
      </div>
      <div class="small-10 cell">
        <select id="orientation-input" required>
            <option value="north-south">North-South</option>
            <option value="east-west">East-West</option>
        </select>
        </div>
      </div>




    <div class="medium-12 large-12 cell">
        <div class="button">
            <button type="submit" style="padding: 0px 35px"> Save </button>
        </div>
        <div class="button">Add Another Field</div>
      </div>

    </form>

因此,重申一下,在尝试将上述表单数据发布到 MySQL 数据库时,我收到了 405 Method Not Allowed 错误。网站和数据库都托管在我的机器上。

编辑:我刚刚查看了我网站的 node.js 日志,发现了这个:

"POST /insert.php" Error (404): "Not found"

405 错误的来源来自 chrome 开发者工具。

标签: phpnode.js

解决方案


我猜您正在使用npm http-server,因为您引用了“node.js 日志”,由于它是只读服务器,因此支持。POST你可以尝试像serverJSON-server这样支持的替代方案POST,但是 node 默认不支持 PHP,它只支持 javascript。

如果您已经在 MySQL 上使用 XAMPP,您可以将整个东西托管在那里并完全取消节点服务器,或者通过node.js mysql在 javascript 中重写您的 php 代码。

如果我模仿我认为是您的设置,则在提交表单时会遇到相同的错误:

D:\Test>http-server --cors -p 80

Starting up http-server, serving ./

Available on:  
  http://192.168.0.10:80  
  http://127.0.0.1:80  
Hit CTRL-C to stop the server  
[Thu May 24 2018 15:42:35 GMT-0700 (Pacific Daylight Time)] "GET /test.html" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36"
[Thu May 24 2018 15:42:41 GMT-0700 (Pacific Daylight Time)] "POST /insert.php" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36"
[Thu May 24 2018 15:42:41 GMT-0700 (Pacific Daylight Time)] "POST /insert.php" Error (404): "Not found"

推荐阅读