[英]php undefined index error
I have a stored procedure with three parameters @startdate, @enddate and @field1 and when I try to execute the following php code, I get error 我有一个带有三个参数@ startdate,@ enddate和@ field1的存储过程,当我尝试执行以下php代码时,出现错误
undefined index: startdate undefined index: enddate undefined index: field1 未定义的索引:开始日期未定义的索引:结束日期未定义的索引:field1
<?php
$myServer = "instance_name"; // host/instance_name
$myUser = "username"; // username
$myPass = "password"; // paasword
$myDB = "databasename"; // database name
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn’t connect to SQL Server on $myServer");
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn’t open database $myDB");
<?php
$startdate = isset($_REQUEST['startdate']) ? $_REQUEST['startdate'] : null;
$enddate = isset($_REQUEST['enddate']) ? $_REQUEST['enddate'] : null;
$field1 = isset($_REQUEST['field1']) ? $_REQUEST['field1'] : null;
?>
<form action = "thisfile.php" method = "post">
Please input startdate <input type = "text" name = "startdate" /> </br>
Please input enddate <input type = "text" name = "enddate" /> </br>
Please input field1 name <input type = "text" name = "field1" /> </br>
Please submit <input type = "submit" name = "submit" value = "submit" /> </br>
</form>
<table border = "5" id="1">
<?php
if (isset($startdate) && isset($enddate) ){
$query = "storedprocedure '$startdate','$enddate','field1'";
$result = mssql_query($query);
while ($rows = mssql_fetch_array($result) ) {
echo "<tr><td>".$rows[0]." <td>".$rows[1]."
<td>".$rows[2]." <td>".$rows[3]."
<td>".$rows[4]." <td>".$rows[5]." ";
}
}
</table>
</body>
</html>
I was just looking for three text boxes on the top and when I enter startdate, enddate and field1 names in the text boxes, it should display the output in the same page. 我只是在顶部寻找三个文本框,当我在文本框中输入startdate,enddate和field1名称时,它应该在同一页面中显示输出。
Edit: My stored procedure is something similar to below : 编辑:我的存储过程类似于以下内容:
Create procedure storedprocedure
@startdate datetime,
@enddate datetime,
@field1 varchar(50)
select date, sum(qty), sum(qty), sum(qty2), sum(qty3), sum(qty4) from table1
where date between @startdate and @enddate and field1 like @field1 + '%'
group by date
why don't you do try this 你为什么不试试这个
if (isset($startdate) && isset($enddate) ){
$query = "storedprocedure '$startdate','$enddate','field1'";
$result = mssql_query($query);
while ($rows = mssql_fetch_array($result) ) {
echo "<tr><td>".$rows[0]." <td align=right>".$rows[1]."
<td align=right>".$rows[2]." <td align=right>".$rows[3]."
<td align=right>".$rows[4]." <td align=right>".$rows[5]." ";
}
}
This should stop the notice 这应该停止通知
You can rewrite part of your code in such way: 您可以通过以下方式重写部分代码:
<?php
$startdate = isset($_REQUEST['startdate']) ? $_REQUEST['startdate'] : null;
$enddate = isset($_REQUEST['enddate']) ? $_REQUEST['enddate'] : null;
$field1 = isset($_REQUEST['field1']) ? $_REQUEST['field1'] : null;
?>
But actually you need to check whether all your request parameters (especially enddate
and field1
) are present. 但是实际上您需要检查是否所有请求参数(尤其是
enddate
和field1
)都存在。
See also http://php.net/manual/en/language.types.array.php 另请参见http://php.net/manual/zh/language.types.array.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.