delete from db problem

amira - July 4, 2009 - 11:01

i tried to delete from db but nothing happened....any suggestions!!!!!!!!

<?
function managelinks_block($op = 'list', $delta = 0, $edit = array())

global $base_url;
if ($op == "list")
{
    // Generate listing of blocks from this module, for the admin/block page
    $block = array();
    $block[0]["info"] = t('Manage Link Block');
    return $block;
  }
 
else if ($op == 'view')
{

   $query = "SELECT title, url FROM managelinks";
   $queryResult =  db_query($query);
  
   $path_self = "/" . drupal_lookup_path('alias', $_GET['q']);
   $block_content = "<div align=\"center\"><form name=\"makechange\" id=\"f1\" method=\"get\" action=\"" . $base_url . $path_self . "\" />\r";
   $block_content .='<table id="t" border=3>';

  while ($links = db_fetch_object($queryResult))
  {
   $block_content .='<tr><td><input type="textbox" name="ti" value="'.$links->title.'" readonly="readonly"></td>';
   $block_content .='<td><input type="textbox" name="ur" value="'.$links->url.'" readonly="readonly"></td>';
   $block_content .='<td><input type="submit" name="Edit" value="Edit"></td>';
   $block_content .=   '<td><input type="submit" name="delete" value="Delete"> <INPUT TYPE="hidden"  name="delete" VALUE="delete"></td>';
$block_content .='</br>';
$block_content .=   '</tr>';  
  }
 
  $block_content .='</table></form></div>';
  $block_content .='<td><input type="button" name="ADD" value="ADD" onclick=add("t") ></td>';


}



if ($block_content == '')
{   
    /* No content from a week ago.  If we return nothing, the block  
     * doesn't show, which is what we want.
     */
    return;
  }

  // set up the block 
  $block = array();
  $block['subject'] = 'Manage Your Links'; 
  $block['content'] = $block_content;
 
return $block;

} // end function


  ?>
 

<script language="javascript">
var count = "1";
function add(t)
{

                     var tbody = document.getElementById("t").getElementsByTagName("TBODY")[0];

                     // create row
                     var row = document.createElement("TR");
                   
                    // create table cell 1 ----- title
                     var td1= document.createElement("TD")
                     var strHtml1= "<INPUT TYPE=\"text\" NAME=\"ti\">";
                    td1.innerHTML = strHtml1.replace(/!count!/g,count);


// create table cell 2 ----- url
                     var td2 = document.createElement("TD")
                     var strHtml2 = "<INPUT TYPE=\"text\" NAME=\"ur\">";
                    td2.innerHTML = strHtml2.replace(/!count!/g,count);


                

var td3 = document.createElement("TD")
                    var strHtml3 = "<INPUT TYPE=\"submit\"  VALUE=\"Save\"   ><INPUT TYPE=\"hidden\"  name=\"insert\" VALUE=\"insert\">";
                    td3.innerHTML = strHtml3.replace(/!count!/g,count);



                    // append data to row
                        row.appendChild(td1);
                        row.appendChild(td2);
                        row.appendChild(td3);
                 
                       
               // add to count variable
                          count = parseInt(count) + 1;
                     // append row to table
                           tbody.appendChild(row);
 
}


</script>

<?
if ($_GET[insert]=="insert")
{

  $query2 = "INSERT INTO managelinks (title ,url )VALUES ('".$_GET[ti]."', '".$_GET[ur]."')";
  $queryResult2 = db_query($query2);
  if($queryResult2)
  {
  drupal_set_message(t("added ok"));
  }
}

else if ($_GET[delete]=="delete")
{

$query3 = "delete from managelinks where title='.$_GET[ti].'";


  $queryResult3 = db_query($query3);
  if($queryResult3)
  {
  drupal_set_message(t("Deleted ok"));
  }
}
?>

did u check whether it is coming into the delete case

junedkazi - July 4, 2009 - 11:53

did u check whether it is coming into the delete case .
If yes then check whether $_GET[ti] is having some value.

i checked but the problem is

amira - July 4, 2009 - 12:22

i checked but the problem is that it deleted now ok but the last record in the db it didnot delete what i said it to delete
here is the update of my code

<?
function managelinks_block($op = 'list', $delta = 0, $edit = array())

