-2

successfully updated but with an error <br /><b>Warning</b>: Undefined variable $topic in <b>C:\xampp\htdocs\mintech\templates\editcat.php</b> on line <b>13</b><br /><br /><b>Warning</b>: Attempt to read property Warning: Undefined array key "cat_id" in C:\xampp\htdocs\mintech\editcat.php on line 10

Am sorry am new to php oop mvc structures I tried passing the id into the action=editcat.php not i also tried input hidden to pass the cat_id still nothing i tried declaring a variable topic nothing i tried changing the placeholder when updating nothing

  public function update_Cat($data){
    //Insert Query
    $this->db->query("UPDATE `categories` SET name=:name Where cat_id = :cat_id ");
    //Bind Values
    $this->db->bind(':name', $data['name']);
    $this->db->bind(':cat_id', $data['cat_id']);
    if($this->db->execute()){
      return true;
    } else {
      return false;
    }
    //echo $this->db->lastInsertid
  }
<?php
//Create Topics Object
$topic = new Topic;
$user = new User;
$validate = new Validator;

//Get Template From URL
$categories_id = $_GET['cat_id'];


if (isset($_POST['edit_cat'])) {
    //Creatr Data array
    $data = array();
    $data['name'] = $_POST['name'];
//required array
$field_array = array('name');
if ($validate->isRequired($field_array)) {
    //Register User
    if ($topic->update_Cat($data)) {
        redirect('editcat.php', 'your Category has been updated', 'success');
    } else {
        redirect('editcat.php', 'Something went wrong with your Update', 'error');
    }
    } else {
        redirect('editcat.php',  'Name field is required',  'error');
    }
}
tereško
  • 58,060
  • 25
  • 98
  • 150
  • 1
    The error is on `editcat.php`, not these files. If you originally have a parameter on your URL when you enter `editcat.php`, then you're going to need to pass it back on your redirect – aynber Aug 09 '23 at 14:01
  • would you please explain further – Kerub Angel Aug 09 '23 at 14:07
  • For instance, if the form is on `editcat.php?cat_id=123`, then when you do the redirect on the page above, you need to pass the cat_id back. – aynber Aug 09 '23 at 14:14
  • i have already done that on the form action="editcat.php?cat_id=".$cat_id;> – Kerub Angel Aug 09 '23 at 14:24
  • Right, but you're not doing it on your redirects. `redirect('editcat.php', 'Name field is required', 'error');` does not have the cat_id, so when it redirects back, `?cat_id=...` won't exist anymore, and it will throw the errors. – aynber Aug 09 '23 at 14:35
  • this is the new error now name="name" value="
    Warning: Attempt to read property "name" on bool in C:\xampp\htdocs\mintech\templates\editcat.php on line 13
    – Kerub Angel Aug 09 '23 at 14:52
  • We still don't know the code for `editcat`, but make sure you're passing back everything you need that the original page has – aynber Aug 09 '23 at 15:24
  • This is my form code
    back
    – Kerub Angel Aug 09 '23 at 15:30
  • _"Attempt to read property "name" on bool"_ means that your `$topic` there does not contain whatever object you think it should, but is just a boolean - most likely, `false`. But the code snippets you have shown so far, are really not enough fo us to determine where creating this `$topic` variable actually went wrong. – CBroe Aug 10 '23 at 08:00
  • there is a class Topic in the folder classes then in the main question you will see where i declared: $topic = new Topic; in the question those are all the codes db = new Database; } – Kerub Angel Aug 10 '23 at 09:19
  • there is a class Topic in the folder classes then in the main question you will see where i declared: $topic = new Topic; in the question those are all the codes db = new Database; } this is how the class is inside the Topic class is the code in the question update in the comment its the form for update – Kerub Angel Aug 10 '23 at 09:29

1 Answers1

-1

it seems when i was passing the id it was not been passed through so instead i changed the method of passing the id in the form and also added input hidden and passed the id and declared it on the editcat.php then it worked wow thanks team

you really opened my mind

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 13 '23 at 12:42