Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Mayur Pande
PLUS
Mayur Pande
Courses Plus Student 11,711 Points

Deleting data row from mysql using twig?

I have created a table that displays users info using twig, I have also created a delete button that allows admin to delete the entire user row if need be. However some of the users I am not able to delete.

Here is the twig code;

{% for item in contacts %}
    <tr>
        <td>{{ item.email }}</td>
        <td>{{ item.name }}</td>
        <td>{{ item.address }}</td>
        <td>{{ item.phone }}</td>
        <td>{{ item.type }}</td>
        <td><form method="post" action="delete-contact.php"><input type="hidden" name="email"><input type="submit" value="Delete"></form></td>
    </tr>
{% endfor %}

Here is the php code

<?php


if (!empty($_POST['email']) || (empty($_POST['email'])) {
    $link = mysqli_connect('localhost:3306','root','somepassword','somedatabase');
    $email = mysqli_real_escape_string($link, $_POST['email']);
    mysqli_query($link, "DELETE * FROM user WHERE email = '$email'");
}

header('Location: /admin');

?>

Not sure what's going wrong

1 Answer

Chris Shaw
Chris Shaw
26,676 Points

Hi Mayur,

It would appear you are missing your value attribute for the email field which is what your SQL query is expecting but not receiving. What you want is the below which will solve the issue.

<input type="hidden" name="email" value="{{ item.email }}">

Happy coding!

Mayur Pande
Mayur Pande
Courses Plus Student 11,711 Points

Hi Chris,

Thank you for the reply I did try this out and unfortunately it still doesn't work. I had a feeling it may have had something to do with this. I was thinking do you think it would be better for me to create a delete function and then call it using twig?

Chris Shaw
Chris Shaw
26,676 Points

I see the issue, your IF statement isn't formed correctly which is another issue, it's also better to use the isset function as it covers the empty function too.

if (!isset($_POST['email'])) {
  // code here...
}
Mayur Pande
Mayur Pande
Courses Plus Student 11,711 Points

Would this mean I need to change the name attribute as well?

At the moment I have changed the php code to;

<?php


if (isset($_POST['email'])) {
    $link = mysqli_connect('localhost:3306','root','somepassword','somedb');
    $email = mysqli_real_escape_string($link, $_POST['email']);
    mysqli_query($link, "delete * from user where email = '$email'");
}

header('Location: /admin');

?>
Chris Shaw
Chris Shaw
26,676 Points

No, your name attribute is what gets stored in the _POST superglobal thus changing it would mean you need to change the attribute and any references to email being called from _POST.

Mayur Pande
Mayur Pande
Courses Plus Student 11,711 Points

Still not managing to work it out. Is the problem to do with my query?

Mayur Pande
Mayur Pande
Courses Plus Student 11,711 Points

Managed to sort it out...yay! It was to do with all the foreign keys I had for different tables. Got it working with the code below. Thanks for the help. If there is anything you can point out so that I can improve this code please let me know;

<?php


if (isset($_POST['email'])) {
    $link = mysqli_connect('localhost:3306','root','somepassword','somedb');


    $email = mysqli_real_escape_string($link, $_POST['email']);

    $delTutorQual = "DELETE FROM `tutorqualification` WHERE `email`='$email'";

    $retval1 = mysqli_query($link,$delTutorQual);

    $delTutorExp = "DELETE FROM `tutorexperience` WHERE `email`='$email'";

    $retval2 = mysqli_query($link,$delTutorExp);

    $delTutor = "DELETE FROM `tutor` WHERE `email`='$email'";

    $retval3 = mysqli_query($link,$delTutor);

    $delGroupBook = "DELETE FROM `grouptuitionbooking` WHERE `tutoremail`='$email' OR `studentemail`='$email'";

    $retval4 = mysqli_query($link,$delGroupBook);

    $delGroupTuition = "DELETE FROM `grouptuition` WHERE `tutoremail`='$email'";

    $retval5 = mysqli_query($link,$delGroupTuition);

    $sql = "delete from user where email = '$email'";

    $retval = mysqli_query($link,$sql);

    if(!$retval){
        die('Could not delete data: ' . mysqli_error());
    }
    echo "Deleted data successfully\n";


}

header('Location: /admin');

?>