Click here to Skip to main content
15,890,932 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, I am newbies in PHP and Bootstrap. Currently, I am developing an online examination system. I would try to store the score of students in database but it's a failure. The score was being declared in variable named $per. Could you help me to figure out the coding for doing it ?

What I have tried:

I've try to code. Here are the sample coding.

users.php
PHP
<?php
session_start();
class users{

public $per;

public function __construct()
	{
		$this->conn=new mysqli($this->host,$this->username,$this->pass,$this->db_name);
		if($this->conn->connect_errno)
		{
			die ("database connection failed".$this->conn->connect_errno);
		}
	}

public function store_result($per)
	{
		$this->conn->query($per);
		return true;
	}
?>

store.php

<?php 
include("class/users.php");
$score=new users('per');
$query = "SELECT * FROM table PERCENTAGE $per";

if ($score->store_result($query))
{
	echo $per;
}

?>

And here is the piece of code in answer.php where percentage of score was being declared here. 

        Your result        <?php 
		$per=($answer['right']*100)/($total_qus);
		
		echo $per."%";
		?>      
Posted
Updated 5-Dec-17 7:54am

1 solution

"SELECT * FROM table PERCENTAGE $per"

Since you stipulate that $per is a variable name I can safely say that your SQL query is failing because it's invalid.

Perhaps you mean something like:
SELECT * FROM table PERCENTAGE WHERE per=$per

Not enough in your code sample to be sure and assuming per is an int

Resolve that and let's go on from there, if necessary.
 
Share this answer
 
Comments
Azifatilah Ramli 5-Dec-17 22:12pm    
Thank you sir Balboos, however, how can I solve this error ?
Undefined variable: per
I am screwed up.
W Balboos, GHB 6-Dec-17 6:36am    
If you are within a class and use a member variable in the class ($per), you must tell the system where that variable is located:
If withing the class methods, you need to use: $this->per
If in an outside instance of the class ($score): $score->per

This way you can tell (if more than one class contains $per) which one you are referring to. In your case, there is no $per that is not either within your class or part of an instance of the class.

Here's an example of a class, instance, and it's useage

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900