0

I try to delete my data in "admin" database, but the delete button does not function.

This is my top part

<?php
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="admin"; // Database name 
$tbl_name="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);
?>

This is my checkbox code

<tbody>
<?php
    while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $rows['course_code']; ?></td>
<td><?php echo $rows['course_name']; ?></td>
<td><?php echo $rows['lecture_id']; ?></td>
<td><input name="checkbox[]" type="checkbox"
    id="checkbox[]" value="<?php echo $rows['course_code'];?>"></td>
<td><form>
</form>
</td>
</tr>
<?php
    }
?>
</tbody>

and, this is my button code

<input type='button' id="delete" value='Delete' name='delete'>

This is my php function code

<?php
if(isset($_POST['delete'])){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE course_code='$del_id'";
$result = mysql_query($sql);
}
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete.php\">";
}
}
mysql_close();
?>
4
  • Your input is outside your <form> tag; first major issue. Plus, there's no action or method set to the file that's supposed to do the work. I.e.: <form action="delete.php"... method = "post" Commented May 28, 2014 at 17:23
  • correct this put your input files inside the form <td><form> </form> Commented May 28, 2014 at 17:25
  • 1
    Consult this Q&A on SO which is similar to yours stackoverflow.com/q/14475096 you will find your answer there. This is the one I started off with, and has helped me a lot in the past. Commented May 28, 2014 at 17:25
  • Plus, form method defaults to GET if not set. Do <form action="delete.php" method="post"> - You now have enough information to fix this yourself. Commented May 28, 2014 at 17:29

4 Answers 4

8

include all the input elements within your <form> tags: <form> all inputs are here </form>

update:

<input name = "checkbox[]" type="checkbox"  id="checkbox[]" value="<?php echo     $rows['course_code'];?>">

to (id doesn't matter here):

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

and your button code:

<input type='button' id="delete" value='Delete' name='delete'>

to

<input type="submit" value="Delete"/>

set opening <form> tag to <form action="delete.php" method="post">

Note: I assume below codes are in delete.php file. if not replace "delete.php" with that name in above opening form tag.

your delete.php file:

<?php
$cheks = implode("','", $_POST['checkbox']);
$sql = "delete from $tbl_name where course_code in ('$cheks')";
$result = mysql_query($sql) or die(mysql_error());
mysql_close();
?>

Note: Since mysql_ will deprecate on future, better is use mysqli extension. But before use that, you have to enable it on your server. mysqli is a part of php and newer version of php has it but not enabled. To enable this, view php info page and find the path of php.ini file in "Loaded Configuration File" row on that page. You can see php info page by loading below php file in the browser:

<?php
 phpinfo();
?>

open that php.ini file in a text editor and un-comment or add a line extension=php_mysqli.dll at the extensions list there. also search for "extension_dir" and open the directory it says and make sure php_mysqli.dll file is there. (you may have .so extension if you not use windows OS)

Then restart your server and you are done!

By Fred -ii-

Using mysqli_ with prepared statements is indeed a better and safer method. However, some will even suggest PDO, but even PDO doesn't have some of the functionalities that mysqli_ offers; strangely that. Even PDO needs sanitization. Many think that using PDO will solve injection issues, which is false. -Thanks Fred.

Sign up to request clarification or add additional context in comments.

5 Comments

and 1+ for not using loops :)
Simple and elegant, my inner code monkey is delighted.
mysql_ deprecation notice would probably yield even more "bonus" points, as well as SQL and XSS injection. ;-)
Using mysqli_ with prepared statements is indeed a better and safer method. However, some will even suggest PDO, but even PDO doesn't have some of the functionalities that mysqli_ offers; strangely that. Even PDO needs sanitization. Many think that using PDO will solve injection issue, which is false. Many will either not upvote or will downvote a good answer, when not mentioning the mysql_ deprecation notice, thought you'd like to know ;-)
@Fred-ii- Thanks for telling about PDO comparing with mysqli extension. I updated the answer.
-1

try this code. it is working well. connection.php

<?php $hostname_conection = "localhost"; /* this is the server name(assigned to variable) which is localhost since it runs on local machine */ 
$database_conection = "company"; /* this is the database name( assigned to variable)*/ 
$username_conection = "root"; /* user name (assigned to variable)*/
$password_conection = ""; /*password (assigned to variable) */ 
$conection = mysql_connect($hostname_conection, $username_conection, $password_conection) or trigger_error(mysql_error(),E_USER_ERROR); /* Mysql_connect function is used to conncet with database it takes three parameters server/hostname, username,and password*/ 
mysql_select_db($database_conection,$conection) or die(mysql_error("could not connect to database!")); /* Mysql_select is used to select the database it takes two parameters databasename and connection variable in this case $conection */ 
?>

multiple_delete.php

