Jump to content

Update database with form


newToPhp01

Recommended Posts

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?

Link to comment
Share on other sites

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();
}

Link to comment
Share on other sites

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".

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.