解决提取对象的未捕获错误 [英] Solve uncaught error of fetch object

查看:158
本文介绍了解决提取对象的未捕获错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

control.php

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





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





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





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>



执行编辑时发生此错误:

致命错误:未捕获错误:在C:\ xampp \ htdocs \ crud_php \ crud \ control.php:40中的布尔值上调用成员函数fetch_object():40堆栈跟踪:#0 C:\ xampp \ htdocs \ crud_php \ crud \ edit .php(2):include()#1 {main}在第40行的C:\ xampp \ htdocs \ crud_php \ crud \ control.php中抛出

我尝试过的事情:

执行编辑时发生此错误:

致命错误:未捕获错误:在C:\ xampp \ htdocs \ crud_php \ crud \ control.php:40中的布尔值上调用成员函数fetch_object():40堆栈跟踪:#0 C:\ xampp \ htdocs \ crud_php \ crud \ edit .php(2):include()#1 {main}在第40行的C:\ xampp \ htdocs \ crud_php \ crud \ control.php中抛出



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

推荐答案

型号=新型号; if(isset(
model = new Model; if(isset(


_POST [''register''])){
_POST[''register''])) {


fname =


这篇关于解决提取对象的未捕获错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