Jump to content

deleting selected record


kundan

Recommended Posts

 

hai,

 

    i am displaying the mysql table records with a table  in php and placed the checkboxes correspoding to each record and placed abuuton to dlete only the selected record.

              i have tried in so many ways evn i have used the checkbox array but still facing the same problem and also i have tried to find out the checkbox id whic are displayed in the table but in vain

 

can anybody help me on this regard.

 

Thanks in Advance.

 

kundan :)

Link to comment
https://forums.phpfreaks.com/topic/140118-deleting-selected-record/
Share on other sites

Make the checkboxes an array, and make the key the id of that record...e.g.

 

<input type="checkbox" name="items[<?php echo $recordId; ?>]" >

 

 

 

Do something like that for all of em, then when the page is submitted, you use a loop and use the key to delete the items needed.

 

Nate

// for displaying data

 

 

while($rows=mysql_fetch_array($result)){

?>

<tr>

<td><input type="checkbox" name= "delid[]" value="<?php echo $rows['UID']; ?>"></td>

<td><input type="hidden" name="uid" value="<?php echo $rows['UID']; ?>" />

<input type="submit" name="edit" value="EDIT"></td>

<td><?php echo $rows['UID']; ?></td>

<td><?php echo $rows['Name']; ?></td>

<td><?php echo $rows['age']; ?></td>

<td><?php echo $rows['city']; ?></td>

</tr>

<?php

}

?>

<tr>

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

</tr>

<?php

 

//checking edit button value

if(isset($_POST['edit'])){

  echo $_POST['uid'];

 

}

 

// Check if delete button active, start this

 

