PHP delete help

So I am trying to delete data from my custom made API but am getting http code 404, i as wondering if my curl code is correct and matches up to the delete page function. thanks fro any help

[php]if(isset($_GET[‘tablename’]))
{
$tableName = $_GET[‘tablename’];
$id = $_GET[‘id’];
$idColumn = $_GET[‘idColumn’];
$values = array($idColumn => $id);
$result = delete($values, $tableName);
}
?>[/php]

[php]<?php

function delete (array $id, $resourcename)
{

$id[key($id)];
$url = DOMAIN . FOLDER . APIPATH . “{$resourcename}/” . rawurlencode(key($id));
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,‘DELETE’);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_exec($ch);

echo curl_getinfo($ch, CURLINFO_HTTP_CODE);

}

?>
[/php]

Howdy, Bill…

Well, you don’t really show us all of your URL data, but, you should try this debugging method to see what
is heading into the curl function…

Change:
[php]
$ch = curl_init($url);
[/php]
to:
[php]
$die($url);
$ch = curl_init($url);
[/php]

It will kill your page and show you what is inside of the $url variable so that you can debug it.
My guess is that you are missing slashes between your domain, folders, apipath’s, etc…

Let us know…

Hi ErnieAlex

I have tried what you suggested and this is my URL

http://xserve.uopnet.here.ac.uk/modules/isad259/name/api/Books/ISBN

This is my URL returned when I click my delete button ISBN is the ID/primary key in the table.

It looks correct although I am unsure what the issue is.

I have a delete link next to each row of data and i want to click delete to get rid of the row i click the delete button on.

Well, Bill, let’s see, first I will assume your row of data is inside a table? And, you have added a button to that
row that calls a PHP script and deletes data from where ever you get the table created from. Then, you re-do
the table and that row is gone. Is that what you mean?

Normally, how I handle that is using a button that formatted a bit like this…

name="del_row">Delete this row

It is actually really a submit button. The ID that is in the value of the button is unique to the row. The name
of the button is the same as every other row’s name. When the user presses any row’s button, it call my
PHP code which deletes that row from the database using the ID in the value of the posted row. Since the
user can only press one button at a time, it has only one ID to delete. Then, it just continues reloading the
table and the page. This has worked very well for me for many years… Also, I use CSS to make the button
look really nice! (Lots of websites that make fancy buttons online for you…)

Now that I have explained how delete buttons are used in general, curl is not really used for this. Curl is
more for website access and posting to sites. Perhaps I do not understand what you are deleting. Is it
data in a database? Are you loading a table with books and then wanting to delete one from a list or from
a table? Or do you want to delete a file in that URL? The curl code you show is set to delete a URL. If that
is what you want to do, first you need to make sure that URL exists. Also, I do feel that you still need to
make sure that the URL in the CURL routine is formatted correctly. That was the DIE() function I showed
you. I bet when you try that, that you will find it has an extra space in the URL or a slash missing.

One last note is that the folder you are deleting a file from must have the correct permissions set on it to
allow CURL to delete files from it.

Not sure if any of this helps…

Hi

I am deleting a row from a database table.

I think i now know what is wrong,

The error is coming in the way that i am accessing the identifier of the resource to be deleted.
So for example : If you wanted to delete author_name = Pratchett, Terry - this would be in the $id variable. You would need to consider how to get to the array. Consider:
$idColumn = $key($id) //This pulls back author_name. $idToBeDeleted =
$id[$idColumn] //This pulls out Pratchett, Terry from the $id variable because it is using the
ct the value.

but am unsure how to fix this

Well, Bill, I am totally confused with your code and comments… Let’s start over…

You started by talking about using CURL to delete a file. So we discussed using DIE() function to see what
file you were trying to delete. Your URL in the delete code in CURL was:
http://xserve.uopnet.here.ac.uk/modules/isad259/name/api/Books/ISBN

This is a FOLDER, not a file. Then, you talked about problems getting the ID of a row that was displayed.
I showed you how to do that with buttons that work very well. But, it is based on using the ID of a row
of data from a table and deleting that row thru a database query.

Now, you are talking about deleting a row of data in a database. You would NOT use CURL for that. You
just have PHP code at the top of the page instead. You have your form post to itself using something like

... So that when the submit button is pressed or when a button of type='submit' is pressed, the form is sent to itself. Then, in the PHP code, you do a very simple check for this and handle the delete from database. Loosely like this: <?PHP if (isset($_POST["delete_button"])) { $row_id=$_POST["delete_button]; $query="DELETE FROM table-name WHERE id=" . $row_id; mysqli_query($conn, $query); etc... } ?> So, the page posts to itself and then deletes the one row and then redisplays the page with the one row missing... Easy...

Is this what you need? Hope so…

Sponsor our Newsletter | Privacy Policy | Terms of Service