Click here to Skip to main content
15,916,180 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
view_student.php

PHP
<?php
require '../mvc/student.php';
$obj = new Student();

$result = $obj->select_all_student();


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Add student info

    	
        	<table><tbody><tr>        		<td>                	<a href="add_student.php">Add student</a> |
       		 		<a href="view_student.php">View student</a>
                    <h3>All student</h3>
                </td>        	</tr>            </tbody></table>
        
        	            <?php
			while($row = $result->fetch_assoc()) {
			?>
        	            <?php
			}
			?>
		<table border="1"><tbody><tr>        		<th>Id</th>	            <th>Student name</th>    	        <th>Email address</th>        	    <th>Mobile number</th>            	<th>Action</th>        	</tr><tr>        		<td><?php echo $row['student_id'] ?></td>	            <td><?php echo $row['student_name'] ?></td>    	        <td><?php echo $row['email_address'] ?></td>        	    <td><?php echo $row['mobile_number'] ?></td>            	<td>                <a href="edit.php?id=<?php echo $row['student_id'] ?>">Edit</a> |
                <a href="delete.php?id=<?php echo $row['student_id'] ?>">Delete</a>
                </td>        	</tr></tbody></table>

student.php
PHP
<?php

class Student {
	public function __construct() {
		$hostname = 'localhost';
		$username = 'root';
		$password = '';
		$database = 'db_ci_batch21';
		$this->conn = new mysqli($hostname, $username, $password, $database);
		if(!$this->conn) {
			die("not connected");
		} 
	}
	
	public function save_student($data) {
		$sql = "INSERT INTO tbl_student(student_name, email_address, mobile_number)VALUES('$data[student_name]', '$data[email_address]', '$data[mobile_number]')";
		if($this->conn->query($sql)) {
			$message = "Save data successfully";
			return $message;
		} else {
			$message = "Data not saved";
			return $message;
			exit;
		}
	}
	
	public function select_all_student() {
		$sql = "SELECT * FROM tbl_student";
		$result = $this->conn->query($sql); 
		//echo '<pre>';
		//print_r($result);
		//exit;
		return $result;
	}
	
	public function delete_student($student_id) {
		$sql = "DELETE FROM tbl_student WHERE student_id = '$student_id'";
		$this->conn->query($sql);
		header('Location: view_student.php');
	}
	
	public function select_student_info_by_id($student_id) {
		 $sql = "SELECT * FROM tbl_student WHERE student_id = '$student_id' ";
		 $result = $this->conn->query($sql);
		 return $result;
	}
	
	public function update_student($data) {
		$sql = "UPDATE tbl_student SET student_name='$data[student_name]', email_address='$data[email_address]', mobile_number='$data[mobile_number]' WHERE student_id='$data[student_id]' ";
		$this->conn->query($sql);
		header("Location:view_student.php");
	}
}

?>

edit.php
PHP
<?php
require '../mvc/student.php';
$obj = new Student();

$student_id = $_GET['id'];
$result = $obj->select_student_info_by_id($student_id);
$student_info = $result->fetch_assoc();
/*echo '<pre>';
print_r($student_info);
exit;*/

if(isset($_POST['btn'])) {
	$obj->update_student($_POST);
}

?>
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;


&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;Edit student info



  
	
    	
        <table><tbody><tr>        <td>        <a href="add_student.php">Add student</a> |
        <a href="view_student.php">View student</a>
        <h3>Edit student</h3>
        </td>        </tr>        	<tr>            	<td>Student name</td>               	<td>                	
                    
                 </td>            </tr>             <tr>            	<td>Email address</td>               	<td>                    
                </td>            </tr>            <tr>            	<td>Mobile number</td>               	<td>                    
                </td>            </tr>            <tr>            	<td></td>               	<td>                    
                </td>            </tr>        </tbody></table>

add_student.php
PHP
<?php
require '../mvc/student.php';
$obj = new Student();
if(isset($_POST['btn'])) {
	$message = $obj->save_student($_POST);
}


?>

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;


&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
&lt;title&gt;Add student info



	<h3>Add student</h3>
   &lt;!-- <h4 style="color: green">
    &lt;?php /*
		if(isset($message)) {
			echo $message;
			unset($message);
		}
	*/?&gt;
    </h4>	--&gt;	
	
    	
        <table><tbody><tr>        <td>        <a href="add_student.php">Add student</a> |
        <a href="view_student.php">View student</a>
        </td>        </tr>        	<tr>            	<td>Student name</td>               	<td>                	
                 </td>            </tr>             <tr>            	<td>Email address</td>               	<td>                    
                </td>            </tr>            <tr>            	<td>Mobile number</td>               	<td>                    
                </td>            </tr>            <tr>            	<td></td>               	<td>                    
                </td>            </tr>        </tbody></table>

delete.php
PHP
<?php

require "../mvc/student.php";
$obj = new Student();

$student_id = $_GET['id'];
//echo $student_id;

$obj->delete_student($student_id);

?>



when i run edit.php
its shows error:
Notice: Undefined index: id in C:\xampp\htdocs\crud_php\mvc\edit.php on line 5

What I have tried:

when i run edit.php
its shows error:
Notice: Undefined index: id in C:\xampp\htdocs\crud_php\mvc\edit.php on line 5
Posted
Updated 8-Apr-18 1:40am
v3

Notice: Undefined index: id in C:\xampp\htdocs\crud_php\mvc\edit.php on line 5

this means that id does not exist in
PHP
$student_id = $_GET['id'];

check fields in the page that call this code.
 
Share this answer
 
Comments
Member 13761429 8-Apr-18 23:30pm    
my program work when i run view_student.php and edit it...but its shows error only when i run edit.php
Member 13761429 8-Apr-18 23:31pm    
<?php
require '../mvc/student.php';
in edit.php


$obj = new Student();

$student_id = $_GET['id'];
$result = $obj->select_student_info_by_id($student_id);
$student_info = $result->fetch_assoc();
Patrice T 9-Apr-18 5:06am    
Advice: use the debugger to see what is in $_GET
Member 13761429 9-Apr-18 6:00am    
ok but how to see specifically from debugger what is in $_GET
Patrice T 9-Apr-18 6:27am    
I don't use PHP, so you have to read manual or find tuto
I'm not sure about the link you have in the table. Instead of
<a href="edit.php?id=<?php echo $row['student_id'] ?>">Edit</a>

try
<a href='edit.php?id=".$row["student_id"]."'>Edit</a>
 
Share this answer
 
Comments
Member 13761429 8-Apr-18 23:29pm    
not work....edit not work....my program work when i run view_student.php and edit it...but its shows error only when i run edit.php
Wendelius 9-Apr-18 23:30pm    
When calling the add_student, you do not pass an OD to the code, hence you have now problem. See
Add student|View student

What is the error you get?
Member 13761429 10-Apr-18 4:55am    
no error come...my program run completely and also edit function work...but its shows error only when i run edit.php page otherwise its work
Wendelius 11-Apr-18 11:43am    
You wrote "its shows error only when i run edit.php"

What is the error you get?
Member 13761429 11-Apr-18 22:59pm    
Notice: Undefined index: id in C:\xampp\htdocs\crud_php\mvc\edit.php on line 5

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