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

<?php

include("model.php");

$model = new Model;

if(isset($_POST['register'])) {
	$fname = $_POST['fname'];
	$lname = $_POST['lname'];
	$uname = $_POST['uname'];
	$pass = $_POST['pass'];
	$mail = $_POST['mail'];
	$add = $_POST['add'];
	$f = "upload/";
	$f = $f.$_FILES['file']['name'];
	move_uploaded_file($_FILES['file']['tmp_name'], $f);
	$data = array("firstname"=>$fname, "lastname"=>$lname, "username"=>$uname, "password"=>$pass, "email"=>$mail, "address"=>$add, "file"=>$f);
	$model->insertall($conn, "user", $data);
	echo "inserted";
	header("Location:view_user.php");
}

$select = $model->selectall($conn, "user");

if(isset($_GET['del'])) {
	$id = $_GET['del'];
	$result = $model->deleteall($conn, "user", $id);
	if($result == false) {
		echo "Error: cannot delete user";
		return false;	
	} else {
		return true;
	}
}

if(isset($_GET['edit'])) {
	$eid = $_GET['edit'];
	$where = array("id"=>$eid);
	$fetch = $model->select_where($conn, "user", $where);
	$edit = $fetch->fetch_object();
	if(isset($_POST['update'])) {
	$fname = $_POST['fname'];
	$lname = $_POST['lname'];
	$uname = $_POST['uname'];
	$mail = $_POST['mail'];
	$add = $_POST['add'];
	$f = "upload/";
	$f = $f.$_FILES['file']['name'];
	move_uploaded_file($_FILES['file']['tmp_name'], $f);
	$data = array("firstname"=>$fname, "lastname"=>$lname, "username"=>$uname, "email"=>$mail, "address"=>$add, "file"=>$f);
	$model->updateall($conn, "user", $data,$where);
	echo "updated";
	header("Location:view_user.php");
	}
}

?>



model.php


<?php

include("conn.php");
$obj = new Connection;
$conn = $obj->connect();

class Model {
	function insertall($conn, $table, $data) {
		$keys = array_keys($data);
		$key = implode(",", $keys);
		
		$vals = array_values($data);
		$val = implode("','", $data);
		
		$ins = "INSERT INTO $table($key) VALUES('$val')";
		$conn->query($ins);
	}
	
	function selectall($conn, $table) {
		$sel = "SELECT * FROM $table";
		$res = $conn->query($sel);
		while ($row = $res->fetch_object()) {
			$r[] = $row;
		}
		return $r;
	}
	
	function deleteall($conn, $table, $id) {
		$del = "DELETE FROM $table WHERE id='$id' ";
		$result = $conn->query($del);
		if($result) {
			header("Location: view_user.php");
		}
	}
	
	function select_where($conn, $table, $where) {
		$wkey = array_keys($where);
		$wval = array_values($where);
		$sel = "SELECT * FROM $table WHERE";
		$i = 0;
		foreach($where as $w) {
			$sel.= "$wkey[$i] = '$wval[$i]'";
			$i++;
		}
		return $conn->query($sel);
	}
	
	function updateall($conn, $table, $data, $where) {
		$wkey = array_keys($where);
		$wval = array_values($where);
		
		$dkey = array_keys($data);
		$dval = array_values($data);
		
		$up = "UPDATE $table SET";
		$count = count($data);
		$i = 0;
		foreach ($data as $d) {
			if ($count == $i + 1) {
				$up.= "$dkey[$i] = '$dval[$i]'";
			} else {
				$up.= "$dkey[$i] = '$dval[$i]',";
			}
			$i++;
		}
		$up.= "WHERE 1 = 1";
		$j = 0;
		foreach($where as $w) {
			$up.= "AND $wkey[$j] = '$wval[$j]'";
			$j++;
		}
		return $conn->query($up);
	}
	
}

?>



edit.php


<?php
include("control.php");
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>registration</title>
</head>

<body>
<h1><center>Registration</center></h1><hr/>
	<form method="post" enctype="multipart/form-data">
		<table border="1" align="center">
			<tr>
				<td>First Name:</td>
				<td><input type="text" name="fname" required="" value="<?php  echo $edit->firstname;  ?>"></td>
			</tr>
			<tr>
				<td>last Name:</td>
				<td><input type="text" name="lname" required="" value="<?php  echo $edit->lastname;  ?>"></td>
			</tr>
			<tr>
				<td>User Name:</td>
				<td><input type="text" name="uname" required="" value="<?php  echo $edit->username;  ?>"></td>
			</tr>
			<tr>
				<td>Email ID:</td>
				<td><input type="email" name="mail" required="" value="<?php  echo $edit->email;  ?>"></td>
			</tr>
			<tr>
				<td>Address:</td>
				<td><textarea name="add" required="" rows="3" cols="21"><?php  echo $edit->address;  ?></textarea></td>
			</tr>
			<tr>
				<td>File:</td>
				<td><input type="file" name="file" required="" value="<?php  echo $edit->file;  ?>"></td>
			</tr>
			<tr>
				<td colspan="2"><center><input type="submit" name="update" value="Update"></center></td>
			</tr>
		</table>
	</form>
</body>
</html>



view_user.php


<?php
include("control.php");
?>
<!DOCTYPE html>
<html>
<head>
	<title>View User Details</title>
</head>
<body>
		<form method="post">
			<table border="1">
				<tr>
					<th>id</th>
					<th>First Name</th>
					<th>Last Name</th>
					<th>User Name</th>
					<th>Email Id</th>
					<th>Address</th>
					<th>File</th>
					<th>Delete</th>
					<th>Edit</th>
				</tr>
			<?php
				foreach($select as $s)
				{
			?>
						<tr>
							<td><?php echo $s->id; ?></td>
							<td><?php echo $s->firstname; ?></td>
							<td><?php echo $s->lastname; ?></td>
							<td><?php echo $s->username; ?></td>
							<td><?php echo $s->email; ?></td>
							<td><?php echo $s->address; ?></td>
							<td><img src="<?php echo $s->file; ?>" height="100px" width="100px"></td>
							<td><a href="view_user.php?del=<?php echo $s->id; ?>">Delete </a></td>
							<td><a href="edit.php?edit=<?php echo $s->id; ?>">Edit </a></td>
						</tr>
						<?php
						}
					?>
			</table>	
		</form>
</body>
</html>


when perform edit this error occured:

Fatal error: Uncaught Error: Call to a member function fetch_object() on boolean in C:\xampp\htdocs\crud_php\crud\control.php:40 Stack trace: #0 C:\xampp\htdocs\crud_php\crud\edit.php(2): include() #1 {main} thrown in C:\xampp\htdocs\crud_php\crud\control.php on line 40

What I have tried:

when perform edit this error occured:

Fatal error: Uncaught Error: Call to a member function fetch_object() on boolean in C:\xampp\htdocs\crud_php\crud\control.php:40 Stack trace: #0 C:\xampp\htdocs\crud_php\crud\edit.php(2): include() #1 {main} thrown in C:\xampp\htdocs\crud_php\crud\control.php on line 40
Posted
Updated 12-Apr-18 0:23am
Comments
Richard MacCutchan 12-Apr-18 6:10am    
Your variable $fetch is not what you think it is.

1 solution

Always perform error checking when functions may indicate errors. In your case the error is sourced by the call to PHP: mysqli::query - Manual[^]. Read there about the return value:
Quote:
Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.
If the call fails, you will not get a mysqli_result object returned but a boolen FALSE. And that does not have member function fetch_object() ("Call to a member function fetch_object() on boolean"). See the example code at the above link on how to check for errors and handle them.
 
Share this answer
 
Comments
Member 13761429 12-Apr-18 6:58am    
how to error checking
Jochen Arndt 12-Apr-18 7:28am    
See the example code at the PHP manual pages for the query function (link provided in my answer) and also all other functions that return a boolean or an object:
if ($fetch = $model->query($query)) {
// Success: Can use $fetch->fetch_object() and other member functions
$fetch->close();
} else {
// Error: Use $model->error to get the error message
}

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