我正在尝试将浮点值从表单更新到数据库。我的表单正在正确读取数据并显示该值是否已存储,但是当我更新时没有任何反应:
if ($_POST['action'] == 'Update') {
foreach($_POST['game'] as $game) {
$spread = ((floatval($game['spread']) > 0) ? $game['spread'] : 'NULL');
$sql = "update " . DB_PREFIX . "schedule ";
$sql .= "set spread = " . $spread . " ";
$sql .= "where gameID = " . $game['gameID'] && "CAST('spread' AS DECIMAL(5,1)) = " . $game['spread'];
$mysqli->query($sql) or die('Error updating score: ' . $mysqli->error);
}
header('Location: ./');
exit;
}
这是我在阅读数据并在表单上显示的地方:
<?php
$sql = "select s.*, ht.city, ht.team, ht.displayName, vt.city, vt.team, vt.displayName ";
$sql .= "from " . DB_PREFIX . "schedule s ";
$sql .= "inner join " . DB_PREFIX . "teams ht on s.homeID = ht.teamID ";
$sql .= "inner join " . DB_PREFIX . "teams vt on s.visitorID = vt.teamID ";
$sql .= "where weekNum = " . $week . " ";
$sql .= "order by gameTimeEastern";
$query = $mysqli->query($sql);
if ($query->num_rows > 0) {
echo '<table class="table table-striped">' . "\n";
echo ' <tr><th colspan="6" align="left">Week ' . $week . '</th></tr>' . "\n";
$i = 0;
while ($row = $query->fetch_assoc()) {
$homeTeam = new team($row['homeID']);
$visitorTeam = new team($row['visitorID']);
$rowclass = (($i % 2 == 0) ? ' class="altrow"' : '');
echo ' <tr' . $rowclass . '>' . "\n";
echo ' <td><input type="hidden" name="game[' . $row['gameID'] . '][gameID]" value="' . $row['gameID'] . '" />' . date('D n/j g:i a', strtotime($row['gameTimeEastern'])) . ' ET</td>' . "\n";
echo ' <td align="right"><input type="hidden" name="gameID[' . strtolower($visitorTeam->team) . ']" value="' . $row['gameID'] . '" />' . $visitorTeam->teamName . '</td>' . "\n";
echo ' <td><input type="text" name="game[' . $row['gameID'] . '][spread]" id="game[' . $row['gameID'] . '][spread]" value="' . $row['spread'] . '" size="3" /></td>' . "\n";
echo ' <td align="right"><input type="hidden" name="gameID[' . strtolower($homeTeam->team) . ']" value="' . $row['gameID'] . '" />at ' . $homeTeam->teamName . '</td>' . "\n";
echo ' </tr>' . "\n";
$i++;
}
echo '</table>' . "\n";
}
$query->free;
?>
有关如何发布更新值的任何建议?
以下是现在的整个代码:
<?php
require('includes/application_top.php');
require('includes/classes/team.php');
if (!$user->is_admin) {
header('Location: ./');
exit;
}
if ($_POST['action'] == 'Update') {
foreach($_POST['game'] as $game) {
$spread = ((floatval($game['spread']) > 0) ? $game['spread'] : 'NULL');
$sql = "update " . DB_PREFIX . "schedule ";
$sql .= "set spread = " . $spread . " ";
$sql .= "where gameID = " . $game['gameID'] . " && CAST('spread' AS DECIMAL(5,1)) = " . $game['spread'];
$mysqli->query($sql) or die('Error updating spread: ' . $mysqli->error);
}
header('Location: ./');
exit;
}
$week = (int)$_GET['week'];
if (empty($week)) {
//get current week
$week = (int)getCurrentWeek();
}
include('includes/header.php');
?>
<h1>Enter Spreads - Week <?php echo $week; ?></h1>
<?php
//display week nav
$sql = "select distinct weekNum from " . DB_PREFIX . "schedule order by weekNum;";
$query = $mysqli->query($sql);
$weekNav = '<div class="navbar3"><b>Go to week:</b> ';
$i = 0;
while ($row = $query->fetch_assoc()) {
if ($i > 0) $weekNav .= ' | ';
if ($week !== (int)$row['weekNum']) {
$weekNav .= '<a href="spreads.php?week=' . $row['weekNum'] . '">' . $row['weekNum'] . '</a>';
} else {
$weekNav .= $row['weekNum'];
}
$i++;
}
$query->free;
$weekNav .= '</div>' . "\n";
echo $weekNav;
?>
<script type="text/javascript">
function getScores(weekNum) {
$.get("getHtmlScores.php", {week: weekNum}, function(data) {
for(var item in data) {
visitorScoreField = document.getElementById('game[' + data[item].gameID + '][visitorScore]');
homeScoreField = document.getElementById('game[' + data[item].gameID + '][homeScore]');
OTField = document.getElementById('game[' + data[item].gameID + '][OT]');
if (visitorScoreField.value !== data[item].visitorScore) {
visitorScoreField.value = data[item].visitorScore;
visitorScoreField.className="fieldLoaded";
}
if (homeScoreField.value !== data[item].homeScore) {
homeScoreField.value = data[item].homeScore;
homeScoreField.className="fieldLoaded";
}
if (data[item].overtime == '1') {
OTField.checked = true;
}
}
},'json');
}
</script>
<p><input type="button" value="Load Scores" onclick="return getScores(<?php echo $week; ?>);" class="btn btn-info" /></p>
<form id="spreadForm" name="spreadForm" action="spreads.php" method="post">
<input type="hidden" name="week" value="<?php echo $week; ?>" />
<div class="table-responsive">
<?php
$sql = "select s.*, ht.city, ht.team, ht.displayName, vt.city, vt.team, vt.displayName ";
$sql .= "from " . DB_PREFIX . "schedule s ";
$sql .= "inner join " . DB_PREFIX . "teams ht on s.homeID = ht.teamID ";
$sql .= "inner join " . DB_PREFIX . "teams vt on s.visitorID = vt.teamID ";
$sql .= "where weekNum = " . $week . " ";
$sql .= "order by gameTimeEastern";
$query = $mysqli->query($sql);
if ($query->num_rows > 0) {
echo '<table class="table table-striped">' . "\n";
echo ' <tr><th colspan="6" align="left">Week ' . $week . '</th></tr>' . "\n";
$i = 0;
while ($row = $query->fetch_assoc()) {
$homeTeam = new team($row['homeID']);
$visitorTeam = new team($row['visitorID']);
$rowclass = (($i % 2 == 0) ? ' class="altrow"' : '');
echo ' <tr' . $rowclass . '>' . "\n";
echo ' <td><input type="hidden" name="game[' . $row['gameID'] . '][gameID]" value="' . $row['gameID'] . '" />' . date('D n/j g:i a', strtotime($row['gameTimeEastern'])) . ' ET</td>' . "\n";
echo ' <td align="right"><input type="hidden" name="gameID[' . strtolower($visitorTeam->team) . ']" value="' . $row['gameID'] . '" />' . $visitorTeam->teamName . '</td>' . "\n";
echo ' <td><input type="text" name="game[' . $row['gameID'] . '][spread]" id="game[' . $row['gameID'] . '][spread]" value="' . $row['spread'] . '" size="3" /></td>' . "\n";
echo ' <td align="right"><input type="hidden" name="gameID[' . strtolower($homeTeam->team) . ']" value="' . $row['gameID'] . '" />at ' . $homeTeam->teamName . '</td>' . "\n";
echo ' </tr>' . "\n";
$i++;
}
echo '</table>' . "\n";
}
$query->free;
?>
</div>
<input type="submit" name="action" value="Update" class="btn btn-info" />
</form>
<?php
include('includes/footer.php');
答案 0 :(得分:0)
更改您的更新查询,如下所示删除&&
并使用and
将其放入引号
if ($_POST['action'] == 'Update') {
foreach($_POST['game'] as $game) {
$spread = ((floatval($game['spread']) > 0) ? $game['spread'] : 'NULL');
$sql = "update " . DB_PREFIX . "schedule ";
$sql .= "set spread = " . $spread . " ";
$sql .= "where gameID = " . $game['gameID']." AND CAST('spread' AS DECIMAL(5,1)) = " . $game['spread'];
$mysqli->query($sql) or die('Error updating score: ' . $mysqli->error);
}
header('Location: ./');
exit;
}
还可以使用预备语句来防止sql注入
答案 1 :(得分:0)
好的,经过一些研究和测试后,最终的解决方案就是做出这样的改变:
$sql .= "where gameID = " . $game['gameID']." AND CAST('spread' AS DECIMAL(5,1)) = CAST('spread' AS DECIMAL(5,1))";