基本上我正在尝试使用 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/>
<input type="submit" value="submit">
</form>