Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

undefined offset - ajax form PHP MySQL script

Status
Not open for further replies.

webdev007

Programmer
Sep 9, 2005
168
I am working on a dynamic quiz manager system
This is called by AJAX and build as many for rows as the php/ajax calls for

$counter =1;

while( $counter <= $num)
{
echo"<tr>
<td align='left' width='80%'>
Question: {$counter}<input type='text' name='quest[]' value='$quest' size='50'></td>
<td align='left' width='10%'>True: <input type='checkbox' name='a[]' value='y'></td>
<td align='left' width='10%'>False: <input type='checkbox' name='a[]' value='n'></td></tr>";
$counter++;
}

This is where the results go


$db = new MySQL_Db;
$db->connect($host, $un, $pw);
$db->query($db_db);

$subject=CleanDb($subject);
//$quest=CleanDb($quest);
//$answer=CleanDb($a);
$quiz_url=CleanDb($quiz_url);
/*
$result = $db->query("
INSERT INTO quiz
(subject, quest, answer, quiz_url)
VALUES('$subject', '".CleanDb($quest[$counter])."', '".CleanDb($a[$counter])."', '$quiz_url')
");
*/

$result = $db->query("
INSERT INTO quiz
(quest)
VALUES('".CleanDb($quest[$counter])."')
");

as you see I even shortened the INSERT for test purpose

if I do: print'<pre>'; print_r($quest); exit('</pre>');
the array resulting is what is expected

but the insert always result in:
Notice: Undefined offset: 1 in ... which is the insert value

what am I overlooking at?
thanks
regards

 
I made it, but I would like to know the "why" ??
to fix it I added:

$counter=0;
while($counter <count($quest) )
{
// my DB insert etc..

BUT in another script simply using $counter did the rick without starting a new one

(since a counter is already used above the query to evaluate how many items are in the array POST value)
I suppose my DB class (not used on the other script) does not allow to keep a previous counter activated.
or maybe I should have instantiated the DB class above the very first $counter; not sure...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top