Skip to main content
January 12, 2010
Answered

PHP Notice: Undefined offset ???

Hi Chaps,

I'm having some trouble with a PHP Form page and an Update Script page.

I've tested this on my Apache server and it works fine, but fails on my 'Live' IIS server:
Form Code:

PHP Code:

<input type='text' name='jobpriority[]' value="<?php echo $row_rsWorkload_All['jobpriority'];?>"/>

<?php echo $row['jobcount']; ?>
<?php
echo $row_rsWorkload_All['jobpriority'];?>
<?php
    $table_name
= $row_rsWorkload_All['fromtable'];
       
$item_id = $row_rsWorkload_All['jobid'];
?>

<input type="hidden" name="setpriority[]" value="<?php echo $table_name; ?>:<?php echo $item_id; ?>" />

The 'hidden' input did look like this:


PHP Code:

<input type="hidden" name="setpriority[]" value="<?php echo $table_name; ?>:<?php echo $item_id; ?>:<?php $_POST ['jobpriority']; ?>" />
But I had to remove the
Code:
:<?php $_POST ['jobpriority']
bit to get the page to open on the IIS server.
Since removing the code, the page opens but when I 'click send' to pass the data to the script page, I get this error:
Quote:
PHP Notice: Undefined offset: 22 in C:\Inetpub\XxxxxXxxxx\NewFiles\Xxxxx\Xxxxx\scripts\script.php on line 64
Script.PHP:
Code:
$allowed_tables = Array('tbl_table1','tbl_table2','tbl_table3'); // to prevent SQL injection
$i = 1;
foreach($_POST['setpriority'] as $var) {
    $arr = explode(':', $var);
    if(in_array($arr[0], $allowed_tables)) {
        $table = $arr[0];
        $rowid = $arr[1];
        $priority = $_POST['jobpriority'][$i];
          $i++;
        if(is_numeric($rowid)) {
            // run your SQL query here to update $table where row matches $rowid
            $query = sprintf("
               UPDATE $table
               SET jobpriority='$priority'
               WHERE jobid=$rowid");
            $result = mysql_query($query, $conndb2) or die(mysql_error());
               $mess = $ref = $_SERVER['HTTP_REFERER']; header( 'refresh: 0; url='.$ref);
        }
    else {
  $mess = "<p>There was a problem</p>";
}
     }
}
I'm a bit stuck as I have around 10 pages that use the same sort of functionality, so if anyone can help with this, I'd be most grateful!
This topic is closed to new replies. Start a new post to keep the conversation going.
David_Powers
David_PowersCorrect answer
Inspiring
January 12, 2010

It would help if you indicated what was on line 64, rather than expecting others to look for a needle in a haystack.

However, I suspect the problem is on this line:

$priority = $_POST['jobpriority'][$i];

You initialize $i as 1, but PHP arrays begin at 0. There are probably 22 items in the $_POST['jobpriority'] array, but the 22nd item is not offset 22, but offset 21.

By the way, the code in your original hidden field was meaningless. To insert $_POST['jobpriority'] into the value attribute, you need to use echo. It should be:

<?php echo $_POST['jobpriority']; ?>

2 replies

David_Powers
David_PowersCorrect answer
Inspiring
January 12, 2010

It would help if you indicated what was on line 64, rather than expecting others to look for a needle in a haystack.

However, I suspect the problem is on this line:

$priority = $_POST['jobpriority'][$i];

You initialize $i as 1, but PHP arrays begin at 0. There are probably 22 items in the $_POST['jobpriority'] array, but the 22nd item is not offset 22, but offset 21.

By the way, the code in your original hidden field was meaningless. To insert $_POST['jobpriority'] into the value attribute, you need to use echo. It should be:

<?php echo $_POST['jobpriority']; ?>

Translate
January 12, 2010

Hi David, yes you are indeed correct, should have indicated line 64 earlier. It seems

like you are correct in your advice. I will check the remaining files and re-post if required.

Thanks for your help, and I'll remember to point out the necessary info from the outset next time.

Cheers

Translate