1

基本上我正在尝试使用 PHP 来更新 MySQL 数据库,并且我正在使用 HTML 表单对其进行测试。

我打算在 android 应用程序中使用它,以便从中获取值,但目前我只是使用 HTML 表单进行测试以测试 PHP 代码。当我使用 HTML 表单进行测试时,当前没有更新相应的数据。

我的代码有什么问题导致这种情况?

PHP代码:

/*
* Following code will create a new product row
* All player details are read from HTTP Post Request
*/

// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['PlayerID']) && isset($_POST['Score']) && isset($_POST['LastHolePlayed'])&&     
isset($_POST['Overall'])) {

$playerid = $_POST['PlayerID'];
$score = $_POST['Score'];
$lastholeplayed = $_POST['LastHolePlayed'];
$overall = $_POST['Overall'];

// include db connect class
require('db_connection.php');



// mysql inserting a new row
$result = mysql_query("UPDATE `week1` SET Score = `$score`, LastHolePlayed = `$lastholeplayed`, 

Overall` = $overall` WHERE PlayerID = `$playerid`");


// check if row inserted or not
if ($result) {
    // successfully inserted into database
    $response["success"] = 1;
    $response["message"] = "Player successfully added.";

    // echoing JSON response
    echo json_encode($response);
} else {
    // failed to insert row
    $response["success"] = 0;
    $response["message"] = "An error occurred.";

    // echoing JSON response
    echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";

// echoing JSON response
echo json_encode($response);
}

html代码:

<form action="http://localhost/realdeal/updateplayer.php" method="POST">
PlayerID <input type="text" id='PlayerID' name='PlayerID'><br/><br/>
Score <input type="text" id='Score' name='Score'><br/><br/>
LastHolePlayed <input type="text" id='LastHolePlayed' name='LastHolePlayed'><br/><br/>
Overall <input type="text" id='Overall' name='Overall'><br/><br/>

    &nbsp;  <input type="submit" value="submit">

</form>
4

3 回答 3

2

将您的查询更改为:

$result = mysql_query("UPDATE `week1` SET `Score` = '$score', `LastHolePlayed` = '$lastholeplayed', `Overall` = '$overall' WHERE `PlayerID` = '$playerid'");
于 2013-04-24T20:45:47.137 回答
0

你的sql语句是错误的。您可以按上述方式编写,也可以直接将不带撇号的语句编写为 - $result = mysql_query("UPDATE week1 SET Score=$score, LastHolePlayed=$lastholeplayed,Overall=$overall WHERE PlayerID=$playerid");

此外,您能否解释“未更新适当的数据”是什么意思。如果你给出/说明你得到的错误会更清楚。

于 2013-04-24T21:04:35.950 回答
0

您的查询分隔符需要更正:

$result = mysql_query("UPDATE `week1` SET Score = '$score', `LastHolePlayed` = '$lastholeplayed', `Overall` = '$overall' WHERE `PlayerID` = '$playerid'");

请注意列和表名称周围的反引号 (`) 以及值周围的单引号 (')。

此外,在调试查询时,请始终检查 MySQL 错误:

$result mysql_query(...) or die("Query failed: " . mysql_error() );

最后,您应该知道您的查询会使您容易受到 SQL 注入攻击。在将其包含在查询中之前,请始终清理您的输入数据。

于 2013-04-24T21:00:49.167 回答