What causes: "Notice: Uninitialized string offset" to appear?


Question

I have a form that users fill out, and on the form there are multiple identical fields, like "project name", "project date", "catagory", etc. Based on how many forms a user is submitting, my goal is to:

  1. loop over the number of forms
  2. create individual SQL insert statements

However, PHP throws me a NOTICE that I don't seem to understand:

Notice:

Notice: Uninitialized string offset: 1 ...dataPasser.php on line 90

PHP

$myQuery = array();

if ($varsCount != 0)
{
  for ($i=0; $i <= $varsCount; $i++)
  {
    $var = "insert into projectData values ('" . $catagory[$i] . "', '" .  $task[$i] . "', '" . $fullText[$i] . "', '" . $dueDate[$i] . "', null, '" . $empId[$i] ."')";
    array_push($myQuery, $var);     
  }
}

There are references to this issue I am having, but they are not exact and I am having trouble deducing where the actual problem stems from. I would greatly appreciate any help in understanding what is causing the array to not initialize properly.

1
57
6/26/2018 6:55:21 PM

Accepted Answer

This error would occur if any of the following variables were actually strings or null instead of arrays, in which case accessing them with an array syntax $var[$i] would be like trying to access a specific character in a string:

$catagory
$task
$fullText
$dueDate
$empId

In short, everything in your insert query.

Perhaps the $catagory variable is misspelled?

57
8/12/2009 12:19:04 AM

It means one of your arrays isn't actually an array.

By the way, your if check is unnecessary. If $varsCount is 0 the for loop won't execute anyway.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon