1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

  2. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

Trouble deleting record php sql jumping to else

Discussão em 'Outras Linguagens' iniciado por Stack, Junho 30, 2021.

  1. Stack

    Stack Membro Participativo

    I am trying to delete a record in php mysql but my code jumps me to the else statement where I have "record not deleted" message printed out. Strangely it prints out 3 times. I am attempting to have it simply show an alert when the record is deleted to keep things simple. I know I am connected to the db as I can see my records on the screen.

    I can see the sql id record in the address bar so I also know it is going for the correct record but on the screen, I see - Error Deleting RecordError Deleting RecordError Deleting Record

    I am sure it is a simple thing but for the life of my, I just can't seam to identify where I could have gone wrong. If anyone can help me spot the issue, I would surely appreciate it.

    My code is as follows -

    user-data.php

    <?php require_once('protector.php'); ?>
    <?php
    $title = "Zitalk | User Data";
    include('header.php');
    ?>
    <main style="padding-top: 150px; text-align: center;">
    <div class="row justify-content-center">
    <div class="col-auto mb-5">
    <table class="table table-dark table-striped">
    <tr>
    <th scope="col">Id</th>
    <th scope="col">First Name</th>
    <th scope="col">Last Name</th>
    <th scope="col">Email</th>
    <th scope="col">Telephone Number</th>
    <th scope="col">Action</th>
    </tr>
    <?php
    $conn = mysqli_connect("MYDBHOST", "MYDBUSERNAME", "MYDBPASS", "MYDBNAME");
    if($conn-> connect_error) {
    die("Connection failed:". $conn-> connect_error);
    }
    $sql = "SELECT id, firstname, lastname, email, tel FROM users";
    $result = $conn->query($sql);
    if($result-> num_rows > 0) {
    while($row = $result-> fetch_assoc()) {
    ?>
    <tr>
    <td><?php echo $row["id"]; ?></td>
    <td><?php echo $row["firstname"]; ?></td>
    <td><?php echo $row["lastname"]; ?></td>
    <td><?php echo $row["email"]; ?></td>
    <td><?php echo $row["tel"];?></td>
    <td>
    <a href="deletecode.php?id=<?php echo $row['id']; ?>" class="btn btn-xs btn-danger del_btn deletebtn" style="max-width: 75px; max-height: 50px; font-size: 12px;padding: 5px 7px;margin: 0;">DELETE</a></td>
    <tr>


    <?php
    }
    }
    ?>
    </table>

    </div>
    </div>


    deletecode.php

    <?php
    $conn = mysqli_connect("MYDBHOST", "MYDBUSERNAME", "MYDBPASS", "MYDBNAME");
    if($conn-> connect_error) {
    die("Connection failed:". $conn-> connect_error);
    }
    $sql = "SELECT id, firstname, lastname, email, tel FROM users";
    $result = $conn->query($sql);
    if($result-> num_rows > 0) {
    while($row = $result-> fetch_assoc()) {
    $id = $_GET['id'];
    $del = mysqli_query($db, "DELETE FROM users WHERE id='$id'");
    if($del) {
    echo '<script>alert("User Deleted");</script>';
    mysqli_close($db);
    header('Location: user-data.php');
    exit;
    } else {
    echo 'Error Deleting Record';
    }
    }
    }


    Again, if anyone can help me out here, I would surely appreciate it. Thank you in advance.

    Continue reading...

Compartilhe esta Página