简体   繁体   English

在PHP中运行SQL存储过程

[英]Running SQL Stored Procedure in PHP

I am not familiar with PHP but am trying to figure out why this isn't working. 我对PHP不熟悉,但是试图弄清楚为什么它不起作用。 I trying to run an SQL stored procedure which takes in some parameters via php. 我试图运行一个通过php接收一些参数的SQL存储过程。 This code is supposed to add an extra line in the form for user data entry based on the criteria. 该代码应根据条件在表单中为用户数据输入添加额外的一行。 When I run the stored procedure directly in SQL Server Management Studio it works fine and adds the row to the back end table and reflects the row in the front-end form, but when try to add the row in front-end with the underlying code, I get no error and nothing updates. 当我直接在SQL Server Management Studio中运行存储过程时,它可以很好地工作并将行添加到后端表中并以前端形式反映行,但是当尝试在行中使用基础代码添加行时,我没有错误,也没有更新。

I am not familiar with PHP but am trying to learn and figure out why this isn't working. 我对PHP不熟悉,但是正在尝试学习并弄清楚为什么它不起作用。 Any help is greatly appreciated. 任何帮助是极大的赞赏。

Here is the underlying PHP code. 这是基础的PHP代码。

<?PHP include 'Connections/PromAPPS01Connection.php'; ?>
<?php
session_start();
$sUserid = "9999";
if(isset($_SESSION['sUserID'])){
    $sUserid = $_SESSION['sUserID'];
    //echo "Yes";
}
else{
    /* Redirect browser */
    header("Location: LoginSession.php");
    /* Make sure that code below does not get executed when we redirect. */
    exit;
    //echo "No";
}

$sPostPropertyId = $_POST["Props"];
$sPostGroupId = $_POST["AcctGroup"];
$sRecordStart = $_POST["RS"];
$sPostAcct = $_POST["Acct"];
$sPostYear = $_POST["Year"];
$sBudgetID = $_POST["BudgetID"];
$sPostRowCount = $_POST["AcctCount"];
$sSubmitType = $_POST["SubmitType"];
$sView = $_POST["View"];
$sAddLineAcct = $_POST["NewLineAcct"];
$sAddLineProp = $_POST["NewLineProp"];
$sAddLineYear = $_POST["NewLineYear"];
$sPropertyNumber = $_POST["PropertyNumber"];
$sType = $_POST["Type"];
$sDept = $_POST["Dept"];
$sComments = $_POST["Comments"];
$sAddType = $_POST["AddType"];

$sqlNewLine = "Exec Budgeting.procAddNewLines '". $sPostPropertyId ."', '". $sPropertyNumber ."', '". $sAddLineAcct ."', '". $sAddType ."','". $sType ."', '". $sAddLineYear ."', '". $sPostYear ."', '". $sComments ."', '". $sUserid ."','". $sDept ."' ";
//echo "<br>";
//echo $sAddType ." & ". $sAddLineYear . " &". $sPostYear;

if ($sRecordStart == ""){ 
    $sRecordStart = 0;
}

$iNextRS = $sRecordStart;

$navigateLocation = "PlanSheetProperty.php";

//echo "<br>";
//  echo $sqlNewLine;
    // Executing the sql update code
        //conn.Execute sql_New_Line
    //#####################################################################
    $stmtNewLine = sqlsrv_prepare($dbLink, $sqlNewLine);

    sqlsrv_execute($stmtNewLine);
            //#################################

?> 
<html>
<head>
<Link Rel="Stylesheet" href="RenewStyle.css" type="text/css">
    <title>The Budget Machine</title>
</head>
<!--body-->
<body onLoad="document.AcctUpdate.submit();">
<?PHP include 'Top.php'; ?>

