。我正在尝试了解如何更新具有不同值的多行,而我却没有得到它。解决方案无处不在,但对我来说,它看起来很难理解。
我现在的代码它将始终更新类型B ..
echo "<form id='set1' role='form' action=" . $_SERVER['PHP_SELF'] . " method=\"post\">";
echo "<table class='table table-hover table-striped table-bordered'><thead><tr>";
echo "<th class='text-center'>Category</th>";
for($m=1; $m<=12; ++$m){
$monthName=date('M', mktime(0, 0, 0, $m, 1)).'<br>';
echo "<th class='text-center'>$monthName</th>";
}
echo "</tr></thead>";
$result= $DB->query("SELECT * FROM ".$DB->prefix("statistic")." WHERE year='$year' AND statistic_id='$statistic_id'") or die(mysql_error());
while($row = $DB->fetchArray($result)) {
$id=$row['id'];
$year=$row['year'];
$statistic_id=$row['statistic_id'];
$categoryname=$row['categoryname'];
$january=$row['january'];
$february=$row['february'];
$march=$row['march'];
$april=$row['april'];
$may=$row['may'];
$june=$row['june'];
$july=$row['july'];
$august=$row['august'];
$september=$row['september'];
$october=$row['october'];
$november=$row['november'];
$december=$row['december'];
echo "<tr>";
echo "<td class='text-center'>$categoryname</td>";
for($m=1; $m<=12; ++$m){
$monthName=strtolower(date('F', mktime(0, 0, 0, $m, 1)));
//echo "<td class='text-center'>".$$monthName." </td>";
echo "<td class='text-center'> <input style='text-align:center' class='form-control ".$monthName."' type='text' size='60' id='".$monthName."' name='".$monthName."' value='".$$monthName."'></td>";
}
echo "</tr>";
}
echo "</table>";
$total=$january+$february+$march+$april+$may+$june+$july+$august+$september+$october+$november+$december;
echo "<input type='hidden' name='id' value='$id'>";
echo "<input type='hidden' name='year' value='$year'>";
echo "<input type='hidden' name='categoryname' value='$categoryname'>";
echo "<button type='submit' name='update-stats' class='btn btn-default'>Save</button>";
echo "</form>";
//check if posted using the update link
if (isset($_POST['update-stats'])) {
//run the query to update the data
$DB->query("UPDATE ".$DB->prefix("mystatistik_bulanan")." SET
january='" . mysql_escape_string(trim($_POST['january'])) . "',
february='" . mysql_escape_string(trim($_POST['february'])) . "',
march='" . mysql_escape_string(trim($_POST['march'])) . "',
april='" . mysql_escape_string(trim($_POST['april'])) . "',
may='" . mysql_escape_string(trim($_POST['may'])) . "',
june='" . mysql_escape_string(trim($_POST['june'])) . "',
july='" . mysql_escape_string(trim($_POST['july'])) . "',
august='" . mysql_escape_string(trim($_POST['august'])) . "',
september='" . mysql_escape_string(trim($_POST['september'])) . "',
october='" . mysql_escape_string(trim($_POST['october'])) . "',
november='" . mysql_escape_string(trim($_POST['november'])) . "',
december='" . mysql_escape_string(trim($_POST['december'])) . "'
where year='".$_POST['year']."' AND categoryname='".$_POST['categoryname']."' AND id=" . $_POST['id']);
}
答案 0 :(得分:1)
以下是更新多个值的示例
<强> form.html 强>
<html>
<body>
<form method="post" action="update.php">
<input type="hidden" name="id[]" value="1" />
<input type="hidden" name="id[]" value="2" />
<input type="text" name="value[]" value="test" />
<input type="text" name="value[]" value="test1" />
<input type="submit" name="submit" value="submit" />
</form>
</body
</html>
<强> update.php 强>
<?php
if(isset($_POST['submit'])):
foreach($_POST['id'] as $key => $value):
$sql = "update tablename set name='".$_POST['value'][$key]."' where id = '".$_POST['id'][$key]."'";
//execute your query
endforeach;
endif;
希望这会对你有所帮助。