【问题标题】:why does the php function "sqlsrv_query" only work with 2 parameters sometimes but will need 4 other times?为什么 php 函数“sqlsrv_query”有时只能使用 2 个参数,但有时需要 4 个参数?
【发布时间】:2015-01-24 16:22:32
【问题描述】:

我在 Windows Server 2012 R2 上使用 SQL Server 2008 R2 运行 IIS,并且我有 2 个 php 页面:

  • 主页有 2 个字段集,第一个通过SELECT sql 语句显示数据库表。第二个包含一个将数据传递到第二个 php 页面的表单。

  • 第二个 php 页面从表单接收数据并执行UPDATE sql 语句,以便使用从表单接收到的值将新行添加到数据库表中。

主页代码:

<html>
<head>
</head>
<body>
<fieldset>
 <legend align="left">Administraci&oacuten</legend>
 <div>
  <fieldset style="height:250px; overflow:scroll;">
   <legend align="left">List</legend>
   <center>
   <?php

   $servername="server2012\SQLEXPRESS";
   $connect=array("Database"=>"Tienda","UID"=>"sa","PWD"=>"ciclo.01");
   $conn=sqlsrv_connect($servername, $connect);
   $sql="select * from almacen";
   $res=sqlsrv_query($conn, $sql);
   echo "<table border='1' width='100%'>";
   echo "<tr><td><b>PRODUCTO</b></td>
             <td><b>CODIGO</b></td>
             <td><b>PRECIO</b></td>
             <td><b>ALMACEN</b></td>
             <td><b>EXISTENCIAS</b></td>
         </tr>";
    while ($linea=sqlsrv_fetch_array($res)) {
    echo "<tr>
            <td>$linea[0]</td>
            <td>$linea[3]</td>
            <td>$linea[1]</td>
            <td>$linea[2]</td>
            <td>$linea[4]</td>
         </tr>";
    }

    echo "</table>";

    ?>
    </center>
   </fieldset>
   <a href="almacen.php">
    <input type="button" value="Refresh" style="float:right">
   </a>
 </div>

 <div style="float:left; width:50%;">
  <fieldset>
   <legend align="left">Insert</legend>
   <form action="insercion.php" method="post">
   <table>
    <tr><td>C&oacutedigo</td><td><input type="text" name="cod"></td></tr>
    <tr><td>Producto</td><td><input type="text" name="pro"></td></tr>
    <tr><td>Precio</td><td><input type="text" name="pre"></td></tr>
    <tr><td>Almacen</td><td><input type="text" name="alm"></td></tr>
    <tr><td>Existencias</td><td><input type="text" name="exi"></td></tr>
   </table>
   <input type="submit" value="Insert" style="float:right;">
   </form>
  </fieldset>
 </div>
</fieldset>
</body>
</html>

主页成功执行SQL语句并打印表格。

第二页的代码(包含UPDATE 语句):

<html>
<head>
</head>
<body>
<center>
<?php

$cod=$_POST['cod']; 
$pro=$_POST['pro']; 
$pre=$_POST['pre'];
$alm=$_POST['alm'];
$exi=$_POST['exi']; 

$servername="server2012\SQLEXPRESS";
$connect=array("Database"=>"Tienda","UID"=>"sa","PWD"=>"ciclo.01");
$conn=sqlsrv_connect($servername, $connect);
$sql="insert into almacen values('$pro','$pre','$alm','$cod','$exi')";
$params=array();
$options=array("Scrollable" => SQLSRV_CURSOR_KEYSET);
$res=sqlsrv_query($conn, $sql, $params, $options);
//$res=sqlsrv_query($conn, $sql);
$rows=sqlsrv_num_rows($res);

echo "<p>$rows row inserted</p>";
echo "<a href='almacen.php'><input type='button' value='Return'></a>";

?>

</center> 
</body>
</html>

第二页按原样工作,注释掉包含 2 个参数的原始 sqlsrv_query() 并添加行 $params=array();$options=array("Scrollable" =&gt; SQLSRV_CURSOR_KEYSET);,以便可以使用带有4个参数的sqlsrv_query()

那么,主页面中的 sqlsrv_query 仅使用 2 个参数而第二页中的 sqlsrv_query 使用 2 个参数失败的原因是什么? (第二页的输出信息将是row inserted而不是row inserted,返回主页面后,表格不会包含新行)。

如果我更改主页中的表单以使其仅包含 1 个字段,并且我将第二页中的 UPDATE sql 语句更改为将使用主页面的 DELETE 语句,这似乎也是这种情况主页面单字段表单传递的key值。

基本上,我想知道$params=array();这几行 和$options=array("Scrollable" =&gt; SQLSRV_CURSOR_KEYSET); 这样做以及为什么它们似乎并不总是对 sqlsrv_query 函数是必需的。

提前致谢

【问题讨论】:

  • 确保您启用了 PHP 的错误报告 - 始终在开发代码时,error_reporting(E_ALL); ini_set('display_errors', 1);。在调用sqlsrv_query() 之后,您必须检查错误和错误的返回值。 if (!$res) print_r(sqlsrv_errors()); 您的输入值不会针对 SQL 注入进行转义,因此您可能会与因此而无效的查询发生冲突。您应该直接使用 ? 占位符而不是变量,如文档中的示例 php.net/manual/en/function.sqlsrv-query.php

标签: php sql-server


【解决方案1】:

试试这个,它对我有用。

$sql="insert into almacen values(?,?,?,?,?)";
$params=array(
                array($pro,SQLSRV_PARAM_IN),
                array($pre,SQLSRV_PARAM_IN),
                array($alm,SQLSRV_PARAM_IN),
                array($cod,SQLSRV_PARAM_IN),
                array($exi,SQLSRV_PARAM_IN)
               );
$options=array("Scrollable" => SQLSRV_CURSOR_KEYSET);
$res=sqlsrv_query($conn, $sql, $params, $options);

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2011-03-13
    • 2021-04-02
    • 1970-01-01
    • 2021-06-29
    • 2021-08-30
    • 1970-01-01
    • 2021-05-06
    • 2017-11-24
    • 1970-01-01
    相关资源
    最近更新 更多