1

php 7

I have a section in a php script which gathers info from a database. I am not the original author and am fairly new to php. The table in the database goes up to "doc16" however when testing this script I get an error for the following code because "Unidentified index: doc17".

Here is the section giving the error:

$sql="select * from users where username='$userid'";
$result=mysqli_query($info,$sql);
$line1=mysqli_fetch_array($result);
$docname="doc1";
$ptr=1;
while($permission=($line1["$docname"])){
   $inquirytable[$ptr]=$permission;
   $ptr++;
   $docname="doc".$ptr;

}

$info is the database connection info

In the database the doc#X for 1-16 is either a yes or no value I have tried playing with the values of $ptr to to force it to make $docname = doc16 or doc2 but no matter the value that just causes a Fatal : Allowed memory size error.

Any direction or assistance is appreciated. I will do my best to provide more information if necessary.

EDIT 1: using var_dump ($line1["$docname"]); before my while loop returns string 'Y' (length=1)

Newb 4 You BB
  • 1,205
  • 1
  • 11
  • 30
  • 2
    Please secure your requests: https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php/60496. – Blackhole May 22 '19 at 16:14
  • @Blackhole definitely planning to make the the site more secure, thank you – Newb 4 You BB May 22 '19 at 16:18
  • Check to make sure that `$line1[$docname]` exists before you try to assign the permission – aynber May 22 '19 at 16:19
  • @u_mulder the number of possible `doc.$ptr` is larger in some instances – Newb 4 You BB May 22 '19 at 16:22
  • @aynber a `var_dump()` of `$line1["$docname"]` before the while loop returns `string 'Y' (length=1)` so it seems to exist – Newb 4 You BB May 22 '19 at 16:25
  • 1
    It's not breaking before the while loop, it's breaking in the middle. You overwrite `$docname` at the end of the loop. You hit 16, and at the end of the loop, `$docname` becomes `doc17`. Then it goes back to the loop, and does `$permission = ($line1['doc17'])`. However, $line1 does not have the index `doc17`, so it breaks. – aynber May 22 '19 at 16:26
  • 1
    Instead of a while loop and renaming the columns, you might be better off doing `foreach($line1 as $docname => $permission)`. And then inside the loop, only do the assignment if `strpos($docname, 'doc') === 0` – aynber May 22 '19 at 16:28

0 Answers0