0

我想向 php 发送一些数据以更改我的 MySQL 数据库中的某些内容,但我不断收到此 405 错误“不允许使用 POST 方法”。我用谷歌搜索了很多,但找不到任何有用的解决方案。有人可以向我解释我做错了什么吗?

我在 HTML 中使用 Razor 在网页上呈现数据:

@foreach(var row in db.Query(getKamers))
     {            
        <form id="@row.id" action='_DataConn.php' method='post' class='ajaxform'>                    
            <input type="text" value="@row.id" name="id" id="td-id" />
            <input type="text" value="@row.oppervlakte" name="oppervlakte" id="td-opp" />
            <input type="text" value="@row.locatie" name="locatie" id="td-loc" />
            <input type="text" value="@row.type" name="type" id="td-type" />
            <input type="text" value="@row.kamernr" name="nummer" id="td-kamernr" />
            <input type="text" value="@row.vrij" name="vrij" id="td-vrij" />
            <input type="submit" value="opslaan" name="opslaan" id="@row.id" />                
        </form>              
     }

然后我有这个 JavaScript 文件:

$(document).ready(function () {
$('.ajaxform').submit(function () {
    $.ajax({
        url: $(this).attr('action'),
        type: $(this).attr('method'),
        dataType: 'json',
        data: $(this).serialize(),
        success: function (data) {
            console.log(data);
        }
    });

    return false;
});

});

我的 PHP 看起来像这样:

<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "root";
$dbname = "Studentenkamers";
//Connect to MySQL Server
mysql_connect($dbhost, $dbuser, $dbpass);
//Select Database
mysql_select_db($dbname) or die(mysql_error());
// Retrieve data from Query String


$id = $_POST['id'];
$oppervlakte = $_POST['oppervlakte'];
$locatie = $_POST['locatie'];
$kamernr = $_POST['nummer'];
$type = $_POST['type'];
$vrij = $_POST['vrij'];
echo 'ok'
// Escape User Input to help prevent SQL Injection

$id = mysql_real_escape_string($id);
$oppervlakte = mysql_real_escape_string($oppervlakte);
$locatie = mysql_real_escape_string($locatie);
$kamernr = mysql_real_escape_string($kamernr);
$type = mysql_real_escape_string($type);
$vrij = mysql_real_escape_string($vrij);
//build query
//"UPDATE Studentkamer SET oppervlakte='" + room[1] + "', locatie='" + room[2] + "', type='" + room[3] + "', vrij='" + room[4] + "' WHERE id='" + room[0] + "'";
$query = "UPDATE Studentkamer SET oppervlakte = '$id', locatie = '$locatie', type='$type', kamernr   = '$kamernr', vrij = '$vrij' WHERE id='$id'";

//Execute query
 $qry_result = mysql_query($query) or die(mysql_error());
 ?>

编辑:

下面是错误信息截图的链接,它与静态上下文有关吗? http://img41.imageshack.us/img41/9139/errorde.jpg

我正在使用 localhost 在 Webmatrix 中工作,我是否需要将此网站放到网上才能正常工作?

4

2 回答 2

1

查看提交按钮和表单 ID。剃须刀是否将其转换为有效字符串?在我看来,id 好像包含@. 根据http://www.w3.org/TR/html4/types.html@, id 值是不允许的。这可能作为包含这些字符的值传递

    ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number
 of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").

尝试

@(row.id)

参考 - MVC3 Razor 语法。如何将此 ASP.NET 样式转换为 Razor。(引号内的@也有问题)

于 2012-09-14T13:36:13.490 回答
0

您必须在您的网络服务器的配置中允许 POST 方法

于 2012-09-14T13:34:56.863 回答