0

I have a minor problem where my code store the value and can run as usual, but when I want to display it again, the value won't display and the error is:

Notice: Undefined variable: result_c in C:\xampp\htdocs\eAttendance\*******.***php on line 91

Which is:

<td><?php echo $GLOBALS['result_c'] ?></td>

My question is:

  1. Why I cant call back my variable inside $result_c?
  2. What should I do? do you have any ideas?
  3. What is the appropriate method to make this coding successful?

I made this code because I wanted to calculate all the values in array stored inside $result_c to become average percentage of attendance.

<table >
<tr>
 <td>Matrix Card</td>
    <td>Percent</td>
 </tr>
 <?php
$sbj = $_GET['sbj'];
$cls = $_GET['cls'];
  $sql = mysql_query ("SELECT * FROM stu_course where class = '$cls' and subject_code = '$sbj'");
  $a = 0;

  while ($row = mysql_fetch_assoc($sql)){ 
       ?>
      <tr>
          <?php
    $result1[] = $row['login_id'];

    $sql1 = mysql_query("Select * from attendance where id_student = '$result1[$a]' and subject_code = '$sbj'") or die('Query failed. ' . mysql_error());
    $b = 0;
    while($row1 = mysql_fetch_assoc($sql1))
    {
        $popo = 1;
        $result_array[] = $row1['credit_hour'];
        $add = $result_array[$b];
        $result_c = $result_c + $add;
    }?>
      <td><?php echo $result1[$a];?></td>
     <td><?php echo $GLOBALS['result_c'] ?></td>
   <?php             
    $a++;      
  }
  ?>
  </tr>
</table>
2
  • 1
    There is so much wrong with that code, you are not initialising the $result_c variable, you are using $GLOBALS although $result_c is in your current instead of global scope, you are always setting the $popo variable to 1 without ever using it (or if you removed that portion of code then you could simply set the variable outside of the loop. To get back to your initial question, simply use $result_c rather than $GLOBALS['result_c']. Commented Mar 16, 2014 at 10:24
  • …and you really shouldn't put unescaped get parameters directly in you query Commented Mar 16, 2014 at 10:32

3 Answers 3

1

Instead of <?php echo $GLOBALS['result_c'] ?> why can't you just output <?php echo $result_c ?>? Or am I not understanding correctly.

Sign up to request clarification or add additional context in comments.

9 Comments

still it will not display the $result_c .. but when i do like sql query like this <br/> "$sql1 = mysql_query("Select * from attendance where id_student = '202133415' and subject_code = 'FP 901'") or die('Query failed. ' . mysql_error());" <br/> it will display the value store in $result_c. I just changed sql query. Maybe i can't do it straight, need filter to make it string?
Is $sql1 = mysql_query("Select * from attendance where id_student = '$result1[$a]' and subject_code = '$sbj'") or die('Query failed. ' . mysql_error()); not returning any rows? Try adding {} around your array variables in a string. I.E $sql1 = mysql_query("Select * from attendance where id_student = '{$result1[$a]}' and subject_code = '$sbj'") or die('Query failed. ' . mysql_error());
it will return a result, but the result will be zero / 0 from $result1 .... if I change in sql query statement " $sql1 = mysql_query("Select * from attendance where id_student = '1909dsa' and subject_code = '$sbj'") " .. It will display the value..
Did you wrap your array variable in {} take another look at my previous comment.
yes i did it with {} .. and the result still the same.. the display value is 0 .. why ? is it about mysql_real_escape_string
|
0

Also I would use prepared statements. Here is an example: http://forums.phpfreaks.com/topic/285929-pdo-in-a-php-class/#entry1467894

It seems you are injecting data straight from the client into your db call leaving you wide open to attack!

Comments

0

Try declaring $result_c outside the while loop, so you don't have to use $GLOBALS to cope with the variable scope issue you're having. Furthermore, the use of $GLOBALS isn't recommended.

Extra advise: I'd recommend working on your naming conventions so we can understand your code easier.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.