使用mysqlserver和PHP更新数据库上的值

时间:2017-07-20 18:37:59

标签: php sql sql-server

我想知道是否有人可以指出我在正确的道路上。有一个包含字段的数据库:PersonIDFirstNameLastNameisActive

我从表单中收集这些值:

<form action="" method="POST" id="myForm"><input type="hidden" name="hdnId" id="hdnId" value="<?php echo $var_PersonID?>">

First Name<br>
<input type="text" name="txt_first_name" id="txt_first_name" value="<?php echo $var_FirstName;?>">
Last Name<br>
<input type="text" name="txt_last_name" id="txt_last_name" value="<?php echo $var_LastName;?>">
  Status <br>
    <select name="status" id="status">
<option value=""  > </option>
<option value="1" >Active</option>
<option value="2" >Inactive</option>
   </select>
   </form>

提交表单时的PHP代码如下所示:

<?php
        if(isset($_POST['Save']) || isset($_POST['Saveexit']))
        {
            $var_PersonID        = $_POST['hdnId'];
            $var_FirstName       = $_POST['txt_first_name'];
            $var_LastName        = $_POST['txt_last_name'];
            $var_IsActive        = $_POST['status'];
              if($var_PersonID!='')
          {
              $sql = "UPDATE vw_Persons SET FirstName, LastName, isActive = ?  WHERE PersonID  = $var_PersonID ";



              $params = array($var_FirstName, $var_LastName,$var_IsActive);


              $stmt = sqlsrv_query( $gaSql['link'], $sql, $params); 
       }
     }  ?>

加载页面后,我会抓住这样的值:

enter code here
 <?php
         if(isset($_GET['id']))
          {
        $query = "SELECT * FROM vw_Persons WHERE PersonID = '" . $_GET['id'] . "'";
    $result = sqlsrv_query($gaSql['link'], $query) or die("<pre>" . print_r(sqlsrv_errors()) . "<br><br>$query</pre>");
    $obj = sqlsrv_fetch_object( $result );

    $var_PersonID        = $obj->PersonID;
    $var_LastName        = $obj->LastName;
    $var_FirstName       = $obj->FirstName;
    $var_IsActive       = $obj->isActive;

  }  ?>

因此,查询会更新FirstNameLast Name值但不会更新isActive值,为什么会发生这种情况?

1 个答案:

答案 0 :(得分:0)

很好,因为isActive字段是一个位类型字段,它只接受值1和0所以我将其更改为<option value="0" >Inactive</option>并且它有效