Search script mysql with delete

Discussion in 'PHP' started by SharingEngines, Nov 26, 2010.

  1. #1
    Hi,

    I have this code. He searches database and is possible delete searchead records.
    The problem is when I click on delete buton nothing happens.

    Can you help me please?

    Thank you in advance!

    <html>
    <head>
    <title>Search</title>
    </head>
    <body>
    <form name="frmSearch" method="get" action="<?=$_SERVER['SCRIPT_NAME'];?>">
      <table width="599" border="1">
        <tr>
          <th>Keyword
          <input name="txtKeyword" type="text" id="txtKeyword" value="<?=$_GET["txtKeyword"];?>">
          <input type="submit" value="Search"></th>
        </tr>
      </table>
    </form>
    <?
    
    
    
    // ALT
    $host="localhost"; // Host name
    $username="user"; // Mysql username
    $password="pass"; // Mysql password
    $db_name="name"; // Database name
    $tbl_name="tbname"; // Table name
    
    // Connect to server and select databse.
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
    
    $sql="SELECT * FROM $tbl_name";
    $objResult=mysql_query($sql);
    
    $count=mysql_num_rows($objResult);
    
    ?>
    <td><form name="form1" method="post" action="">
    <?
    
    // ALT
    
    
    if($_GET["txtKeyword"] != "")
    	{
    	
    	// Search By Name
    	$strSQL = "SELECT * FROM sh WHERE (tags LIKE '%".$_GET["txtKeyword"]."%')";
    	$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");
    	$Num_Rows = mysql_num_rows($objQuery);
    
    
    	$Per_Page = 30;   // Per Page
    
    	$Page = $_GET["Page"];
    	if(!$_GET["Page"])
    	{
    		$Page=1;
    	}
    
    	$Prev_Page = $Page-1;
    	$Next_Page = $Page+1;
    
    	$Page_Start = (($Per_Page*$Page)-$Per_Page);
    	if($Num_Rows<=$Per_Page)
    	{
    		$Num_Pages =1;
    	}
    	else if(($Num_Rows % $Per_Page)==0)
    	{
    		$Num_Pages =($Num_Rows/$Per_Page) ;
    	}
    	else
    	{
    		$Num_Pages =($Num_Rows/$Per_Page)+1;
    		$Num_Pages = (int)$Num_Pages;
    	}
    
    
    	$strSQL .=" order  by id ASC LIMIT $Page_Start , $Per_Page";
    	$objQuery  = mysql_query($strSQL);
    
    	
    	?>
    	<table width="600" border="1">
    	  <tr>
    		<th width="91"> <div align="center">ID </div></th>
    		<th width="98"> <div align="center">Tags </div></th>
    		<th width="198"> <div align="center">Url </div></th>
    		<th width="97"> <div align="center">Enable </div></th>
    		
    	  </tr>
    	<?
    	while($objResult = mysql_fetch_array($objQuery))
    	
    	{
    	?>
    	  <tr>
    		<td><div align="center"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?=$objResult["id"];?>"></div></td>
    		<td><?=$objResult["tags"];?></td>
    		<td><?=$objResult["url"];?></td>
    		<td><div align="center"><?=$objResult["enable"];?></div></td>
    		
    	  </tr>
    	<?
    	}
    	?>
    	<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
    	</table>
    	<br>
    	
    	
    	
    	<?
    // Check if delete button active, start this
    if($delete){
    for($i=0;$i<$count;$i++){
    $del_id = $checkbox[$i];
    $sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
    $objQuery = mysql_query($sql);
    }
    
    // if successful redirect to search.php
    if($objQuery){
    echo "<meta http-equiv=\"refresh\" content=\"0;URL=search.php\">";
    }
    }
    	
    ?>	
    	
    
    	Total <?= $Num_Rows;?> Record : <?=$Num_Pages;?> Page :
    	<?
    	if($Prev_Page)
    	{
    		echo " <a href='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page&txtKeyword=$_GET[txtKeyword]'><< Back</a> ";
    	}
    
    	for($i=1; $i<=$Num_Pages; $i++){
    		if($i != $Page)
    		{
    			echo "[ <a href='$_SERVER[SCRIPT_NAME]?Page=$i&txtKeyword=$_GET[txtKeyword]'>$i</a> ]";
    		}
    		else
    		{
    			echo "<b> $i </b>";
    		}
    	}
    	if($Page!=$Num_Pages)
    	{
    		echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page&txtKeyword=$_GET[txtKeyword]'>Next>></a> ";
    	}
    
    	}	
    	?>
    </body>
    </html>
    
    Code (markup):
     
    SharingEngines, Nov 26, 2010 IP
  2. learnerabn

    learnerabn Peon

    Messages:
    131
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you have used if($delete){ but u didnt assigned any value for $delete and in your case i think it wont accept if you give $delete = $_POST['delete'] since delete is the submit button so you to have give a hidden field like <input type="hidden" name="chk_del" value="1"> infront of the delete (submit)button and instead of checking if($delete) u have to check if(isset($_POST['chk_del'])) it will work well then hope so...since i'm in rush i cant execute the code...u test execute if any issues araise u post here...
     
    learnerabn, Nov 26, 2010 IP
  3. drctaccess

    drctaccess Peon

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    0
    #3
    1. Before this line

    "// Check if delete button active, start this"

    add the code below

    $delete = null;
    $checkbox = array();

    if(isset($_POST['delete']))
    {
    $delete = $_POST['delete'];
    $checkbox = $_POST['checkbox'];
    }

    2. find the lines below:

    $del_id = $checkbox[$i];
    $sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
    $objQuery = mysql_query($sql);


    and replace with lines below

    $del_id = $checkbox[$i];
    if($del_id > 0)
    {
    $sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
    $objQuery = mysql_query($sql);
    }

    Enjoy!
     
    drctaccess, Nov 26, 2010 IP
  4. SharingEngines

    SharingEngines Member

    Messages:
    83
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    43
    #4
    You are the best!

    Works like a charm!

    THANK YOU for your great help!
     
    SharingEngines, Nov 26, 2010 IP