Page 1 of 1

Delete Buuton

Posted: Sat Nov 22, 2014 9:31 am
by crack
Hi guys am now learning php coding so i decided to create a form wen submitted to the database and retrieved back ,you could be able to delete a specific row by the click of a button. So before you delete you click on the radio button beside the specific you to delete to indicate that this is the row i want to delete and then click on the delete button to finally delete. But my difficulty is i cant seem to make the button delete the specific row or any row at all when its been clicked. Also i dont not know what to match with my ID in the delete query.
please do notify me if there are any errors restricting my code from functioning. Thank!!


<body>

Code: Select all

  <?php 
      
        $con = mysqli_connect ("localhost", "rupert", "" , "example") or die ("Database connect error");
        mysqli_select_db($con, "example") or die ("error connecting to database");
      
	   $query = "SELECT * FROM validation";
       $result = mysqli_query($con , $query);   
	   $rows = mysqli_fetch_assoc($result);
	   $totals = mysqli_num_rows($result);
	   
     ?> 
            <div id="css">
    <form action="<?php htmlentities ($_SERVER['PHP_SELF'])?>" method="post" > 
    
     
       <table width="80%" border="0" cellpadding="2" cellspacing="2" > 
          <caption><h2>Personal Details of Customers</h2></caption> 
          <tr class="white"> 
              <td bgcolor="#330033">&nbsp;</td>
              <td bgcolor="#330033"> Id Number </td>
              <td bgcolor="#330033"> Full Name </td>
              <td bgcolor="#330033"> Email Address </td>
              <td bgcolor="#330033"> Website </td>
              <td bgcolor="#330033"> Comment </td> 
              <td bgcolor="#330033"> Time </td>   
     
</tr>  

  
       <?php while($rows=mysqli_fetch_assoc($result)) 
	   { 
	     ?>
      
     <tr>	 
	  <td><input type="radio" name="ID" value="<?php $rows['ID']?>;"/></td> 
      
	  <td bgcolor="#FFFFCC"><?php echo $rows['ID'];?></td> 
      <td bgcolor="#FFFFCC"><?php echo $rows['Name'];?></td> 
      <td bgcolor="#FFFFCC"><?php echo $rows['Email'];?></td>
      <td bgcolor="#FFFFCC"><?php echo $rows['Website'];?></td> 
      <td bgcolor="#FFFFCC"><?php echo $rows['Comment'];?></td> 
      <td bgcolor="#FFFFCC"><?php echo $rows['Time'];?></td>   
      <td>&nbsp;</td> 
      
      
       </tr> 
       <?Php
       }
       ?>     
       <td><input type="submit" name="del" value="delete" /></td>
        
        </table>
   </form>
  </div>
 
     <?php  
  if (isset($_POST['del'])) {  
            $con = mysqli_connect ("localhost", "rupert", "" , "example") or die ("Database connect error");
           mysqli_select_db($con, "example") or die ("error connecting to database");
      
      
	   $query1 = "DELETE FROM validation where ID =  ";
	   $result1 = mysqli_query($con,$query1); 
	     
	  }
  
 ?>
</body>
</html>

Re: Delete Buuton

Posted: Sat Nov 22, 2014 9:42 am
by Celauran
You have named the radio button 'ID', so you want to look for $_POST['ID'].

Re: Delete Buuton

Posted: Sat Nov 22, 2014 9:43 am
by Celauran
As an aside, you don't want to just drop that in the query. Sanitize it first, or look into using prepared statements.

Re: Delete Buuton

Posted: Sat Nov 22, 2014 9:55 am
by crack
so query will be something like this $query1 = "DELETE FROM validation where ID =" .$_POST['ID']." "
but its still not deleting...

Re: Delete Buuton

Posted: Sat Nov 22, 2014 9:58 am
by Celauran
Have you checked the value of $_POST['ID']? Have you checked mysqli_error?

Re: Delete Buuton

Posted: Sat Nov 22, 2014 10:00 am
by Celauran

Code: Select all

value="<?php $rows['ID']?>;"
You need an echo in there, and your ; is in the wrong place.

Code: Select all

value="<?= $rows['ID']; ?>"

Re: Delete Buuton

Posted: Sat Nov 22, 2014 10:44 am
by crack
It worked now after adding echo and mysqli
Thank you much!!!
But there is a problem i cant seem to insert data to my database anymore. Even when i create a row manually in my database i cant retrieve the row and then also although i have deleted the rows when i insert from my database it does not start from number 1 but continues from the number it last ended.