I am currently using one form only for both updating and inserting, it works well. But I would like to have it prefilled when updating only. I am not exactly sure how to do this. If I set the value with the php variable I get an error in the log that states it is not defined. Any help is appreciated. Thank you.
Here is the form:
<?php include_once('Crud_class.php'); $obj = new Crud("loocalhost","root","password","mydb"); $id = $_GET['id']; if (isset($_GET['id']) && $_GET['id'] > 0) { echo "Update for record id#:" . $_GET['id']; } else { echo "Insert new book"; } ?> <html> <head> <title>Add New Product</title> </head> <body> <form method="post" action="actions.php"> <ul class="form"> <li><label for"title">Title:</label> <input type="text" name="title" value="<?php echo $title?>" /></li> <li><label for="author">Author:</label> <input type="text" name="author"/></li> <li><label for="category">Category:</label> <select name="category"> <option value="General">General</option> <option value="HTML/CSS">HTML/CSS</option> <option value="Javascript">Javascript</option> <option value="PHP">PHP</option> <option value="Other">Other</option> </select></li> <li><label for"description">Description:</label> <textarea name="description"></textarea></li> <li><label for="img_path">Image Path:</label> <input type="text" name="img_path"/></li> <input type="hidden" name="id" value="<?php if ($id > 0) { echo $id;} else {echo 0;} ?>"/> <li><input class="submit" type="submit" name="submit" value="Submit"/></li> </ul> </form> </body> </html> and here is my actions file:
<?php include('Crud_class.php'); $obj=new Crud("localhost","root","password","mydb"); if (isset($_POST['id']) && $_POST['id'] > 0) { //update extract($_REQUEST); $obj->update($id,$title,$author,$category,$description,$img_path); } else { // insert extract($_REQUEST); $obj->insert($title,$author,$category,$description,$img_path); } ?> and my crud file
<?php class Crud{ public $mysqli; public $data; public function __construct($host,$username,$password,$db_name){ $this->mysqli = new mysqli('localhost', 'root', 'password', 'mydb'); } // BOOKS Table //READ public function readAll(){ $query="SELECT * FROM books"; $result= $this->mysqli->query($query); $num_result=$result->num_rows; if($num_result>0){ while($rows=$result->fetch_assoc()){ $this->data[]=$rows; //print_r($rows); } return $this->data; } } //INSERT public function insert($title,$author,$category,$description,$img_path){ $query="INSERT INTO books SET title='$title', author='$author', category='$category', description='$description', img_path='$img_path'"; $result= $this->mysqli->query($query) or die(mysqli_connect_errno()."Product Failed to Insert"); if($result){ header('location:read.php?insert_status=success'); } } //UPDATE public function update($id,$title,$author,$category,$description,$img_path){ $query="UPDATE books SET title='$title', author='$author', category='$category', description='$description', img_path='$img_path' WHERE id='$id'"; $result= $this->mysqli->query($query) or die(mysqli_connect_errno()."Cannot update"); if($result){ header('location:read.php?update_status=success'); } } //Delete public function delete($id){ $query="DELETE FROM books WHERE id='$id'"; $result= $this->mysqli->query($query) or die(mysqli_connect_errno()."Failed to Delete"); if($result){ header('location:read.php?delete_status=success'); } } } ?>