Page 1 of 1

error Undefined offset 2 - help please

Posted: Thu Dec 09, 2004 9:13 am
by rp
Hi folks

Can anybody help with this script, in short the "top" form adds a budget to my DB for a business location (this works OK)

What Happens next is the user should then allocate that budget to actual departments within the business loaction. But before "posting" this second bit to the DB, FIRST the script should check that the total allocated does not exceed the actual budget.

I now get an error Undefined offset 2 when I run the script, any suggestions please.

Code: Select all

<?php
echo "<form name='selectsite' method='post' action='budgeting.php'>";
echo "<div id='col25'>";
echo "Select a site to set a budget";
echo "</div>";
echo "<select name='siteID'>";
$sqlsite = "select * from employersites";
$rssite = mysql_query($sqlsite);
while ($row = mysql_fetch_array($rssite, MYSQL_ASSOC))
{ 
echo "<option value='".$row['EmployerSitesID']."'>".$row['SiteName']."</option>";
}
echo "</select>";
echo "<br>";
echo "<div id='col25'><label for='bud' accesskey='k'>Budget</label></div>";
echo "<input  type='text' name='budget' size='12' tabindex='1' id='bud' value='Budget'>";
echo "<br>";
echo "<div id='col25'><label for='fr' accesskey='m'>From</label></div>";
echo "<input  type='text' name='from' value='yyyy-mm-dd' size='10' tabindex='3' id='fr'>";
echo "<br>";
echo "<div id='col25'><label for='to' accesskey='n'>To</label></div>";
echo "<input  type='text' name='to' value='yyyy-mm-dd' size='10' tabindex='4' id='to'>";
echo "<br>";
echo "<p>";
echo "<input name='sitebudget' id='menubutton' type='submit' value='Allocate'>";
echo "</form>";

if(isset($_POST['sitebudget']))
{
$siteID = $_POST['siteID'];
$budget = $_POST['budget'];
$from = $_POST['from'];
$to = $_POST['to'];

$sqlInsert = "insert into budgets(Budget,LocationID,yearstart,yearend) values ('$budget','$siteID','$from','$to')";
$rsInsert = mysql_query($sqlInsert); 
if($rsInsert) 
{
echo "<div id='title'>Allocate to departments</div>";
echo "<form name='departmentbudget' method='post' action='budgeting.php'>";
$sqldept = "SELECT * FROM employerdepartments WHERE employerdepartments.EmployerSiteID='$siteID'";
$rsdept = mysql_query($sqldept);
while ($row = mysql_fetch_array($rsdept, MYSQL_ASSOC))
{ 
echo "<div id='col25'>";
echo "<input name='dep' type='hidden' value='".$row['EmployerDepartmentID']."'>";
echo $row['Department'];
echo "</div>";
echo "<input name='deptbudget[]' type='text' value='Budget' size='10'>";
echo "<br>";
}
echo "<p>";
echo "<input name='budget' type='text' value='$budget'>";
echo "<br>";
echo "<input name='to' type='text' value='$to'>";
echo "<br>";
echo "<input name='from' type='text' value='$from'>";
echo "<br>";
echo "<input name='allocate' id='menubutton' type='submit' value='Set budgets'>";
echo "</form>";
}
else
{
}
}
else
{
}
if(isset($_POST['allocate']))
{
$budget = $_POST['budget'];
$deptbudget = $_POST['deptbudget'];
for ($i=0; $i <count($deptbudget); $i++) 
$deptbudget[$i];
$total = "".array_sum($deptbudget)."\n";
if($budget<$total)
{
$from = $_POST['from'];
$to = $_POST['to'];
$depID = $_POST['dep'];
$sqlInsert = "insert into budgets(Budget,DepartmentID,yearstart,yearend) values ('$deptbudget[$i]','$depID','$from','$to')";
$rsInsert = mysql_query($sqlInsert); 
if($rsInsert) 
{
echo "Budgets sucsessfully allocated";
}
else
{
}
?>
Help very much appreciated

Undefined offset

Posted: Thu Dec 09, 2004 10:13 am
by AVATAr
Undefined offset happens when you are referencing a key of an array, and it doesent exists..

example:

Code: Select all

<?php
$arr = array('a'=>1,'b'=>2,'c'=>3);

echo $arr['f'];
?>
this will give you that error. you can suppress is using @

Code: Select all

<?php
$arr = array('a'=>1,'b'=>2,'c'=>3);

echo @$arr['f'];
?>

Posted: Thu Dec 09, 2004 10:33 am
by timvw
imho, @ is a bad thing ;)

[php_man]isset[/php_man] or [php_man]array_key_exists[/php_man] can be used to test if the key exists... and based on the result of this test, you can decide to use the key.