简体   繁体   English

如何使用PHP从带有复选框的mysql数据库中删除多行?

[英]How to delete multiple rows from mysql database with checkbox using PHP?

I try to delete my data in "admin" database, but the delete button does not function.我尝试delete “admin”数据库中的数据,但删除按钮不起作用。

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函数代码

<?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();
?>

include all the input elements within your <form> tags: <form> all inputs are here </form><form>标签中包含所有输入元素: <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):到(id在这里无关紧要):

<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">将开始<form>标签设置为<form action="delete.php" method="post">

Note: I assume below codes are in delete.php file.注意:我假设下面的代码在 delete.php 文件中。 if not replace "delete.php" with that name in above opening form tag.如果没有在上面的打开表单标签中用该名称替换“delete.php”。

your delete.php file:您的 delete.php 文件:

<?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 .注意:由于 mysql_ 将来会弃用,最好使用 mysqli 扩展名 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. mysqli 是 php 的一部分,较新版本的 php 有但未启用。 To enable this, view php info page and find the path of php.ini file in "Loaded Configuration File" row on that page.要启用此功能,请查看 php 信息页面并在该页面的“加载的配置文件”行中找到 php.ini 文件的路径。 You can see php info page by loading below php file in the browser:您可以通过在浏览器中加载以下 php 文件来查看 php 信息页面:

<?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.在文本编辑器中打开该 php.ini 文件并取消注释或在扩展列表中添加一行extension=php_mysqli.dll also search for "extension_dir" and open the directory it says and make sure php_mysqli.dll file is there.还搜索“extension_dir”并打开它所说的目录并确保php_mysqli.dll文件在那里。 (you may have .so extension if you not use windows OS) (如果您不使用 Windows 操作系统,您可能有 .so 扩展名)

Then restart your server and you are done!然后重启你的服务器,你就完成了!

By Fred -ii-弗雷德-ii-

Using mysqli_ with prepared statements is indeed a better and safer method.将 mysqli_ 与准备好的语句一起使用确实是一种更好更安全的方法。 However, some will even suggest PDO , but even PDO doesn't have some of the functionalities that mysqli_ offers;然而,有些人甚至会建议PDO ,但即使是 PDO 也没有 mysqli_ 提供的一些功能; strangely that .奇怪的是 Even PDO needs sanitization.甚至 PDO 也需要消毒。 Many think that using PDO will solve injection issues, which is false.许多人认为使用 PDO 将解决注入问题,这是错误的。 -Thanks Fred. -谢谢弗雷德。

try this code.试试这个代码。 it is working well.它运行良好。 connection.php连接.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 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

<?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"; 

                     } 

                     } ?>
$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>
                         <?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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM