简体   繁体   中英

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

undefined index: startdate undefined index: enddate undefined index: 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]."&nbsp;<td>".$rows[1]."&nbsp;
   <td>".$rows[2]."&nbsp;<td>".$rows[3]."&nbsp;
    <td>".$rows[4]."&nbsp;<td>".$rows[5]."&nbsp;";
}
}

</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.

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]."&nbsp;<td align=right>".$rows[1]."&nbsp;
   <td align=right>".$rows[2]."&nbsp;<td align=right>".$rows[3]."&nbsp;
    <td align=right>".$rows[4]."&nbsp;<td align=right>".$rows[5]."&nbsp;";
}
}

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.

See also http://php.net/manual/en/language.types.array.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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