global $base_url;
if ($op == "list")
{
    // Generate listing of blocks from this module, for the admin/block page
    $block = array();
    $block[0]["info"] = t('Manage Link Block');
    return $block;
  }
 
else if ($op == 'view')
{

   $query = "SELECT title, url FROM managelinks";
   $queryResult =  db_query($query);
  
   $path_self = "/" . drupal_lookup_path('alias', $_GET['q']);
   $block_content = "<div align=\"center\"><form name=\"makechange\" id=\"f1\" method=\"get\" action=\"" . $base_url . $path_self . "\" />\r";
   $block_content .='<table id="t" border=3>';

  while ($links = db_fetch_object($queryResult))
  {
   $block_content .='<tr><td><input type="textbox" name="ti" value="'.$links->title.'" readonly="readonly"></td>';
   $block_content .='<td><input type="textbox" name="ur" value="'.$links->url.'" readonly="readonly"></td>';
   $block_content .='<td><input type="submit" name="Edit" value="Edit"></td>';
   $block_content .=   '<td><input type="submit" name="delete" value="Delete"> <INPUT TYPE="hidden"  name="delete" VALUE="'.$links->title.'"></td>';
$block_content .='</br>';
$block_content .=   '</tr>';  
  }
 
  $block_content .='</table></form></div>';
  $block_content .='<td><input type="button" name="ADD" value="ADD" onclick=add("t") ></td>';


}



if ($block_content == '')
{   
    /* No content from a week ago.  If we return nothing, the block  
     * doesn't show, which is what we want.
     */
    return;
  }

  // set up the block 
  $block = array();
  $block['subject'] = 'Manage Your Links'; 
  $block['content'] = $block_content;
 
return $block;

} // end function


  ?>
 

<script language="javascript">
var count = "1";
function add(t)
{

                     var tbody = document.getElementById("t").getElementsByTagName("TBODY")[0];

                     // create row
                     var row = document.createElement("TR");
                   
                    // create table cell 1 ----- title
                     var td1= document.createElement("TD")
                     var strHtml1= "<INPUT TYPE=\"text\" NAME=\"ti\">";
                    td1.innerHTML = strHtml1.replace(/!count!/g,count);


// create table cell 2 ----- url
                     var td2 = document.createElement("TD")
                     var strHtml2 = "<INPUT TYPE=\"text\" NAME=\"ur\">";
                    td2.innerHTML = strHtml2.replace(/!count!/g,count);


                

var td3 = document.createElement("TD")
                    var strHtml3 = "<INPUT TYPE=\"submit\"  VALUE=\"Save\"   ><INPUT TYPE=\"hidden\"  name=\"insert\" VALUE=\"insert\">";
                    td3.innerHTML = strHtml3.replace(/!count!/g,count);



                    // append data to row
                        row.appendChild(td1);
                        row.appendChild(td2);
                        row.appendChild(td3);
                 
                       
               // add to count variable
                          count = parseInt(count) + 1;
                     // append row to table
                           tbody.appendChild(row);
 
}


</script>

<?
if ($_GET[insert]=="insert")
{

  $query2 = "INSERT INTO managelinks (title ,url )VALUES ('".$_GET[ti]."', '".$_GET[ur]."')";
  $queryResult2 = db_query($query2);
  if($queryResult2)
  {
  drupal_set_message(t("added ok"));
  }
}

else if ($_GET[delete])
{


$query3 = "delete from managelinks where title='$_GET[ti]'";

drupal_set_message( $query3);
  $queryResult3 = db_query($query3);
  if($queryResult3)
  {
  drupal_set_message(t("Deleted ok"));
  }
}
?>

If it did not delete what u want it to delete then

junedkazi - July 4, 2009 - 12:48

If it did not delete what u want it to delete then the problem will be with the value
u get in $_GET['ti'] .

Just before delete query is fired just check the value in $_GET['ti'] and check if that
value exists in the table.

yes the problem in

amira - July 4, 2009 - 13:10

yes the problem in $_GET['ti'] which is array with all values ,could i get the only value of the record which i click to delete

you will have to get the title value in the $_GET

junedkazi - July 4, 2009 - 15:13

You will have to get the title value in the $_GET['ti'].
It cannot be an array.

$GET['ti'] should hold a single value it cannot be an array.

BTW who is setting the value in $_GET['ti'] .

 
 

Drupal is a registered trademark of Dries Buytaert.