简体   繁体   中英

Can't delete rows in mysql

I'm using a form with multiple checkboxes to select several rows and delete all of them, the code I use for that is:

foreach ($_POST as $valor){
    $del_user = $_POST["borrar_usuario"];
    $q_borrar = "delete from usuarios where id_user in ('$del_user')";
    mysqli_query($con, $q_borrar);
    echo "usuario borrado";
    header('refresh: 3; url= exito.php');
}

But it does not delete anything. Any help, please?

These things?

foreach ($_POST["borrar_usuario"] as $valor){
    $del_user = $valor;
    $q_borrar = "delete from usuarios where id_user in ('$del_user')";
    mysqli_query($con, $q_borrar);
    echo "usuario borrado";
    header('refresh: 3; url= exito.php');
}

Ok since mysql_query accepts two parameter query and link you misplaced them, it should be:

mysqli_query($q_borrar, $con);

Also I would remove header() at the end of foreach since it will fire and there is a chance that not all data will be deleted.

This is all assuming your $deluser variable is ok and I would strongly suggest escaping all data before your query.

If post['borrar_usuario'] is array then may be you can implode the values in quotes of by

// applied mysql_real_escape string
$del_user = array_map( function($var){return mysql_real_escape_string($var); } ,$_POST["borrar_usuario"]);  
$delUserList = implode("','",$del_user)."'";
$q_borrar = "delete from usuarios where id_user in ($delUserList)"; // please check SQL before executing
mysqli_query($con, $q_borrar);
echo "usuario borrado";
header('refresh: 3; url= exito.php');

You should place header() outside of foreach() function.

$deleteItems = $_POST['items'];
foreach ($deleteItems as $s)
{
    $str = $s;
    mysqli_query($con,"DELETE FROM table WHERE id='$str'")or die(mysqli_error($con));
}
header("location: manage_menu.php");
exit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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