<table width="100%" bordercolor="#003366" cellpadding="0" cellspacing="0"><tr><td>
<table width="100%" cellpadding="3" cellspacing="0" border="0">
    <tr class="Title1">
        <td valign="bottom">&nbsp;

        </td>
        <td colspan="5" rowspan="2" valign="middle" nowrap>&nbsp;

        </td>
        <td colspan="13" align="right" valign="bottom" nowrap>&nbsp;

        </td>
        <td align="right" valign="bottom" nowrap>&nbsp;

        </td>
        <td align="right" valign="bottom" nowrap>&nbsp;

        </td>
        <td align="right" valign="bottom" nowrap>
        <!--input type="Button" value="< Back to Properties" onClick="document.location='PropertyList.asp'"-->
        </td>
        <td valign="bottom">&nbsp;

        </td>
    </tr>
    <tr class="Title1">
        <td valign="bottom">&nbsp;

        </td>
        <td colspan="5" rowspan="2" valign="middle" nowrap>&nbsp;

        </td>
        <td colspan="13" align="left" valign="bottom" nowrap>
        Saving data...<br>
        Please be patient
        </td>
        <td align="right" valign="bottom" nowrap>&nbsp;

        <form method="post" action="<?php echo $navigateLocation ?>" name="AcctUpdate" id="AcctUpdate" />        
        <input type="hidden" name="RS" id="RS" value="<?PHP echo $iNextRS ?>">
        <input type="hidden" name="Props" id="Props" value="<?PHP echo $sPostPropertyId ?>">
        <input type="hidden" name="AcctGroup" id="AcctGroup" value="<?PHP echo $sPostGroupId ?>">
        <input type="hidden" name="GLAcct" id="GLAcct" value="<?php echo $sAddLineAcct ?>">
        <input type="hidden" name="AcctCount" id="AcctCount" value="<?PHP echo $sPostRowCount ?>">
        <input type="hidden" name="BudgetID" id="BudgetID" value="<?PHP echo $sBudgetID ?>">
        <input type="hidden" name="NewLineAcct" id="NewLineAcct" value="<?php echo $sPostAddLineAcct ?>">        
        <input type="hidden" name="PropertyNumber" id="PropertyNumber" value="<?php echo $sPostPropertyNumber ?>">
        <input type="hidden" name="SubmitType" id="SubmitType" value="<?php echo $sSubmitType ?>">
        <input type="hidden" name="View" id="View" value="<?php echo $sView ?>">
        <input type="hidden" name="NewLineProp" id="NewLineProp" value="<?php echo $sPostAddLineProp ?>">
        </form>
        </td>
        <td align="right" valign="bottom" nowrap>&nbsp;

        </td>
        <td align="right" valign="bottom" nowrap>&nbsp;

        </td>
        <td align="right" valign="bottom" nowrap>
        </td>
        <td valign="bottom">&nbsp;

        </td>
    </tr>
     </table>
</td></tr></table>

</body>
</html>

Here is the stored procedure: 这是存储过程:

 SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure AddNewLines
(
    @PropertyID numeric (10,0) = NULL,
    @PropertyNumber varchar (30) = NULL,
    @GLAccountNumber varchar (10) = NULL,
    @InsertType varchar (10) = NULL,
    @LineAssoc varchar (10) = NULL, 
    @ClickYear varchar (30) = NULL,
    @Year varchar (30) = NULL,
    @Comments varchar (800) = NULL,
    @User int = NULL,
    @DeptCode varchar (10) = NULL
) AS




IF @InsertType = 'Single'
    BEGIN
    IF @LineAssoc = 'Project'
        BEGIN
        INSERT INTO BudDat(PropertyID, 
            PropertyNumber, 
            GLAccountNumber, 
            Project, 
            [Year], 
            BudgetID, 
            Comments, 
            LastChangedDate, 
            LastChangedBy) 
            VALUES (@PropertyID, 
            @PropertyNumber, 
            @GLAccountNumber, 
            'Y', 
            @ClickYear, 
            '', 
            @Comments, 
            getDate(), 
            @User)
        END



    IF @LineAssoc = 'None'
        BEGIN
        INSERT INTO BudDat(PropertyID, 
            PropertyNumber, 
            GLAccountNumber, 
            DeptCode, 
            [Year], 
            BudgetID, 
            Comments, 
            LastChangedDate, 
            LastChangedBy) 
            VALUES (@PropertyID, 
            @PropertyNumber, 
            @GLAccountNumber, 
            @DeptCode,
            @ClickYear, 
            '', 
            @Comments, 
            getDate(), 
            @User)
        END
    END

