更新不在mysql中工作

时间:2014-02-25 10:43:07

标签: php mysql

这是我的update.php代码,我想我写了一个错误的代码,但是找不到我做错的地方只是告诉我,如果这样的情况可以使用

EDIT --->我希望level1只更新一个来自level1区域的字段,其他都应该保持相同但是当我从一个级别的用户提交一个级别用户无法看到他们向他隐藏的其他级别用户的字段时它们变空了,

if(!empty($_REQUEST['update'])){

    $sql = "SELECT * FROM `tabl1` WHERE `ID` = ".$_REQUEST['update'];
    $query = mysql_query( $sql );
    $res = mysql_fetch_array($query);

    $IndexNo= $_POST['IndexNo'];
    $ZoneID = $_POST['ZoneID'];
    $NameofCoordinator= $_POST['NameofCoordinator'];
    $GridNumber= $_POST['GridNumber'];
    $MobileNumber= $_POST['MobileNumber'];
    $StatusID= $_POST['StatusID'];

    $sql="UPDATE `tabl1` SET `IndexNo` = '$IndexNo',`ZoneID` = '$ZoneID'"; 
    $sql .= if($userName =='admin' || 'level1'){ ",`NameofCoordinator` = '$NameofCoordinator'"; }
    $sql .= if($userName =='admin' || 'level2'){ ", `GridNumber` = '$GridNumber'"; }
    $sql .= if($userName =='admin' || 'level3'){ ",`MobileNumber` = '$MobileNumber'"; }
    $sql .= ",`Keyword` = '$Keyword', `StatusID` = '$StatusID' WHERE `ID` = ".$_REQUEST['update'];
}

4 个答案:

答案 0 :(得分:1)

$sql="UPDATE `table1` SET `IndexNo` = '$IndexNo',`ZoneID` = '$ZoneID'"; 
            $sql .= if($userName =='admin' || 'level1'){ ",`NameofCoordinator` = '$NameofCoordinator',`RegistrationSurveyDate` = '$RegistrationSurveyDate'"; }
            $sql .= if($userName =='admin' || 'level2'){ ", `GridNumber` = '$GridNumber',`ESRCommandArea` = '$ESRCommandArea'"; }
            $sql .= if($userName =='admin' || 'level3'){ ",`MobileNumber` = '$MobileNumber', `LandlineNumber` = '$LandlineNumber', `ConnectionCategory` = '$ConnectionCategory'"; }
            $sql .= ",`Keyword` = '$Keyword', `StatusID` = '$StatusID', `Comment`='$Comment' WHERE `ID` = ".$_REQUEST['update'];

添加到最后

  

的mysql_query($ SQL);

有任何错误吗?

答案 1 :(得分:1)

替换它

$sql="UPDATE `table1` SET `IndexNo` = '$IndexNo',`ZoneID` = '$ZoneID'"; 
            if($userName =='admin' || 'level1')
            {
                 $sql .= ",`NameofCoordinator` = '$NameofCoordinator',`RegistrationSurveyDate` = '$RegistrationSurveyDate'";
            }

            if($userName =='admin' || 'level2')
            { 
                $sql .= ", `GridNumber` = '$GridNumber',`ESRCommandArea` = '$ESRCommandArea'";
            }
            if($userName =='admin' || 'level3')
            { 
                $sql .= ",`MobileNumber` = '$MobileNumber', `LandlineNumber` = '$LandlineNumber', `ConnectionCategory` = '$ConnectionCategory'"; 
            }
                $sql .= ",`Keyword` = '$Keyword', `StatusID` = '$StatusID', `Comment`='$Comment' WHERE `ID` = ".$_REQUEST['update'];


myql_query($sql) or die();

答案 2 :(得分:0)

试试这个

 $sql=mysql_query("UPDATE `ocwconnections` SET `IndexNo` = '$IndexNo',`ZoneID` = '$ZoneID'
        `NameofCoordinator` = if($userName =='admin' || $userName == 'level1' ,'$NameofCoordinator' , ''),
        `GridNumber`    = if($userName =='admin' || $userName == 'level2' ,'$GridNumber' , ''),   
        `MobileNumber` = if($userName =='admin' || $userName == 'level3' ,$MobileNumber ,''),
        `Keyword` = '$Keyword', `StatusID` = '$StatusID' WHERE `ID` = ".$_REQUEST['update'] ");

或者,如果你想像你一样分开这样做:

$sql=mysql_query("UPDATE `ocwconnections` SET `IndexNo` = '$IndexNo',`ZoneID` = '$ZoneID' ";
$sql .= " `NameofCoordinator` = if($userName =='admin' || $userName == 'level1' ,'$NameofCoordinator' , ''), ";
$sql .= " `GridNumber`    = if($userName =='admin' || $userName == 'level2' ,'$GridNumber' , ''), ";   
$sql .= " `MobileNumber` = if($userName =='admin' || $userName == 'level3' ,$MobileNumber ,''), ";
$sql .= " `Keyword` = '$Keyword', `StatusID` = '$StatusID' WHERE `ID` = ".$_REQUEST['update'] ");

注意:我已经在每个列中更新了'',当if语句在条件中没有返回true时,您可以将其更改为您想要更新的值。

编辑>

   $sql=mysql_query("UPDATE `ocwconnections` SET `IndexNo` = '$IndexNo',`ZoneID` = '$ZoneID' , 
    `NameofCoordinator` = if($userName =='admin' || $userName == 'level1' ,'$NameofCoordinator' , `NameofCoordinator`),
    `GridNumber`    = if($userName =='admin' || $userName == 'level2' ,'$GridNumber' , `GridNumber`),   
    `MobileNumber` = if($userName =='admin' || $userName == 'level3' ,$MobileNumber ,`MobileNumber`),
    `Keyword` = '$Keyword', `StatusID` = '$StatusID' WHERE `ID` = ".$_REQUEST['update'] ");

答案 3 :(得分:0)

1)首先,不要将你的帖子数据分配给变量,而是按原样使用。 2)使用三元运算符而不是if条件

检查以下代码

如果(!空($ _ REQUEST [ '更新'])){     $ sql =“SELECT * FROM tabl1 WHERE ID =”。$ _ REQUEST ['update'];     $ query = mysql_query($ sql);     $ res = mysql_fetch_array($ query);

$sql = "UPDATE `tabl1` SET `IndexNo` = '$_POST['IndexNo']',`ZoneID` = '$_POST['ZoneID']'"; 
$sql.= ($userName =='admin' || 'level1')?(",`NameofCoordinator` = '$_POST[\'NameofCoordinator\']'")
      :(($userName =='admin' || 'level2')?(`GridNumber` = '$_POST[\'GridNumber\']'")
      :(($userName =='admin' || 'level3')?(`MobileNumber` = '$_POST[\'MobileNumber\']'"):''));
$sql .= ",`Keyword` = '$Keyword', `StatusID` = '$_POST[\'StatusID\']' WHERE `ID` = ".$_REQUEST['update'];

}