![](/img/trans.png)
[英]Running a MS SQL server stored procedure with a mac in php using PDO
[英]Running SQL Stored Procedure in PHP
我對PHP不熟悉,但是試圖弄清楚為什么它不起作用。 我試圖運行一個通過php接收一些參數的SQL存儲過程。 該代碼應根據條件在表單中為用戶數據輸入添加額外的一行。 當我直接在SQL Server Management Studio中運行存儲過程時,它可以很好地工作並將行添加到后端表中並以前端形式反映行,但是當嘗試在行中使用基礎代碼添加行時,我沒有錯誤,也沒有更新。
我對PHP不熟悉,但是正在嘗試學習並弄清楚為什么它不起作用。 任何幫助是極大的贊賞。
這是基礎的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">
</td>
<td colspan="5" rowspan="2" valign="middle" nowrap>
</td>
<td colspan="13" align="right" valign="bottom" nowrap>
</td>
<td align="right" valign="bottom" nowrap>
</td>
<td align="right" valign="bottom" nowrap>
</td>
<td align="right" valign="bottom" nowrap>
<!--input type="Button" value="< Back to Properties" onClick="document.location='PropertyList.asp'"-->
</td>
<td valign="bottom">
</td>
</tr>
<tr class="Title1">
<td valign="bottom">
</td>
<td colspan="5" rowspan="2" valign="middle" nowrap>
</td>
<td colspan="13" align="left" valign="bottom" nowrap>
Saving data...<br>
Please be patient
</td>
<td align="right" valign="bottom" nowrap>
<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>
</td>
<td align="right" valign="bottom" nowrap>
</td>
<td align="right" valign="bottom" nowrap>
</td>
<td valign="bottom">
</td>
</tr>
</table>
</td></tr></table>
</body>
</html>
這是存儲過程:
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
我對PHP不太了解,但是您可以大大簡化該存儲過程。 不需要循環之類的東西。 這應該做同樣的事情。
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
順便說一句...不太確定為什么將其標記為sql-server。 好像程序本身很好。 顯然,問題出在您的php中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.