IF @InsertType = 'Multiple'
    BEGIN
    DECLARE @YearCount int
    Select @YearCount = 0
    IF @LineAssoc = 'Project'
        BEGIN
        WHILE (@YearCount < 5)
            BEGIN
            INSERT INTO BudDat(PropertyID, 
                PropertyNumber, 
                GLAccountNumber, 
                Project, 
                [Year], 
                BudgetID, 
                Comments, 
                LastChangedDate, 
                LastChangedBy) 
                VALUES (@PropertyID, 
                @PropertyNumber, 
                @GLAccountNumber, 
                'Y', 
                @Year, 
                '', 
                @Comments, 
                getDate(), 
                @User)
            Select @YearCount = @YearCount+1
            Select @Year = @Year+1
            END
        END



    IF @LineAssoc = 'None'
        BEGIN
        WHILE (@YearCount < 5)
            BEGIN
            INSERT INTO BudDat(PropertyID, 
                PropertyNumber, 
                GLAccountNumber, 
                DeptCode, 
                [Year], 
                BudgetID, 
                Comments, 
                LastChangedDate, 
                LastChangedBy) 
                VALUES (@PropertyID, 
                @PropertyNumber, 
                @GLAccountNumber, 
                @DeptCode,
                @Year, 
                '', 
                @Comments, 
                getDate(), 
                @User)
            Select @YearCount = @YearCount+1
            Select @Year = @Year+1
            END
        END
    END

I don't know much about PHP but you could greatly simplify that stored proc. 我对PHP不太了解,但是您可以大大简化该存储过程。 No need for loops and that sort of thing for this. 不需要循环之类的东西。 This should do the same thing. 这应该做同样的事情。

ALTER Procedure AddNewLines
(
    @PropertyID numeric (10,0) = NULL,
    @PropertyNumber varchar (30) = NULL,
    @GLAccountNumber varchar (10) = NULL,
    @InsertType varchar (10) = NULL,
    @LineAssoc varchar (10) = NULL, 
    @ClickYear varchar (30) = NULL,
    @Year varchar (30) = NULL,
    @Comments varchar (800) = NULL,
    @User int = NULL,
    @DeptCode varchar (10) = NULL
) AS

    IF @LineAssoc = 'Project'
        INSERT INTO BudDat(PropertyID, 
            PropertyNumber, 
            GLAccountNumber, 
            Project, 
            [Year], 
            BudgetID, 
            Comments, 
            LastChangedDate, 
            LastChangedBy) 
            select @PropertyID, 
            @PropertyNumber, 
            @GLAccountNumber, 
            'Y', 
            @ClickYear, 
            '', 
            @Comments, 
            getDate(), 
            @User
            from (VALUES (1),(2),(3),(4),(5)) as Numbers(x)
            where x <= case when @InsertType = 'Single' then 1 else 5 end


    IF @LineAssoc = 'None'
        INSERT INTO BudDat(PropertyID, 
            PropertyNumber, 
            GLAccountNumber, 
            DeptCode, 
            [Year], 
            BudgetID, 
            Comments, 
            LastChangedDate, 
            LastChangedBy) 
            SELECT @PropertyID, 
            @PropertyNumber, 
            @GLAccountNumber, 
            @DeptCode,
            @ClickYear, 
            '', 
            @Comments, 
            getDate(), 
            @User
            from (VALUES (1),(2),(3),(4),(5)) as Numbers(x)
            where x <= case when @InsertType = 'Multiple' then 1 else 5 end

btw...not really sure why this is tagged as sql-server. 顺便说一句...不太确定为什么将其标记为sql-server。 It seems as though the procedure itself is fine. 好像程序本身很好。 Obviously the issue is somewhere in your php. 显然,问题出在您的php中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM