kenny Posted August 7, 2013 at 12:06 PM Report #521068 Posted August 7, 2013 at 12:06 PM Boas a todos. Sou muito principiante em PHP e resolvi ir a net buscar um código para apagar registo na base de dados através de checkbox. Não funciona muito bem. Ou seja o botao faz o submit mas nao apaga a linha. <form action="" method="post" name="form"> TABELA <?php while($row = mysql_fetch_assoc($query)){ ?> <?php echo row[1] ?> <?php echo row[2] ?> <?php echo row[3] ?> <?php echo row[4] ?> <input type="checkbox" name="unban[]" id="checkbox[]" value="<? echo $row['1']; ?>"> <input name="unban" type="submit" id="unban" value="unban"> if(isset($_POST['unban'])){ $checkbox = $_POST['checkbox']; for($i=0;$i<count($_POST['checkbox']);$i++){ $del_id = $checkbox[$i]; $query = "DELETE FROM table_name WHERE row='$del_id'"; print $query; $result = dbquery($query);} if($result){echo "<meta http-equiv=\"refresh\" content=\"0;URL=indext.php\">";}} ?> </form>
N3lson Posted August 7, 2013 at 12:29 PM Report #521070 Posted August 7, 2013 at 12:29 PM (edited) $query = "DELETE FROM table_name WHERE row='$del_id'"; // ? print $query; $result = dbquery($query); // ? Tens um campo na tabela que se chama row ??? $query = "DELETE FROM table_name WHERE row='$del_id'"; // o campo chama-se row ??? mysql_query($query); // troca o dbquery por isto Tb sou principiante mas acho esse código muito confuso e mal estruturado (pelo menos para mim) Edited August 7, 2013 at 12:31 PM by N3lson You can't do it, kid. But don't worry, my boy. You're not the only one. No one else can do it.
kenny Posted August 7, 2013 at 12:37 PM Author Report #521073 Posted August 7, 2013 at 12:37 PM <table bgcolor='#FFFFFF' width='90%' align='center'> <form action="" method="post" name="form"> <tr align='center' class='font' bgcolor='#0000FF'> <td width='1%' style='white-space: nowrap' title='SteamID ou IP'>SteamID ou IP</td> <td width='1%' style='white-space: nowrap' title='Tempo'>Tempo</td> <td width='7%' style='white-space: nowrap' title='Data de UnBan'>Data de UnBan</td> <td width='1%' style='white-space: nowrap' title='Razao'>Razao</td> <td width='10%' style='white-space: nowrap' title='Admin'>Admin</td> <td width='1%' style='white-space: nowrap' title='Delete'>Delete</td> <?php while($bans = mysql_fetch_assoc($portfolio)){ ?> <td style="border-bottom: 1px solid #000;border-left: 1px solid #000;" align="center"> <b><?php echo $bans['steamid']; ?></b></td> <td style="border-bottom: 1px solid #000;border-left: 1px solid #000;" align="center"> <b><?php echo $bans['banlength']; ?></b></td> <td style="border-bottom: 1px solid #000;border-left: 1px solid #000;" align="center"> <b><?php echo $bans['unbantime']; ?></b></td> <td style="border-bottom: 1px solid #000;border-left: 1px solid #000;" align="center"> <b><?php echo $bans['reason']; ?></b></td> <td style="border-bottom: 1px solid #000;border-left: 1px solid #000;" align="center"> <b><?php echo $bans['admin_name']; ?></b></td> <td style="border-bottom: 1px solid #000;border-left: 1px solid #000;" align="center"><input type="checkbox" name="unban[]" id="checkbox[]" value="<? echo $bans['steamid']; ?>"></td> </tr> if(isset($_POST['unban'])){ $checkbox = $_POST['checkbox']; for($i=0;$i<count($_POST['checkbox']);$i++){ $del_id = $checkbox[$i]; $query_bans = "DELETE FROM advanced_bans WHERE steamid='$del_id'"; $result = mysql_query($query_bans);} if($result){echo "<meta http-equiv=\"refresh\" content=\"0;URL=ban_edit.php\">";}} tá assim mas mesmo assim nao funcionou
N3lson Posted August 7, 2013 at 12:41 PM Report #521074 Posted August 7, 2013 at 12:41 PM (edited) tira o $result = e o form = delete Edited August 7, 2013 at 12:44 PM by N3lson You can't do it, kid. But don't worry, my boy. You're not the only one. No one else can do it.
kenny Posted August 7, 2013 at 12:45 PM Author Report #521075 Posted August 7, 2013 at 12:45 PM Sem sucesso também. 😞
N3lson Posted August 7, 2013 at 12:46 PM Report #521077 Posted August 7, 2013 at 12:46 PM (edited) Em vez de checkbox coloca via link para testar ... echo'<td style="border-bottom: 1px solid #000;border-left: 1px solid #000;" align="center"> <a href="xxx.php?steamid='.$bans['steamid'].'">'.$bans['steamid'].'</a></td>'; Edited August 7, 2013 at 12:51 PM by N3lson You can't do it, kid. But don't worry, my boy. You're not the only one. No one else can do it.
HappyHippyHippo Posted August 7, 2013 at 12:57 PM Report #521078 Posted August 7, 2013 at 12:57 PM foreach ($_POST['unban'] as $value) { $value = (int)$value; if ($value != 0) $query_bans = "DELETE FROM advanced_bans WHERE steamid = '{$value}'"; } IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
kenny Posted August 7, 2013 at 01:02 PM Author Report #521079 Posted August 7, 2013 at 01:02 PM Isso aparece o campo steamid com hiperligação e nao faz nada pois nao corre a funçao para apagar. <table bgcolor='#FFFFFF' width='90%' align='center'> <form action="" method="post" name="form"> <tr align='center' class='font' bgcolor='#0000FF'> <td width='1%' style='white-space: nowrap' title='SteamID ou IP'>SteamID ou IP</td> <td width='1%' style='white-space: nowrap' title='Tempo'>Tempo</td> <td width='7%' style='white-space: nowrap' title='Data de UnBan'>Data de UnBan</td> <td width='1%' style='white-space: nowrap' title='Razao'>Razao</td> <td width='10%' style='white-space: nowrap' title='Admin'>Admin</td> <td width='1%' style='white-space: nowrap' title='Delete'>Delete</td> </tr> <?php while($bans = mysql_fetch_assoc($portfolio)){ ?> <tr> <td style="border-bottom: 1px solid #000;border-left: 1px solid #000;" align="center"> <b><?php echo $bans['steamid']; ?></b></td> <td style="border-bottom: 1px solid #000;border-left: 1px solid #000;" align="center"> <b><?php echo $bans['banlength']; ?></b></td> <td style="border-bottom: 1px solid #000;border-left: 1px solid #000;" align="center"> <b><?php echo $bans['unbantime']; ?></b></td> <td style="border-bottom: 1px solid #000;border-left: 1px solid #000;" align="center"> <b><?php echo $bans['reason']; ?></b></td> <td style="border-bottom: 1px solid #000;border-left: 1px solid #000;" align="center"> <b><?php echo $bans['admin_name']; ?></b></td> <td style="border-bottom: 1px solid #000;border-left: 1px solid #000;" align="center"><input type="checkbox" name="unban[]" id="checkbox[]" value="<? echo $bans['steamid']; ?>"></td> </tr> <?php } ?> </tr> </table> if(isset($_POST['unban'])){ $checkbox = $_POST['checkbox']; foreach ($_POST['unban'] as $value) { $value = (int)$value; if ($value != 0) $query_bans = "DELETE FROM advanced_bans WHERE steamid = '{$value}'"; }} Sem sucesso na mesma 😞
N3lson Posted August 7, 2013 at 01:29 PM Report #521083 Posted August 7, 2013 at 01:29 PM (edited) $query_bans = "DELETE FROM advanced_bans WHERE steamid = '{$value}'"; }} O teu código termina aqui falta-te o mysql_query($x) * * * Testaste em forma de link ? Edited August 7, 2013 at 01:32 PM by N3lson You can't do it, kid. But don't worry, my boy. You're not the only one. No one else can do it.
kenny Posted August 7, 2013 at 01:39 PM Author Report #521086 Posted August 7, 2013 at 01:39 PM (edited) Testei com um form para adicionar e ficou assim. <table bgcolor='#FFFFFF' width='90%' align='center'> <form action="" method="post" name="form"> <tr align='center' class='font' bgcolor='#0000FF'> <td width='1%' style='white-space: nowrap' title='SteamID ou IP'>SteamID ou IP</td> <td width='1%' style='white-space: nowrap' title='Tempo'>Tempo</td> <td width='7%' style='white-space: nowrap' title='Data de UnBan'>Data de UnBan</td> <td width='1%' style='white-space: nowrap' title='Razao'>Razao</td> <td width='10%' style='white-space: nowrap' title='Admin'>Admin</td> <td width='1%' style='white-space: nowrap' title='Delete'>Delete</td> </tr> <?php while($bans = mysql_fetch_assoc($portfolio)){ ?> <tr> <td style="border-bottom: 1px solid #000;border-left: 1px solid #000;" align="center"> <b><?php echo $bans['steamid']; ?></b></td> <td style="border-bottom: 1px solid #000;border-left: 1px solid #000;" align="center"> <b><?php echo $bans['banlength']; ?></b></td> <td style="border-bottom: 1px solid #000;border-left: 1px solid #000;" align="center"> <b><?php echo $bans['unbantime']; ?></b></td> <td style="border-bottom: 1px solid #000;border-left: 1px solid #000;" align="center"> <b><?php echo $bans['reason']; ?></b></td> <td style="border-bottom: 1px solid #000;border-left: 1px solid #000;" align="center"> <b><?php echo $bans['admin_name']; ?></b></td> <td style="border-bottom: 1px solid #000;border-left: 1px solid #000;" align="center"><input type="checkbox" name="unban" id="checkbox" value="<? echo $bans['steamid']; ?>"></td> </tr> <?php } ?> </tr> </table> <table width='1%' style="margin: 0; background: none; border: 0;"> </form> <form method="post"> <table> <tr> <td>Nome:</td> <td><input type="text" name="name" /></td> </tr> <tr> <td>SteamID ou IP</td> <td><input type="text" name="steamid" /></td> </tr> <tr> <td>Tempo</td> <td><input type="text" name="banlength" /></td> </tr> <tr> <td>Razao</td> <td><input type="text" name="reason" /></td> </tr> <tr> <td>Admin</td> <td><input type="text" name="admin_name" /></td> </tr> <tr> <td> </td> <td><input type="submit" name="add" value="add" /></td> </tr> <tr> <td> </td> <td><input type="submit" name="unban" value="unban" /></td> </tr> </table> <?php if (isset($_POST['add'])) { $name=$_POST['name'] ; $steamid= $_POST['steamid'] ; $tempo=$_POST['banlength'] ; $razao=$_POST['reason'] ; $admin=$_POST['admin_name'] ; dbquery("INSERT INTO `advanced_bans`(name,steamid,banlength,reason,admin_name) VALUES ('$name','$steamid','$tempo','$razao','$admin')"); echo "<meta http-equiv=\"refresh\" content=\"0;URL=ban_edit.php\">"; } if(isset($_POST['unban'])){ $checkbox = $_POST['checkbox']; foreach ($_POST['unban'] as $value) { $value = (int)$value; if ($value != 0) dbquery("DELETE FROM `advanced_bans` WHERE steamid = '{$value}'"); echo "<meta http-equiv=\"refresh\" content=\"0;URL=ban_edit.php\">"; }} ?> </form> O add tá a funcionar o unban ainda nao está :/ deu me os seguintes erros: Invalid argument supplied for foreach() Undefined index: checkbox Edited August 7, 2013 at 01:47 PM by kenny
N3lson Posted August 7, 2013 at 03:17 PM Report #521106 Posted August 7, 2013 at 03:17 PM (edited) coloca @ antes da variável @$checkbox = $_POST['checkbox']; ou $checkbox = (isset($_POST['checkbox'])) ? $_POST['checkbox'] : FALSE ; Mas não percebi a razão do foreach no unban .... e a razão de usares o mesmo form para inserir e eliminar Pois para a eliminação basta saber o ID (steamID) do objecto a eliminar Edited August 7, 2013 at 03:39 PM by N3lson You can't do it, kid. But don't worry, my boy. You're not the only one. No one else can do it.
HappyHippyHippo Posted August 7, 2013 at 04:19 PM Report #521111 Posted August 7, 2013 at 04:19 PM Mas não percebi a razão do foreach no unban .... ele tem uma tabela (e muito mao código tanto PHP como HTML) co uma lista de checkboxes com o nome "unban[]" o que ele irá receber será um array com o valor de cada um desses checkboxes. logo, para eliminar os seleccionados, é necessário uma ciclo de verificação de cada um dos valores. nota : o código que eu apresentei é um exemplo para resolver o problema, se o OP não quer perceber e fazer copy-paste cego, o problema é dele ... 1 Report IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
kenny Posted August 8, 2013 at 08:10 PM Author Report #521276 Posted August 8, 2013 at 08:10 PM (edited) ele tem uma tabela (e muito mao código tanto PHP como HTML) co uma lista de checkboxes com o nome "unban[]" o que ele irá receber será um array com o valor de cada um desses checkboxes. logo, para eliminar os seleccionados, é necessário uma ciclo de verificação de cada um dos valores. nota : o código que eu apresentei é um exemplo para resolver o problema, se o OP não quer perceber e fazer copy-paste cego, o problema é dele ... Boas como eu disse no primeiro post sou muito iniciante em PHP nao é preciso chamar nomes as pessoas. Já tentei com o código que mandou e sem efeito na mesma. nao sei se o problema é do input do checkbox ou da funçao para apagar os dados na BD. o input estou a tentar desta forma <input name="checkbox[]" value="<?php echo $bans['steamid']; ?>" type="checkbox" id="checkbox[<?php echo $bans['steamid']; ?>]" /> e a função já tentei de várias maneiras e agora está assim: if(isset($_POST['unban'])){ $checkbox = $_POST['checkbox']; foreach ($_POST['checkbox'] as $value) { $value = (int)$value; if ($value != 0) dbquery("DELETE FROM `advanced_bans` WHERE steamid = '{$value}'"); echo "<meta http-equiv=\"refresh\" content=\"0;URL=ban_edit.php\">"; }} @N3lson agora tentei como me disse também. if(isset($_POST['unban'])){ $checkbox = (isset($_POST['checkbox'])) ? $_POST['checkbox'] : FALSE ; foreach ($_POST['checkbox'] as $value) { $value = (int)$value; if ($value != 0) dbquery("DELETE FROM `advanced_bans` WHERE steamid = '{$value}'"); echo "<meta http-equiv=\"refresh\" content=\"0;URL=ban_edit.php\">"; }} E ainda nao funciona também. EDIT: Não dá erros nenhuns mas nao apaga nada ainda. Edited August 8, 2013 at 08:13 PM by kenny
N3lson Posted August 8, 2013 at 08:16 PM Report #521278 Posted August 8, 2013 at 08:16 PM Vê se isso te ajuda http://www.phpeasystep.com/mysql/8.html You can't do it, kid. But don't worry, my boy. You're not the only one. No one else can do it.
HappyHippyHippo Posted August 9, 2013 at 12:06 AM Report #521294 Posted August 9, 2013 at 12:06 AM Boas como eu disse no primeiro post sou muito iniciante em PHP nao é preciso chamar nomes as pessoas. aqui ninguem chamou nomes a ninguem ... se és iniciante, quanto muito deverias saber que tens muito para aprender Já tentei com o código que mandou e sem efeito na mesma. o código que foi apresentado foi para o post que continha este HTML: <input type="checkbox" name="unban[]" id="checkbox[]" value="<? echo $bans['steamid']; ?>"> que como podes ver, o nome dado aos elementos são : unban[] coisa que alteraste posteriormente por alguma razão. no entanto, continuas com a seguinte verificação no teu código: if(isset($_POST['unban'])){ // <------------------------------------------------------------------------- $checkbox = $_POST['checkbox']; foreach ($_POST['checkbox'] as $value) { $value = (int)$value; if ($value != 0) dbquery("DELETE FROM `advanced_bans` WHERE steamid = '{$value}'"); echo "<meta http-equiv=\"refresh\" content=\"0;URL=ban_edit.php\">"; } } IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
kenny Posted August 9, 2013 at 01:43 AM Author Report #521299 Posted August 9, 2013 at 01:43 AM (edited) Pois tenho muito aprender e é isso que tou a tentar nao se aprende assim em 2 dias. Já tentei de várias maneiras e mesmo assim sem sucesso nenhum. Está assim os 2 HTML: Este a caixa do checkbox <input type="checkbox" name="unban[]" id="checkbox[]" value="<? echo $bans['steamid']; ?>"> O botão para fazer o submit: <input name="unban[]" type="submit" id="unban[]" value="Unban"> Função assim: if(isset($_POST['unban[]'])){ // <----------------------------------- dizia assim ? $checkbox = $_POST['checkbox']; foreach ($_POST['checkbox'] as $value) { $value = (int)$value; if ($value != 0) dbquery("DELETE FROM `advanced_bans` WHERE steamid = '{$value}'"); echo "<meta http-equiv=\"refresh\" content=\"0;URL=ban_edit.php\">"; } } Desculpem a minha ignorância mas nao sei o que estou a errar mesmo. @Nelson esse código dá me erros Undefined variable: $delete e o $count E também não me está a funcionar. 😞 Mais uma vez desculpem. Edited August 9, 2013 at 01:44 AM by kenny
HappyHippyHippo Posted August 9, 2013 at 02:01 AM Report #521300 Posted August 9, 2013 at 02:01 AM primeiro, altera o atributo name do botao submit para algo como : do_unban. o nome unban[] é para as checkboxes segundo, estas a fazer um foreach para um array com o nome checkbox ? porque não fazes como te disse ? IRC : sim, é algo que ainda existe >> #p@p Portugol Plus
kenny Posted August 12, 2013 at 02:17 PM Author Report #521516 Posted August 12, 2013 at 02:17 PM (edited) Mesmo assim nao me funcionou e pus assim na checkbox : <input type="checkbox" name="unban[]" value="<? echo $bans['steamid']; ?>"> O botão alterei e ficou assim: <input name="do_unban" type="submit" value="Unban"> if(isset($_POST['do_unban'])){ $checkbox = $_POST['unban']; foreach ($_POST['unban'] as $value) { $value = (int)$value; if ($value != 0) $query_bans = "DELETE FROM advanced_bans WHERE steamid = '{$value}'"; echo "<meta http-equiv=\"refresh\" content=\"0;URL=ban_edit.php\">"; } } Mesmo assim nao me está a funcionar 😕 Não estou a conseguir entender o porque. Edited August 12, 2013 at 02:18 PM by kenny
yoda Posted August 12, 2013 at 05:26 PM Report #521535 Posted August 12, 2013 at 05:26 PM (edited) Se não te estiver a apagar nenhum registo, verifica se fizeste tudo o que o @HappyHippyHippo disse. Se só estiver a apagar um registo, é por causa do refresh que estás a fazer dentro do ciclo. Edited August 12, 2013 at 05:27 PM by yoda before you post, what have you tried? - http://filipematias.info sense, purpose, direction
kenny Posted August 13, 2013 at 08:30 PM Author Report #521653 Posted August 13, 2013 at 08:30 PM Se não te estiver a apagar nenhum registo, verifica se fizeste tudo o que o @HappyHippyHippo disse. Se só estiver a apagar um registo, é por causa do refresh que estás a fazer dentro do ciclo. O código postado na minha ultima mensagem tem os passos todos que o @HappyHippyHippo me mandou fazer mas nao me está a funcionar e nao consigo ver o porque ... O que está na minha ultima mensagem tem que funcionar assim ?
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now