views:

5627

answers:

3

Hello, I have a form that users fill out, 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.

+4  A: 

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.

cletus
yea, I've seen this comment before. Unfortunately, I'm not understanding how its not an array? I'm outputting the data into FirePHP and I see it via the POST and then in the variables as well.
Tomaszewski
ok thanks, I only posted a portion of the code to make it easy to read. Thanks again!
Tomaszewski
+6  A: 

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?

zombat
man, i just want to kick myself in the head. OF COURSE! There is only 1 unique $empId. I was focused on the $myQuery array that I didn't pay attention to the others. Thanks so much! I changed $empID[$i] to $empId and the Notice disappeared as $empId is NOT an array.
Tomaszewski
I think part of the confusion here is that when indexing `$var[$i]` it could be either a string or an array...
Talvi Watia
A: 

FIRST : I DON'T SPEAK ENGLISH VERY WELL ^_^. SECOND :

Try to test and initialised your arrays before used them :

if( !isset($catagory[$i]) ) $catagory[$i] = '' ;
if( !isset($task[$i]) ) $task[$i] = '' ;
if( !isset($fullText[$i]) ) $fullText[$i] = '' ;
if( !isset($dueDate[$i]) ) $dueDate[$i] = '' ;
if( !isset($empId[$i]) ) $empId[$i] = '' ;

WHY ??? :

If $catagory[$i] doesn't exist, you create (Uninitialized) one ... that's all ; => PHP try to read on your table in the adress $i, but at this adress, there's nothing, this adress doesn't exist => PHP retourn you a notice, and it put nothing to you string. So you code is not very clean, it takes you some resources that down you server's performance (just a very little).

TAKE CARE ABOUT YOUR MySQL TABLES DEFAULT VALUE !! ^_^

if( !isset($dueDate[$i]) ) $dueDate[$i] = '0000-00-00 00:00:00' ;

or

if( !isset($dueDate[$i]) ) $dueDate[$i] = 'NULL' ;

Good work every body ^_^

Kiki974
right, but what if its supposed to be an array, `if(!isset($myarray[$i])) $myarray[$i]=array();` perhaps?
Talvi Watia