Jump to content

PHP Notice: Undefined offset ???


kool_samule

Recommended Posts

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:

<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:

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

But I had to remove the

:<?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:

PHP Notice: Undefined offset: 22 in C:\Inetpub\XxxxxXxxxx\NewFiles\Xxxxx\Xxxxx\scripts\script.php on line 64

Script.PHP:

$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!

Link to comment
Share on other sites

Undefined offset usually means about the same thing as undefined index.  And an index is a key in an array.

 

I noticed in your code you were setting $i to 1... and then adding 1 to it again before you ever even used it.  Which would make the last ... loop's $i value... undefined.

 

You also could have fixed the problem by putting the $i++ at the end of the loop.. and keeping $i = 1

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.