如何使用复选框从mysql中删除多行?

时间:2014-05-28 17:21:30

标签: php mysql

我尝试删除“admin”数据库中的数据,但删除按钮不起作用。

这是我的上半部分

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

这是我的复选框代码

<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>

,这是我的按钮代码

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

这是我的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();
?>

4 个答案:

答案 0 :(得分:7)

包含<form>代码中的所有输入元素:<form> all inputs are here </form>

更新

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

to(id在这里没关系):

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

和你的按钮代码:

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

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

<form>标记设置为<form action="delete.php" method="post">

注意: 我假设下面的代码在delete.php文件中。如果没有在上面的开头表格标签中用该名称替换“delete.php”。

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

注意: 由于mysql_将来会弃用,更好的是使用mysqli扩展。但在使用之前,您必须在服务器上启用它。 mysqli是php的一部分,而较新版本的php有它但没有启用。要启用此功能,请查看php信息页面,并在该页面的“已加载配置文件”行中找到php.ini文件的路径。 您可以通过在浏览器中加载以下php文件来查看php信息页面:

<?php
 phpinfo();
?>

在文本编辑器中打开该php.ini文件并取消注释或在扩展名列表中添加一行extension=php_mysqli.dll。 还搜索“extension_dir”并打开它所说的目录并确保php_mysqli.dll文件存在。 (如果你不使用Windows操作系统,你可能有.so扩展名)

然后重新启动服务器,你就完成了!

Fred -ii-

  

将mysqli_与prepared statements一起使用确实更好   更安全的方法。但是,有些人甚至会建议PDO,甚至是PDO   没有mysqli_提供的一些功能;   奇怪的是。甚至PDO也需要消毒。许多人认为使用PDO将解决注射问题,这是错误的。    - 谢谢Fred。

答案 1 :(得分:0)

试试这段代码。它运作良好。 的 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"; 

                     } 

                     } ?>

答案 2 :(得分:0)

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

答案 3 :(得分:-1)

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