简体   繁体   中英

Edit specific row from table (SQL & PHP)

I'm working on a project in PHP and I don't know how to do something.

I have a table with data from a Database.

What I want to do is on each row of table to have a button "edit" that will redirect me to another page from where I can edit and update those information. And a Delete button that will delete that data from database.

My table is :

Clients(IdClient, Name, Address, Phone)

For example I have:

Name | Address | Phone | - EDIT Button | - DELETE Button

This is my code so far.

<?php
    $db_host = 'localhost';
    $db_user = 'root';
    $db_pass = '1234';
    $db_name = "hoteldb";

    $con = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
    if ($con->connect_error)
            die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
    $sql = "SELECT * FROM clients ORDER BY IdClient DESC";
    $result = $con->query($sql);

    ?>
    <table>
        <tr style="font-weight:bold">
        <th>ID</th>
        <th>Name </th>
        <th>Address</th>
        <th>Phone</th>
        </tr>

        <?php
            if ($result->num_rows > 0) {
                 while($row = $result->fetch_assoc()) {
                    echo "<tr>";
                    echo "<td>" . $row["IdClient"] ."</td>";
                    echo "<td>" . $row["Name"]. "</td>";
                    echo "<td>" . $row["Address"] ."</td>";
                    echo "<td>" . $row["Phone"]. "</td>";
                    echo "</tr>";
                 }
            } else {
                 echo "0 results";
            }
            $con->close();
        ?>
    </table>

Can you help me, and tell me how can I do this ?

you can add another td like:

<td><a href="edit.php?id=<?php echo urlencode($row['id']); ?>">Name</a></td>

and then create a function like:

function find_client_by_id($client_id) {
        global $connection;

$safe_client_id = mysqli_real_escape_string($connection, $client_id);

$query = "SELECT * FROM `clients` WHERE id = {$safe_client_id} LIMIT 1";
$client_set = mysqli_query($connection, $query);
if($client = mysqli_fetch_assoc($client_set)) {
    return $client;
} else {
    return null;
   }
}

then in your edit.php page call the function like:

require_once('functions.php');

and also get the client id like this:

$client = find_client_by_id($_GET['id']);

now you can create your form and just call the all the information like an example:

<input type="text" name="name" value="<?php echo htmlentities($client['name'];) ?>"

and then you just update all the information form the form like an example:

$id = $client['id'];
$name = mysqli_real_escape_string($_POST['name']);
$address = mysqli_real_escape_string($_POST['address']);

$query  = "UPDATE clients SET name = '{$name}', ";
$query .= "address = '{$address}' WHERE id = {$id} LIMIT 1";
$result = mysqli_query($connection, $query);
if($result) {
   // do something like success
} else {
   // do something else
}

and for the delete you could just create a delete.php page and get the id of the client just like with the edit page and like:

<td><a href="delete.php?id=<?php echo urlencode($clients['id']); ?>" onclick="return confirm('are you sure?');">Delete</a></td>

here you also have a bit of javascript just to make sure you don't accidentally touch delete.

and in the delete.php page use the same function

$client = find_client_by_id($_GET['id']);

$id = $client_id['id'];

$query = "DELETE FROM clients WHERE id = {$id} LIMIT 1";
$result = mysqli_query($connection, $query);
if($result && mysqli_affected_rows($connection) == 1) {
    //do something
} else {
    //do something else
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM