如何使用PHP更新sqlsrv中的数据

时间:2016-07-03 06:26:01

标签: php sqlsrv

大家好,我在这里度过了难关。我想通过使用PHP更新SQL Server中的数据,但它一直在失败。谁能帮我这个?这是我用过的示例代码。请大家帮助我,我被指派创建它,它让我失败。我不知道问题出在哪里。我将感谢你的帮助。

<?php
     if(isset($_POST['update'])) {
        $server = "WIN-2012SRV-BK";
        $user = "sa";
        $pass = "";
        $db = "test";
        $connInfo = array("Database"=>$db, "UID"=>$user, "PWD"=>$pass);
        $conn = sqlsrv_connect($server, $connInfo) or die( print_r( sqlsrv_errors(), true));


        //$id = $_POST['id'];
        //$emp_salary = $_POST['emp_salary'];
        $pin = $_POST['pin'];
        $phone = $_POST['phone'];

        $sql = "UPDATE [test].[dbo].[subscriptions] ". "SET pin = {$pin} ". 
           "WHERE phone = {$phone}" ;
        //$sqlsrv_select_db('test');
        $retval = sqlsrv_query( $sql, $conn );

        if(! $retval ) {
           die('Could not update data: ');
        }
        echo "Updated data successfully\n";

        mysql_close($conn);
     }else {
        ?>
           <form method = "post" action = "<?php $_PHP_SELF ?>">
              <table width = "400" border =" 0" cellspacing = "0" 
                 cellpadding = "0">

                 <tr>
                    <td width = "100">Phone Number</td>
                    <td><input name = "phone" type = "text" 
                       id = "phone"></td>
                 </tr>

                 <tr>
                    <td width = "100"> </td>
                    <td> </td>
                 </tr>

                 <tr>
                    <td width = "100"> </td>
                    <td>
                       <input name = "update" type = "submit" 
                          id = "update" value = "Update">
                    </td>
                 </tr>

              </table>
           </form>
        <?php
     }
  ?> 

我得到的错误是

警告:sqlsrv_query()要求参数1为资源,字符串在第70行的C:\ xampp \ htdocs \ visionFund \ pin-reset.php中给出 无法更新数据:

1 个答案:

答案 0 :(得分:0)

您必须将连接字符串放在查询中:

$sql = "$connection_string,UPDATE [test].[dbo].[subscriptions] ". "SET pin = {$pin} ". "WHERE phone = {$phone}" ;