Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 view_student() {
		
	}
	
	public function edit_student() {
		
	}
	
	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');
	}
}

?>




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" align="center"><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</a> |
                <a href="delete.php?id = <?php echo $row['student_id'] ?>">Delete</a>
                </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);

?>


What I have tried:

solve the problem about delete action
Posted
Updated 7-Apr-18 19:50pm
v2

Looking at the DELETE statement, is the student_id column really a string column? If not, try removing the apostrophes around the value. In other words
$sql = "DELETE FROM tbl_student WHERE student_id = $student_id";

Also you should consider using parameters to provide the value to the statement. Have a look at PHP: Prepared Statements - Manual[^]
 
Share this answer
 
Comments
Member 13761429 8-Apr-18 6:21am    
edit.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 '
';
print_r($student_info);
exit;*/

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

?>
<!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>Edit student info



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





when i run edit.php its shows error like this:


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

Fatal error: Uncaught Error: Call to a member function fetch_assoc() on boolean in C:\xampp\htdocs\crud_php\mvc\edit.php:7 Stack trace: #0 {main} thrown in C:\xampp\htdocs\crud_php\mvc\edit.php on line 7
Wendelius 8-Apr-18 6:29am    
It looks like you're trying to use id before it's set.

You can use PHP: isset - Manual[^] to investigate if the value has been set.

Have a try with

if(!isset( $_GET['id'])) {
echo "set id first";
}
Member 13761429 8-Apr-18 6:32am    
where i use that...
i used like this...
<?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();
PHP
$sql = "INSERT INTO tbl_student(student_name, email_address, mobile_number)VALUES('$data[student_name]', '$data[email_address]', '$data[mobile_number]')";

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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