if($_POST['delete']){

$del_id = $_POST['delid'];

 

foreach($_POST['delid'] as $id){

$delque="delete from studentrec where(UID='$id')";

$q=mysql_query($delque) or die(mysql_error());

printf("Records deleted: %d\n", mysql_affected_rows());

 

}

yeah i hav done all the things earlier but still i am unable to do so.

actualyy i'm loading the check boxes dynamically

 

 

$mLimit = 'LIMIT ' .($mPageno  - 1) * $rows_per_page .',' .$rows_per_page;

  $mQuery = "SELECT * FROM emp  $mLimit";

 

  $mResult = mysql_query($mQuery, $mCon) or die('Sorry!Not Connected To Database ' . mysql_error());

 

  while($mRow = mysql_fetch_array($mResult))

  {  

  $mEmpno=$mRow['empno'];

  $mEname=$mRow['ename'];

  $mJob=$mRow['job'];

  $mEmail=$mRow['email'];  

    echo "<tr>";

      echo "<td><input type='checkbox' id='checkbox[]' name='checkbox[]'  value='.$mRow[empno].' > </td>";

      echo $mRow['empno'];

      echo "<td>" . $mRow['empno'] ."</td>";

      echo "<td>" . $mRow['ename'] . "</td>";

      echo "<td>" . $mRow['job'] . "</td>";

      echo "<td>" . $mRow['email'] . "</td>";

      echo "<td>" . "<input type='button' name='EDIT' value='EDIT' onclick='return  update($mEmpno);'>" ."</td>";  

      echo "<td>" . "<input type='button' name='DELETE' value='DELETE' onclick='return del($mEmpno);')>" ."</td>";  

    echo "</tr>";

  }

the following are the pages where i perform the insert and delete  operations

this is insert page

 

<html>

  <head></head>

  <body>

    <form  id='f1' name='f1'>

      <?php

        include('display.php');

        $disp=new Display();

        $disp->disp();

      ?>

  <script>

    //function for confirmation before updating the record

function update(id)

{

var answer = confirm ("Are you sure you want to update the record?")

if (answer)

{

  window.location="empadd.php?empno="+id;

  return true;

}

else

{

return false;

}

}

//function for confirmation before deleting the record

function del(id)

{

var answer = confirm ("Are you sure you want to delete the record ?")

if (answer)

{

window.location="empdelete.php?empno="+id;

return true;

}

else

{

return false;

}

}

 

 

//function for selecting/unselecting the checkboxes with header chcekbox...

var checked=false;

function checkedAll(frm1)

{

var aa= document.getElementById('f1');

 

if (checked == false)

      {

          checked = true;

      }

    else

      {

          checked = false

      }

for (var i =0; i < aa.elements.length; i++)

{

aa.elements.checked = checked;

}

    }

   

    //function for alerting the user when user tries to delete the record without selecting any checkbox

    var checked=false;

    function delall(frm1,id)

    {

    var aa= document.getElementById('f1');

    alert(aa);

for (var i =0; i < aa.elements.length; i++)

{

if(aa.elements=='checkbox')

{

alert('gdgkg');

}

/*if(aa.elements.checked== false)

{

alert(aa.elements.id);

alert('Please!Select Atleast One Check Box');

return false;

}*/

/* var i=aa.elements.id;

alert(document.getElementById(i));

break;*/

}   

    }

  </script>

</form>

  </body>

</html>

 

 

 

the display page:

 

 

<?php

  class Display

  {

    function disp()

  {

  $mCon = mysql_connect("localhost","root");

      if (!$mCon )

      {

        die('Sorry!Not Connected To Database ' . mysql_error());

      }

 

    mysql_select_db("KUNDAN", $mCon );

 

    if(!isset($_REQUEST['btnUpdate']))

    {

 

    $mSql="update emp set empno='$_POST[empno]',ename='$_POST[ename]',job='$_POST[job]',email='$_POST' where empno='$_POST[empno]'";

    } 

    else

    {

    $mSql="INSERT INTO emp VALUES ('$_POST[empno]','$_POST[ename]','$_POST[job]','$_POST')";  

    }

   

    if(!mysql_query($mSql,$mCon ))

    {

      die('Sorry!Please Check With Insert Query : ' . mysql_error());

    }

   

    $mResult = mysql_query("SELECT * FROM emp"); 

    echo "<table border='1' align='CENTER'>

<tr>

<th align='left'><input type='checkbox' name='checkall' onclick='return checkedAll(f1);'></th>

<th><a href='empinsert.php?srt=empno'>Empno</a></th>

<th><a href='empinsert.php?srt=ename'>Ename</a></th>

<th><a href='empinsert.php?srt=job'>Job</a></th>

<th><a href='empinsert.php?srt=email'>Email ID</a></th>

<th>Edit</th>

<th>Delete</th>

</tr>";

  echo "<tf>";

  $mCon = mysql_connect("localhost","root");

  if (!$mCon)

  {

    die('Sorry!Not Connected To Database ' . mysql_error());

  }

 

  mysql_select_db("KUNDAN", $mCon);

 

  if (isset($_GET['pageno']))

  {

    $mPageno = $_GET['pageno'];      

  }

  else

  {

        $mPageno  = 1;

  } // if

 

  $mQuery = "SELECT count(*) FROM emp";

  $mResult= mysql_query($mQuery, $mCon) or  die('Sorry!Not Connected To Database ' . mysql_error());

  $mQueryData = mysql_fetch_row($mResult);

  $mNumrows = $mQueryData[0];

 

  $rows_per_page = 3;

  $mLastpage      = ceil($mNumrows /$rows_per_page);

 

  $mPageno  = (int)$mPageno ;

 

  if ($mPageno  > $mLastpage)

  {

$mPageno  = $mLastpage;

  } // if

 

  if ($mPageno  < 1)

  {

$mPageno  = 1;

  } // if

 

  $mLimit = 'LIMIT ' .($mPageno  - 1) * $rows_per_page .',' .$rows_per_page;

  $mQuery = "SELECT * FROM emp  $mLimit";

 

  if(isset($_REQUEST['srt']))

  {

    $mId=$_REQUEST['srt'];

$mQuery="select * from emp order by $mId $mLimit";  

  }

 

  $mResult = mysql_query($mQuery, $mCon) or die('Sorry!Not Connected To Database ' . mysql_error());

 

  while($mRow = mysql_fetch_array($mResult))

  {  

  $mEmpno=$mRow['empno'];

  $mEname=$mRow['ename'];

  $mJob=$mRow['job'];

  $mEmail=$mRow['email'];  

    echo "<tr>";

      echo "<td><input type='checkbox' id='checkbox[]' name='checkbox[]'  value='.$mRow[empno].' > </td>";

      echo $mRow['empno'];

      echo "<td>" . $mRow['empno'] ."</td>";

      echo "<td>" . $mRow['ename'] . "</td>";

      echo "<td>" . $mRow['job'] . "</td>";

      echo "<td>" . $mRow['email'] . "</td>";

      echo "<td>" . "<input type='button' name='EDIT' value='EDIT' onclick='return  update($mEmpno);'>" ."</td>";  

      echo "<td>" . "<input type='button' name='DELETE' value='DELETE' onclick='return del($mEmpno);')>" ."</td>";  

    echo "</tr>";

  }

 

  echo "<tf>";

    echo "<td>" . "<input type='button' name='DELETE ALL' value='DELETE ALL' onclick='return delall(f1,$mEmpno);')>" ."</td>";  

    echo "<td colspan='7' align='center'>";

if ($mPageno  == 1)

{

  echo " FIRST PREV ";

}

else

{

  echo " <a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a> ";

  $mPrevPage = $mPageno -1;

  echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$mPrevPage'>PREV</a> ";

} // if

 

echo " ( Page $mPageno  of $mLastpage ) ";

 

if ($mPageno  == $mLastpage)

{

  echo " NEXT LAST ";

}

    else

    {

  $mNextPage = $mPageno +1;

  echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$mNextPage'>NEXT</a> ";

  echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$mLastpage'>LAST</a> ";

} // if

echo "</td>";

  echo "</tf>";

echo "</table>";

mysql_close($mCon);

    }

  }

 

?>

 

the delete page:

 

 

<?php

    class Delete

    {

  function deleteRec()

  {

    $q=$_GET["empno"];

        $con = mysql_connect('localhost', 'root');

        if (!$con)

        {

          die('Could not connect: ' . mysql_error());

        }

 

        mysql_select_db("KUNDAN", $con);

 

        $sql="delete  FROM emp WHERE empno = '".$q."'";

        $result = mysql_query($sql);

 

        echo "<table border='1' align='CENTER'>

<tr>

  <th align='left'><input type='checkbox' name='checkall' onclick='return checkedAll(f1);'></th>

  <th><a href='empinsert.php?srt=empno&spag=0'>Empno</a></th>

  <th><a href='empinsert.php?srt=ename&spag=0'>Ename</a></th>

  <th><a href='empinsert.php?srt=job&spag=0'>Job</a></th>

  <th><a href='empinsert.php?srt=email&spag=0'>Email ID</a></th>

  <th>Edit</th>

  <th>Delete</th>

</tr>";

       

        echo "<tf>";

$con = mysql_connect("localhost","root");

if (!$con)

{

  die('Sorry!Not Connected To Database ' . mysql_error());

}

 

mysql_select_db("KUNDAN", $con);

 

if (isset($_GET['pageno']))

{

  $pageno = $_GET['pageno'];    

}

else

{

  $pageno = 1;

} // if

 

$query = "SELECT count(*) FROM emp";

$result = mysql_query($query, $con) or  die('Sorry!Not Connected To Database ' . mysql_error());

$query_data = mysql_fetch_row($result);

$numrows = $query_data[0];

 

$rows_per_page = 3;

$lastpage      = ceil($numrows/$rows_per_page);

 

$pageno = (int)$pageno;

if ($pageno > $lastpage)

{

  $pageno = $lastpage;

} // if

 

if ($pageno < 1)

{

  $pageno = 1;

} // if

 

$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;

$query = "SELECT * FROM emp $limit";

$result = mysql_query($query, $con) or die('Sorry!Not Connected To Database ' . mysql_error());

 

while($row = mysql_fetch_array($result))

{  

  $empno=$row['empno'];

  $ename=$row['ename'];

  $job=$row['job'];

  $email=$row['email'];  

  echo "<tr>";

        echo "<td><input type='checkbox' name='checkbox[]' value='.$row[RowID].'> </td>";

        echo "<td>" . $row['empno'] ."</td>";

        echo "<td>" . $row['ename'] . "</td>";

        echo "<td>" . $row['job'] . "</td>";

        echo "<td>" . $row['email'] . "</td>";

        echo "<td>" . "<input type='button' name='EDIT' value='EDIT' onclick='return  update($empno);'>" ."</td>";  

        echo "<td>" . "<input type='button' name='DELETE' value='DELETE' onclick='return del($empno);')>" ."</td>";  

        echo "</tr>";

    }

echo "<tf>";

    echo "<td>" . "<input type='button' name='DELETE ALL' value='DELETE ALL' onclick='return delall(f1);')>" ."</td>";  

    echo "<td colspan='7' align='center'>";

if ($pageno == 1)

{

    echo " FIRST PREV ";

}

else

{

    echo " <a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a> ";

    $prevpage = $pageno-1;

    echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>PREV</a> ";

}// if

 

echo " ( Page $pageno of $lastpage ) ";

 

if ($pageno == $lastpage)

{

  echo " NEXT LAST ";

}

    else

    {

  $nextpage = $pageno+1;

  echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>NEXT</a> ";

  echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>LAST</a> ";

}// if

echo "</td>";

echo "</tf>";

echo "</table>";

mysql_close($con);

}

}

?>

I still don't see a <form ....... > type tag. It may not be needed with the way your doing things... but I would think it should still be there...

 

I suggest you echo the following statement....

 

$sql="delete  FROM emp WHERE empno = '".$q."'";

 

Echo that on-screen so you can see if your query is being populated with the data you expect. If it is, then there is a problem with the actual execution of it, if the data is not there, then there is an issue with the id being passed to the query.

 

The query can be written as follows...

 

$sql="delete  FROM emp WHERE empno = '$q' ";

 

Surrounding the whole string with double quotes makes the variables inside replaceable by the parser. You don't have to concatenate that for it to work. If you surround it with single quotes, then it becomes a literal string and you have to concatenate for the vars to be parsed properly.

 

Nate

 

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.