Foros del Web » Programando para Internet » PHP »

Problema con Delete

Estas en el tema de Problema con Delete en el foro de PHP en Foros del Web. Hola, Intento borrar un registro con PHP y MySQL y no consigo que me borre nada. ¿Qué es lo que hago mal?. Gracias. Este es ...
  #1 (permalink)  
Antiguo 27/12/2010, 10:51
 
Fecha de Ingreso: marzo-2010
Mensajes: 10
Antigüedad: 14 años, 1 mes
Puntos: 0
Problema con Delete

Hola,

Intento borrar un registro con PHP y MySQL y no consigo que me borre nada. ¿Qué es lo que hago mal?. Gracias. Este es el código:

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


?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Nombre</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Direccion</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Tlfno</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>CIF</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Tlfno2</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Fax</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
echo $rows[0];
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows[0]; ?>"></td>
<td bgcolor="#FFFFFF"><?php echo $rows[0]; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['nombre']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['direccion']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['tlf']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['cif']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['tlf2']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['email']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['fax']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>

<?php
// Check if delete button active, start this
if($_POST['delete']){
//print_r($_POST);
//exit;
$checkbox=$_POST['checkbox'];
echo count($checkbox);
foreach($_POST['checkbox'] as $checkbox) {

$del_id = $checkbox;

echo $del_id;

$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}

// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=eliminar_sociedad.php\">";
}
}
mysql_close();
?>
</table>
</form>
</td>
</table>

Necesito ayuda urgente. Gracias
  #2 (permalink)  
Antiguo 27/12/2010, 11:06
 
Fecha de Ingreso: diciembre-2010
Ubicación: Rosario, Santa Fe
Mensajes: 326
Antigüedad: 13 años, 4 meses
Puntos: 13
Respuesta: Problema con Delete

Probaste hacer un var_dump de la consulta SQL para ver si se genera correctamente?
Código PHP:
if($result){
echo 
"<meta http-equiv=\"refresh\" content=\"0;URL=eliminar_sociedad.php\">";

Le falta el else para saber lo posibles errores:
Código PHP:
}else{
  echo 
mysql_error();

Fijate que salida te da para ver si el error esta en la consulta.
  #3 (permalink)  
Antiguo 28/12/2010, 02:09
 
Fecha de Ingreso: marzo-2010
Mensajes: 10
Antigüedad: 14 años, 1 mes
Puntos: 0
Respuesta: Problema con Delete

He hecho esto, estoy viendo que no me recoje los valores del checkbox y no sé como hacerlo. Gracias
  #4 (permalink)  
Antiguo 28/12/2010, 09:04
 
Fecha de Ingreso: diciembre-2010
Ubicación: Rosario, Santa Fe
Mensajes: 326
Antigüedad: 13 años, 4 meses
Puntos: 13
Respuesta: Problema con Delete

Prueba haciendo var_dump de los valores recogidos por los checkboxes para ver que información te devuelve.
  #5 (permalink)  
Antiguo 28/12/2010, 10:47
 
Fecha de Ingreso: marzo-2010
Mensajes: 10
Antigüedad: 14 años, 1 mes
Puntos: 0
Respuesta: Problema con Delete

He hecho un var_dump y obtengo esto:

2string(1) "2" 2 Kiko Las Nieves 923222222 A123456 [email protected]

array(1) { [0]=> string(27) "" } Hola count(Array)Hola string(63) "DELETE FROM sociedad WHERE id_Soc=''" You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'id_Soc']; ?>'' at line 1

Alguna idea?. Gracias. Recoge vacío, pero tendría que coger el valor del checkbox que he seleccionado, pero nada.
  #6 (permalink)  
Antiguo 28/12/2010, 10:59
Avatar de eits  
Fecha de Ingreso: junio-2005
Ubicación: valladolid, yucatán
Mensajes: 1.655
Antigüedad: 18 años, 10 meses
Puntos: 88
Respuesta: Problema con Delete

tu checkbox es un array es por eso que te da el error intenta con esto
Código PHP:
Ver original
  1. $del_id = $checkbox[0];//o cambia el 0 por el número donde este el id a borrar
__________________
El amor es la locura mas lucida que tiene el hombre.- Andres Henestrosa
la tristeza no existe, solo es... la ausencia de la felicidad.
  #7 (permalink)  
Antiguo 28/12/2010, 11:12
 
Fecha de Ingreso: marzo-2010
Mensajes: 10
Antigüedad: 14 años, 1 mes
Puntos: 0
Respuesta: Problema con Delete

Lo he hecho y nada:

Vuelvo a copiar el código y luego el resultado de los var_dump

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


?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Nombre</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Direccion</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Tlfno</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>CIF</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Tlfno2</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Fax</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){

var_dump($rows['id_Soc']);
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['id_Soc']; ?>"></td>
<td bgcolor="#FFFFFF"><?php echo $rows['id_Soc']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['nombre']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['direccion']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['tlf']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['cif']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['tlf2']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['email']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['fax']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>

<?php
// Check if delete button active, start this
if (isset($_POST['delete'])){
//print_r($_POST);
//exit
for($i=0;$i<$count;$i++){
$del_id = $checkbox[0];
var_dump($del_id);
$sql = "DELETE FROM $sociedad WHERE id_Soc='$del_id'";
$result = mysql_query($sql);
}

// if successful redirect to delete_multiple.php
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=eliminar_sociedad.php\">";
}else{
echo mysql_error();

}
}
mysql_close();
?>
</table>
</form>
</td>
</table>



1 Delete multiple rows in mysql
# Id Nombre Direccion Tlfno CIF Tlfno2 Email Fax
string(1) "2" 2 Kiko Las Nieves 923222222 A123456 [email protected]

NULL You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE id_Soc=''' at line 1

Qué veís?. Por lo que veo, antes tenía dos registros, y borré uno de forma manual y me dá que lo sigue teniendo en cuenta, pero no sé. Podéis ayudar?. Gracias
  #8 (permalink)  
Antiguo 28/12/2010, 11:14
Avatar de Nano_  
Fecha de Ingreso: febrero-2006
Ubicación: Bogotá, Colombia
Mensajes: 1.866
Antigüedad: 18 años, 2 meses
Puntos: 96
Respuesta: Problema con Delete

Saludos

Código PHP:
Ver original
  1. //Debes validar antes de que por los menos han seleccionado un checkbox
  2. //Con la función implode los checkbox seleccionados quedan separados por comas
  3. $checkbox= implode(",",$_POST['checkbox']);
  4. //Te evitas el foreach y realizas el delete de la siguiente manera:
  5. $sql = "DELETE FROM $tbl_name WHERE id IN ($checkbox)";
  6. $result = mysql_query($sql)or die ("error al borrar",mysql_error());

Hasta Pronto
__________________
:.:Nano.:: @nano_hard - Retornando al foro
  #9 (permalink)  
Antiguo 28/12/2010, 11:26
 
Fecha de Ingreso: marzo-2010
Mensajes: 10
Antigüedad: 14 años, 1 mes
Puntos: 0
Respuesta: Problema con Delete

He hecho esto que me has dicho y me sale esto: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1 , no sé que error hay en la sentencia sql.
  #10 (permalink)  
Antiguo 28/12/2010, 12:04
 
Fecha de Ingreso: marzo-2010
Mensajes: 10
Antigüedad: 14 años, 1 mes
Puntos: 0
Respuesta: Problema con Delete

Problema solventado. El problema radicaba en no haber puesto un php. Gracias por vuestra ayuda a todos. Saludos

Etiquetas: delete
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 00:32.