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