can't delete records..
here's the codes..

<?php


mysql_connect('localhost','root','');

mysql_select_db('records');

 

?>

<?php


if(isset($_GET['btndelete'])){

$position = $_GET['position'];

if(isset($position)){

foreach($position as $val){

mysql_query( "DELETE FROM positions WHERE id='".$val."'" );

}
}

}


?>
<html>

      <head>

      <tittle>

      confirm

      </title>

      <script>

  function deleterec(){

      if(confirm("Are you sure you want to delete?")){  

     return true;

      }else{

      return false;

      }

      }

     </script>

      </head>

      <body>

      <form name='frm' method='get' onsubmit='return deleterec()'>

      <table border='1'>



      <?php
         mysql_connect('localhost','root');
  
      mysql_select_db('records');

      $result = mysql_query("SELECT * FROM positions") 
        or die(mysql_error());
      while($row = mysql_fetch_array( $result )) {
        
        

        echo '<td><center><td>'<input type='checkbox' name='position' >'</tr></td>'. $row['position'] . '</td>';
      }
      echo "</table>";
     ?>



      <input type='submit' name='btndelete' value='delete' />

      </form>

      </body>

      </html>

Recommended Answers

All 2 Replies

Have you ever tried to echo the results of $position and $val as $position determines $val and $val accounts for an id value which in turn accounts for what is to be deleted, it's good to know if the chain is broken. By the way, you should let 'HTML deal with HTML and PHP deal with PHP'. Maybe you can change your code on lines 39-53 to:

<?php
      mysql_connect('localhost','root');
      mysql_select_db('records');

      $result = mysql_query("SELECT * FROM positions") 
      or die(mysql_error());
      while($row = mysql_fetch_array( $result )) {
        
      ?>

        <tr><td>
<input type="checkbox" name="position" value="<?php  echo $row['position']; ?>" />
</td></tr>
     <?php }?>
     </table>

Why the use of the get method, do you really need the values sent to the url? Unless you are going to use those values for other purposes, you don't want to be spreading data where not necessary. $_POST method works just fine I believe.

commented: Correct. +6

hey takeshi
just change line 50
from

echo '<td><center><td>'<input type='checkbox' name='position' >'</tr></td>'. $row['position'] . '</td>';

to

echo "<td><center><td><input type='checkbox' name='position[]' value='".$row['id']."' ></tr></td>". $row['position'] . "</td>";

the problem there is you forgot to set name to an array, position[], and try to use qoutes properly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.