<?php require_once('conection.php'); ?> 
<?php 
in
/* now to display the data from the database which we inserted in above form we */ /* we make the query to select data from the table EMP */ 
            $display = "select * from test_mysql";
        $result = mysql_query($display, $conection) or die(mysql_error()); /* the query is executed and result of the query is stored in variable $result */ 
        if ($result == FALSE) {
            die(mysql_error()); /* displays error */
        } ?> <h1 align="center"> Displaying Recods in Table </h1> 
        <form method="get" action="" id="deleteform" > 
            <table width="245" border="1" align="center"> 
                <tr>
                    <td width="51">
                        <input type="submit" name="delete" id="button" value="delete" onclick="document.getElementById('deleteform').action = 'delete.php';document.getElementById('deleteform').submit();"/> <!--- here on clicking the button the form is submitted and action is set to delete.php Here we have used javaScript document refers to this whole page and now we can access any tag that has its id with help of getElementById() method and after the we specify the operation we want to perform in this case action and submit. ---> 
                    </td> 
                    <td width="50">id</td> 
                    <td width="55">name</td>
                    <td width="47">lastname</td>
                </tr>
 <?php 
 while ($rows = mysql_fetch_array($result)) 
        { /* here we make use of the while loop which fetch the data from the $result int array form and stores in $row now we can display each field from the table with $row[‘field_name’] as below */ 
     ?> 
                <tr>
                    <td>
                        <input type="checkbox" name="empids[]" value="<?php echo $rows['id']; ?>" /> <!--here with each checkbox we send the id of the record in the empids[] array ---> 
                    </td>
                    <td>
                        <?php echo $rows['id'] ?>
                    </td>
                        <td>
                            <?php echo $rows['lastname'] ?>
                        </td>
                        <td><?php echo $rows['name'] ?></td> 
                            <?php } ?>
                </tr>
            </table>
        </form> ?>
    </body>
</html>

delete.php

<?php 
require_once('conection.php'); 
?>
 <?php
 if (isset($_GET['delete'])) /* checks weather $_GET['delete'] is set*/
     {
     if (isset($_GET['empids'])) /* checks weather $_GET['empids'] is set */ 
         { 
         $checkbox = $_GET['empids']; /* value is stored in $checbox variable */ 
         if (is_array($checkbox)) 
             { 
             foreach ($checkbox as $key => $your_slected_id) /* for each loop is used to get id and that id is used to delete the record below */ 
                 { 
                 $q="DELETE FROM test_mysql WHERE id=$your_slected_id "; /* Sql query to delete the records whose id is equal to $your_slected_id */
                 mysql_query($q,$conection) ; /* runs the query */ 

                 } 
                 header("location:multiple_delete.php"); /* Goes back to index.php */ 

                 } 

                 } else 
                     { 
                     echo" you have not selected reords .. to delete"; 

                     } 

                     } ?>

Comments

-1
$sql = "SELECT * FROM blacklist";
$result = $link->query($sql);
$count=mysqli_num_rows($result);

if ($result->num_rows > 0) {
 while($row = $result->fetch_assoc()) 
{
echo "<table>";
echo "<th>";
echo    "<td>" . "ID: " . $row["id"]."</td>";
echo    "<td>" . " Dial Target: " . $row["dial_target"]."</td>";
echo    "<td>" . " Destination: " . $row["pozn"]."</td>";
echo    "<td>" . " Date: " . $row["block_date"] . "</td>";
echo    "<td>" . "<div class='background' style='position: relative; top:8px;'>" . "<form>" . "<input action='index.php' method='post' type='checkbox' name='chechbox[]' value='".$row["id"]."'/>" ."</form>" . "</div>" . "</td>";
echo "</th>";
echo "</table>"; 
echo "</br>";
}
}
else
{
 echo "0 results";
}


if(isset($_POST['Delete']))
{
for($i=0;$i<$count;$i++)
{
$del_id = $checkbox[$i];
 $del = "DELETE FROM blacklist WHERE Delete='$del_id'";
$result = $link->query($del);
}
if($result)
{
echo "<meta http-equiv=\"refresh\" content=\"0;URL=index.php\">";
}
}

<!-- DELETE BUTTON -->
<form>
<input type='Submit' id="Delete" value='Delete' name='Delete'/>
</form>

1 Comment

BUT my button still does not function
-2
                         <?php
                            $args1 = array(
                                'role' => 'Vendor',
                                'orderby' => 'user_nicename',
                                'exclude' => $user_id.',1',
                                'order' => 'ASC'
                            );
                            $subscribers = get_users($args1);                                        foreach ($subscribers as $user) {
                                $fvendorck = $wpdb->get_row("select * from wp_vandor where parent_id = '".$user_id."' and child_id = '".$user->id."'");
                                $isfavvendor = $fvendorck->child_id;
                             if(!empty($isfavvendor)) {
                             ?>
                                <li><input type="checkbox" id="listID" value='<?php echo $user->id; ?>' name="chk1[]" checked=""/><?php echo $user->headline; ?></li>

                        <?php  }else{ ?>

                                <li><input type="checkbox" id="listID" value='<?php echo $user->id; ?>' name="chk1[]" /><?php echo $user->headline; ?></li>

                        <?php } }?>
                    </ul>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.