如何验证该值是否已经存在。的PHP

时间:2018-11-13 02:40:03

标签: php sql-server forms stored-procedures

MSSQLServer中的存储过程是否遇到问题,这是验证已存在的值的正确语法吗?

ALTER PROCEDURE [dbo].[sp_SRP_DuplicateData]
-- Add the parameters for the stored procedure here
@BankName nvarchar(50),
@BankCode nvarchar(5)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here
SELECT BankName, BankCode FROM ZREF_ROUT_INCO_DOC where BankName=@BankName 
and BankCode=@BankCode

END

这是我的php代码

<?php
    if(isset($_POST['SUBMIT']))
    {
       $query = sqlsrv_query($conn, "EXEC sp_SRP_DuplicateData(?,?)");

    if(sqlsrv_num_rows($query)>0)
    {
      echo "Bank Name or Bank Code already exist";
    }

  }

  ?>

这是表格

    <label class="form-top">Add Bank</label>
    <div class="form-group">
    <form action="test.php" method="POST">
    <div class="modal-body" style="text-align: left;">  

            <b>Bank Name:</b><input type="text" name="BankName" value="" 
id="" class="" /><br>
            <b>Bank Abbreviation: </b><input type="text" name="BankCode" 
value="" id="" class="" /> <br>
            <input type="SUBMIT" name="SUBMIT" value="Proceed" id="Bank" 
class="btn btn-lg btn-default" />

1 个答案:

答案 0 :(得分:0)

为了将您的值传递给存储过程,您需要向sqlsrv_query函数提供值的数组...

$query = sqlsrv_query($conn, "EXEC sp_SRP_DuplicateData(?,?)", array( $_POST['BankName'], $_POST['BankCode'] ));

您发布的存储过程对我来说看起来不错,但是您设置了SET NOCOUNT ON,我相信它不会返回sqlsrv_num_rows函数所需的行数。您应该使用sqlsrv_has_rows函数来测试行...

if(sqlsrv_has_rows($query))
{
  echo "Bank Name or Bank Code already exist";
}