Delete fail

Hi again,
i’m still working on on my project and i encountered another problem. I’m trying to put delete button, but it not works page just refreshs. What i’m doing wrong?

[php]function displayTerms(){
global $database;
$q = "SELECT ID,terminai,reiksme "
.“FROM “.TBL_TERM.” ORDER BY terminai DESC”;
$result = $database->query($q);
/* Error occurred, return given name by default /
$num_rows = mysql_numrows($result);
if(!$result || ($num_rows < 0)){
echo “Error displaying info”;
return;
}
if($num_rows == 0){
echo “Lentelė tuščia.”;
return;
}
/
Display table contents */
echo “<table align=“left” border=“1” cellspacing=“0” cellpadding=“3”>\n”;
echo “

Terminas Reikšmė \n”;
echo “”;
for($i=0; $i<$num_rows; $i++){
$uid = mysql_result($result,$i,“ID”);
$uterm = mysql_result($result,$i,“terminai”);
$ureiksm = mysql_result($result,$i,“reiksme”);
  echo "<tr><td>$uterm</td><td>$ureiksm</td><td><input type=hidden name=uid value=$uid><input type=hidden name=subdel value=1><input type=submit value=Ištrinti></td></tr>\n";

}
echo “”;
echo “
\n”;
}
[/php]

[php] function AdminProcess(){
if(isset($_POST[‘subdel’])){
$this->procDeleteTerm2();
}
}

function procDeleteTerm2(){
global $session, $database;
$id=$_GET[‘uid’];
$q = “DELETE FROM”.TBL_TERM.“WHERE ID=’$id’”;
$database->query($q);
header("Location: ".$session->referrer);
}
[/php]

Well, “DELETE FROMMYTABLE” will not work in a SQL… Add spaces before and after your table…

$q = "DELETE FROM".TBL_TERM."WHERE ID='$id'";
$q = "DELETE FROM ".TBL_TERM." WHERE ID='$id'";

thank you for our response, but that didn’t work. Is there any chance that this line [php]$id=$_GET[‘uid’];
[/php] is wrong, and $id doesn’t get value?

Well, you did not show us your HTML FORM that calls this code, so not sure.

But, here is a very simple trick to find out…

Right after this line: $id=$_GET[‘uid’];

Just put echo $id;
Or, echo “" . $id . "”; (More noticeable…)

And, then you can see what it it getting for the value. If it is okay, then it is not the problem…

Lastly, $_GET[] is used to retrieve “ARGUMENTS”, so it would be from the HREF calling the page.
(Not from a form’s data which you get with $_POST[]… Just in case…)

form that calling this function is in first function. Maybe what i wrote there is impossible to work? I tried to experiment by echoing form, not sure if it’s impossible, button is shown on the page but it does nothing

I do not understand what you are saying…

You showed us 3 functions, nothing else…

To have PHP code work, it has to be called from either a HTML page, another PHP page or even a Javascript page. There must be a page that calls your PHP code… In that page, you send the user id to the PHP code and it read it with $_GET[‘uid’]… But, is that page calling the PHP code with arguments or just posting the form?

Never mind, i changed to $_GET to $_POST and it it started to work, strange because i wasn’t working earlier.

And, here is my form, from first function, i didnt know if it can work this way.
[php] echo “”;
for($i=0; $i<$num_rows; $i++){

$uid = mysql_result($result,$i,“ID”);
$uterm = mysql_result($result,$i,“terminai”);
$ureiksm = mysql_result($result,$i,“reiksme”);

  echo "<tr><td>$uterm</td><td>$ureiksm</td><td><input type=hidden name=uid value=$uid><input type=hidden name=subdel value=1><input type=submit value=Ištrinti></td></tr>\n";[/php]

Thank you for your help :slight_smile:

Okay, showing this line: echo “”;

Tells us that is it a standard FORM that is sending the data to the functions.
Therefore, you must use $_POST[‘variable-name’] to capture the data from the fields.

You only use $_GET[] format if you are sending the data on the HREF line such as this:
Hope that makes sense…

Glad you got it going…

Thanks, now i will know the difference between these two global variables.

Sponsor our Newsletter | Privacy Policy | Terms of Service