Решить неперехваченной ошибки выборки объекта
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>
при выполнении редактирования возникла эта ошибка:
Фатальная ошибка: Неперехваченная ошибка: вызов функции-члена fetch_object() на boolean in C:\xampp\htdocs\crud_php\crud\control.php:40 трассировка стека: #0 C:\xampp\htdocs\crud_php\crud\edit.php(2): include() #1 {main} брошен C:\xampp\htdocs\crud_php\crud\control.php на линии 40
Что я уже пробовал:
при выполнении редактирования возникла эта ошибка:
Фатальная ошибка: Неперехваченная ошибка: вызов функции-члена fetch_object() на boolean in C:\xampp\htdocs\crud_php\crud\control.php:40 трассировка стека: #0 C:\xampp\htdocs\crud_php\crud\edit.php(2): include() #1 {main} брошен C:\xampp\htdocs\crud_php\crud\control.php на линии 40
Richard MacCutchan
Ваша переменная $fetch-это не то, что вы думаете.