-1

While I am new to php i am trying to figure out what the error is while using delete operation i have a database table "local_admin" and the tuples are constituency, username and password with constituency set as primary key. The code is given below and also the data table is not populated in the page.Any help is greatly appreciated. Thanx.

    <?php

    session_start(); 
    include("header.php"); 
    $logout="You have successfully Loged out! Please log in to continue";
    if(!isset($_SESSION['username']))
        header("location:masteradmin.php");
    else
        $id=$_SESSION['username'];

    if(isset($_POST['logout']))
    {
    session_destroy();
    header("location:masteradmin.php");
    }

       $host="localhost"; // Host name 
       $username="root"; // Mysql username 
       $password=""; // Mysql password 
       $db_name="voting"; // Database name 
       $tbl_name="local_admin"; // 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";
      $result=mysql_query($sql);

      $count=mysql_num_rows($result);
      ?>
        <!--Content starts here-->
        <div class="content">

       <table width="400" border="0" cellspacing="1" cellpadding="0">
   <tr>
   <td><form name="form1" method="post" action="<?php echo   
                                                  $_SERVER['PHP_SELF']; ?>">
   <table width="400" border="0" cellpadding="3" cellspacing="1" 
                                                        bgcolor="#CCCCCC">
   <tr>
   <td bgcolor="#FFFFFF">&nbsp;</td>
   <td colspan="4" bgcolor="#FFFFFF"><strong>Delete Local Admin</strong>    
                                                                    </td>
                                                                    </tr>
   <tr>
   <td align="center" bgcolor="#FFFFFF">#</td>
   <td align="center" bgcolor="#FFFFFF"><strong>Constituency</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Username</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Password</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>

<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['constituency']; ?>"></td>
<td bgcolor="#FFFFFF"><? echo $rows['constituency']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['username']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['password']; ?></td>
</tr>

<?php
}
?>

<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>

<?php

// Check if delete button active, start this 

if(isset($_POST['delete']))
{
    $checkbox = $_POST['checkbox'];
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE name='$del_id'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php 
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=deletelocaladmin.php\">";
}
}
mysql_close();
?>

</table>
</form>
</td>
</tr>
</table>

</div>

<?php include("footer.php"); ?>
  • Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and consider using PDO, [it's not as hard as you think](http://jayblanchard.net/demystifying_php_pdo.html). Add error checking, such as `or die(mysql_error())` to your queries. Or you can find the issues in your current error logs.Quit suppressing messages with `@` – Jay Blanchard May 28 '15 at 15:13
  • 1
    Probably $checkbox is an empty string, and you're trying to treat it as an array. – Marc B May 28 '15 at 15:17
  • ok thanks for the error guide i will do that but can you point out the fault in line 80 undefined offset?.. – Mrityunjay May 28 '15 at 15:18
  • Can you point out line 80? – Jay Blanchard May 28 '15 at 15:18
  • $del_id = $checkbox[$i]; is the line 80 – Mrityunjay May 28 '15 at 15:28

1 Answers1

0

Besides the mysql extension recommendations above you should also validate your inputs. Offset error refers to the index not being part of the array

// load selected checkboxes or false
$checkbox = isset($_POST['checkbox'])?$_POST['checkbox']:false;

$result = false;
if(is_array($checkbox)) {
    for($i=0;$i<$count;$i++){
        if(isset($checkbox[$i])) { // the index may not exist
            // should be using PDO 
            $del_id = mysql_real_escape_string($checkbox[$i]); 
            // you are using constituency on the html checkbox value
            // but here you are using the name field?
            $sql = "DELETE FROM $tbl_name WHERE name='$del_id'";
            $result = mysql_query($sql);
        }
    }
}
  • Why would it not exist? It seems clear from the code that it should do. (ish) – Lightness Races in Orbit May 28 '15 at 16:34
  • corrected the name field and replaced with constituency but the problem still remains Notice: Undefined variable: checkbox in C:\wamp\www\testing\deletelocaladmin.php on line 77 – Mrityunjay May 28 '15 at 16:50
  • This can occur if none of the checkbox is checked. In line 77 you have "$checkbox = $_POST['checkbox'];" right? –  May 29 '15 at 17:18
  • I updated the answer to load the $_POST['checkbox']. Just replace your "$checkbox = $_POST['checkbox'];" by "$checkbox = isset($_POST['checkbox'])?$_POST['checkbox']:false;" –  May 29 '15 at 17:26