0

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'); } } } ?> 
3
  • What is not defined? Please include the complete error from your log. Commented Jan 9, 2014 at 18:08
  • [Thu Jan 09 09:44:43 2014] [error] [client 127.0.0.1] PHP Notice: Undefined index: title in /home/prissipix/Projects/BTS_Bookshelf/form.php on line 39, referer: 127.0.0.1/Projects/BTS_Bookshelf/read.php?update_status=success Commented Jan 9, 2014 at 18:15
  • Sounds like your form hasn't been POSTed yet, so $_POST['title'] doesn't exist. See this Commented Jan 9, 2014 at 18:21

4 Answers 4

3

You need to get the data from the record that someone is editing on the form page.

if (isset($_GET['id']) && $_GET['id'] > 0) { echo "Update for record id#:" . $_GET['id']; // GET row from database, something like $obj->read($_GET['id']); } 

For this you need to add some read-function to your CRUD class which selects a line where id= some input integer.

If you have the data, fetch it to an object or array and use these values in the form:

<input type="text" value="<?php if (isset($data)) echo $data['author']; ?>"> 

(of course the select box needs a bit more work)

Sign up to request clarification or add additional context in comments.

Comments

1

You've already got your ID and conditions set up. Now you need to initialize the vars you will need:

if (isset($_GET['id']) && $_GET['id'] > 0) { echo "Update for record id#:" . $_GET['id']; $formValues = readRecordByID($_GET['id']); // you will need to create this crud select function } else { echo "Insert new book"; //initialize blank values: $formValues = array( 'author'=>'', 'category'=>'', 'description'=>'', 'img_path'=>''); } 

Now in your form it is safe to use:

<input type="text" name="author" value="$formValues['author']"/> 

Comments

0

Prefilling a form depends either on embedding the prefill contents in the HTML with attributes such as the value= attribute for text boxes or setting selected="selected" on an option in a <select>, or on having client-side javascript do that based on data either retrieved via AJAX or, again, embedded in the HTML.

Your Title field in the form php is an example of what I'm talking about -- you've got this:

value="<?php echo $title?>" 

and that's what you'd need for all the rest of the fields (and selected="selected" for the <select> fields).

Also, see this question

5 Comments

right, but doing it this way give me an error stating the variable is not defined. I have seen done before when two forms are used one for insert and one for update, but can't seem to get it to work when using one form. I also tried value="<?php echo $_GET['title']?>" with no luck :/
As commented on your question, what is the complete error message? What variable is not defined?
the title variable is not defined. Here is the complete error message [Thu Jan 09 09:44:43 2014] [error] [client 127.0.0.1] PHP Notice: Undefined index: title in /home/prissipix/Projects/BTS_Bookshelf/form.php on line 39, referer: 127.0.0.1/Projects/BTS_Bookshelf/read.php?
That's not what the error message says. The error message says undefined index.
Thank you for pointing it out, I am totally new to php and just trying to figure this out
0

changing the php to

<?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']; } if(isset($_REQUEST['id'])){ // this is added $id=$_REQUEST['id']; $result=$obj->mysqli->query("SELECT * FROM books WHERE id='$id'"); $rows=$result->fetch_assoc(); extract($rows); } else { echo "Insert new book"; } ?> 

and input value to

 value="<?php echo $title; ?>" 

Works!

Thanks everyone for all your help and input! :)

1 Comment

still having issues with the select box, but the rest works fine

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.