gpt4 book ai didi

php - 让mysql进行多次更新

转载 作者:行者123 更新时间:2023-11-28 23:13:48 25 4
gpt4 key购买 nike

我有一张看起来像这样的 table enter image description here

。我想了解如何更新具有不同值的多行,但我就是不明白。解决方案无处不在,但对我来说似乎很难理解。

我当前的代码现在将始终更新类型 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']);

}

最佳答案

这里是更新多个值的例子

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;

希望对您有所帮助。

关于php - 让mysql进行多次更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44797684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com