newToPhp01 Posted November 5, 2017 Share Posted November 5, 2017 I'm trying to make a editable form for a webpage in PHP. I have a database with 3 columns. The rows in this database gets printed to the webpage in a table, and I have made a button that says "Edit" next to each row. When the user clicks the edit button I want a form to show, and in that form the content from the database should show so that the user can edit this row, then click "save" and the database will update.I am trying to learn OOP, but I'm not good at it and i'm not sure what to do next. I have managed to get the ID of the row that has been clicked like this:/*If editbutton is clicked */if(isset($_GET['edit'])) { $edit_id = $_GET['edit'];}So I have that in a variable.But how can I get that specific row into a form so that user can edit it? Quote Link to comment Share on other sites More sharing options...
Barand Posted November 5, 2017 Share Posted November 5, 2017 You query the database to get the record with that I'd then display the contents in form input fields for the user to edit. Quote Link to comment Share on other sites More sharing options...
newToPhp01 Posted November 5, 2017 Author Share Posted November 5, 2017 (edited) Sorry if I ask stupid querstions. But how do I write that query in my class? Where do I put my $edit_id? This is what I have, but I know that this is not working: public function editHotelRoom($id) { $sql = "SELECT * FROM hotelroom WHERE id=" . $id; $result = $this->db->query($sql); return mysqli_fetch_all($result, MYSQLI_ASSOC); $db->close();} Edited November 5, 2017 by newToPhp01 Quote Link to comment Share on other sites More sharing options...
Barand Posted November 5, 2017 Share Posted November 5, 2017 It really helps us to help you if you tell us what "not working" means. We cannot see you screen. As you will only retrieve a single row the is no point in using fetch_all(), just use fetch_assoc(). Don't close the connection after every query. It will close automatically when the script ends. Also $db will be undefined in that function (turn error reporting on). This could be why it's "not working". Quote Link to comment Share on other sites More sharing options...
newToPhp01 Posted November 5, 2017 Author Share Posted November 5, 2017 Your right. I thought there was something obviousy wrong with how the function was written. The database is defined earlier in my class and is working. I have a few other functions as well. I guess my main problem is that i'm not sure about the syntax or how to use variables. I have that if-statement in my main.php file and the variable $edit_id has the ID of the row. And in my main.class.php file I have a database-connection and a class with functions. But I don't know how to make the function find my row. Do I need to instantiate the function first? Like this: $editRooms = new hotelUsers();$theRooms = $editRooms->editHotelRoom(); Then like this: /*If editbutton is clicked*/if(isset($_GET['edit'])) { $edit_id = $_GET['edit']; $editRooms->editHotelRoom($edit_id);} And this in my class: public function editHotelRoom($id) { $sql = "SELECT * FROM hotelroom WHERE id=" . $id; $result = $this->db->query($sql); return mysqli_fetch_all($result, MYSQLI_ASSOC); $db->close();} When I try this I get these warnings: Warning: Missing argument 1 for hotelUsers::editHotelRoom(), called in... Warning: mysqli_fetch_all() expects parameter 1 to be mysqli_result, boolean given in... Notice: Undefined variable: id in... Quote Link to comment Share on other sites More sharing options...
Barand Posted November 5, 2017 Share Posted November 5, 2017 Looks like you have some reading to do on functions, arguments/parameters and return values before embarking on anything else, like OOP. Quote Link to comment Share on other sites More sharing options...
newToPhp01 Posted November 5, 2017 Author Share Posted November 5, 2017 I sure do! I'm a bit lost. I'm a beginner and haven't done any programming before. Trying to learn. Do you have any tips to where the biggest errors in my code is? Quote Link to comment Share on other sites More sharing options...
Barand Posted November 5, 2017 Share Posted November 5, 2017 Looks like you have some reading to do on functions, arguments/parameters and return values before embarking on anything else, like OOP. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.