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 trialMayur Pande
Courses Plus Student 11,711 PointsDeleting 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
26,676 PointsHi 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
Courses Plus Student 11,711 PointsMayur Pande
Courses Plus Student 11,711 PointsHi 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
26,676 PointsChris Shaw
26,676 PointsI see the issue, your
IF
statement isn't formed correctly which is another issue, it's also better to use theisset
function as it covers theempty
function too.Mayur Pande
Courses Plus Student 11,711 PointsMayur Pande
Courses Plus Student 11,711 PointsWould this mean I need to change the name attribute as well?
At the moment I have changed the php code to;
Chris Shaw
26,676 PointsChris Shaw
26,676 PointsNo, 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 toemail
being called from_POST
.Mayur Pande
Courses Plus Student 11,711 PointsMayur Pande
Courses Plus Student 11,711 PointsStill not managing to work it out. Is the problem to do with my query?
Mayur Pande
Courses Plus Student 11,711 PointsMayur Pande
Courses Plus Student 11,711 PointsManaged 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;