我在使用带有WHERE子句的UPDATE语句时遇到了困难
我使用以下代码尝试更新表中的记录WHERE squadnumber是squadnumber下拉列表中的值。
<img src="header.png" alt="Southside FC Header">
<h1>Player Statistics</h1>
<?php
require("config.inc.php");
if (!empty($_POST)) {
//initial query
$query = "UPDATE playerstatistics SET squadnumber=':squadnumber', appearances=':appearances', subappearances=':subappearances',
goalsscored=':goalsscored', yellowcards=':yellowcards', redcards=':redcards' WHERE squadnumber=':squadnumber'";
//Update query
$query_params = array(
':squadnumber' => $_POST['squadnumber'],
':appearances' => $_POST['appearances'],
':subappearances' => $_POST['subappearances'],
':goalsscored' => $_POST['goalsscored'],
':yellowcards' => $_POST['yellowcards'],
':redcards' => $_POST['redcards']
);
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
// For testing, you could use a die and message.
//die("Failed to run query: " . $ex->getMessage());
//or just use this use this one:
$response["success"] = 0;
header('Location: http://localhost/webservice/errorCouldNotAddData.php');
die(json_encode($response));
}
$response["success"] = 1;
header('Location: http://localhost/webservice/managerhomepage.php');
echo json_encode($response);
} else {
?>
<br />
<form action="addplayerstatistic.php" method="post">
Squad Number: <br />
<select name="squadnumber">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
</select>
<br />
Number of appearances:<br />
<input type="number" name="appearances"/><br />
Number of appearances as a substitute:<br />
<input type="number" name="subappearances"/><br />
Number of Goals Scored:<br />
<input type="number" name="goalsscored"/><br />
Number of yellow cards:<br />
<input type="number" name="yellowcards"/><br />
Number of red cards:<br />
<input type="number" name="redcards"/><br />
<input type="submit" value="Add statistics" />
</form>
<a href="managerhomepage.php"><img src="backButton.png"></a>
<?php
}
?>
$ response [&#34; success&#34;]被设置为1,因为查询似乎已正确运行,但是当我检查数据库时没有任何更改。
我认为我的问题是围绕WHERE子句没有设置为下拉列表中的数字,因此没有更新行。
有什么想法吗?
由于
答案 0 :(得分:0)
尝试更新您的查询
$query = "UPDATE playerstatistics SET squadnumber=:squadnumber, appearances=:appearances, subappearances=:subappearances,
goalsscored=:goalsscored, yellowcards=:yellowcards, redcards=:redcards WHERE squadnumber=:squadnumber";
你不应该逃避你的参数--PDO会为你做这件事。