Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 让mysql进行多个更新_Php_Mysql - Fatal编程技术网

Php 让mysql进行多个更新

Php 让mysql进行多个更新,php,mysql,Php,Mysql,我有一张这样的桌子 。我正在尝试了解如何使用不同的值更新多行,但我不明白。解决方案无处不在,但对我来说,似乎很难理解 我当前的代码将始终更新类型B echo "<form id='set1' role='form' action=" . $_SERVER['PHP_SELF'] . " method=\"post\">"; echo "<table class='table table-hover table-striped table-bordered'><the

我有一张这样的桌子

。我正在尝试了解如何使用不同的值更新多行,但我不明白。解决方案无处不在,但对我来说,似乎很难理解

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

}
echo”“;
回声“;
呼应“范畴”;
对于($m=1;$mquery(“从“$DB->prefix(“statistic”)中选择*。”其中year='$year'和statistic_id='$statistic_id'))或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'];
$二月=$row[‘二月’];
$march=$row['march'];
$april=$row['april'];
$may=$row['may'];
$june=$row['june'];
$july=$row['july'];
$august=$row['august'];
$SEPTURE=$row['SEPTURE'];
$十月=$row['十月];
$十一月=$row['十一月];
$december=$row['december'];
回声“;
回显“$categoryname”;
对于($m=1;$mquery(“更新”。$DB->prefix(“mystatistik_bulanan”)”)设置
一月=“.mysql_escape_字符串(trim($_POST['一月]])。”,
二月=“.mysql_escape_字符串(trim($_POST['二月]))。”,
march=“.mysql_escape_string(trim($_POST['march'])。”,
april='”.mysql_escape_字符串(trim($_POST['april'])。”,
may=“.mysql_escape_string(trim($\u POST['may']))。”,
june=“.mysql_escape_字符串(trim($_POST['june']))。”,
july='”.mysql\u escape\u字符串(trim($\u POST['july'])。”,
八月=“.mysql_escape_字符串(trim($_POST['august'])。”,
九月=“.mysql_escape_字符串(trim($_POST['九月]))。”,
十月=“.mysql_escape_字符串(trim($_POST['十月]))。”,
十一月=“.mysql_escape_字符串(trim($_POST['十一月]])。”,
十二月=“.mysql_escape_字符串(trim($_POST['十二月]))。”“
其中年份=“$\u POST['year']”和类别名称=“$\u POST['categoryname']”和id=“.$\u 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>


它将只执行一次。您必须使用foreachloop,并且必须以数组而不是单个值的形式发送数据。例如
name=“id”
,因此它应该像
name=“id[]”一样
您的脚本有风险,请查看一下使用
value=“id]时发生的情况“$$monthName.
?谢谢@Nirav Joshi.。这确实帮助了我。我使用了你的代码
<?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;