Ver Mensaje Individual
  #1 (permalink)  
Antiguo 15/09/2008, 05:31
Avatar de mgomezbuceta
mgomezbuceta
 
Fecha de Ingreso: mayo-2008
Ubicación: Santiago de Compostela
Mensajes: 47
Antigüedad: 15 años, 11 meses
Puntos: 0
Sonrisa Error de duplicación de inserción de datos en una tabla MySQL

Buenas:

Mi problema es que estoy montando una tienda virtual y un formulario que me envía los datos a una página que los procesa y donde hago la inserción en la tabla.
El código de inserción es el siguiente:

++$_SESSION['contador'];

if ($_SESSION['contador']==1)
{

++$_SESSION['numero_compra'];

if (!isset($_SESSION['id_compra']) || $_SESSION['id_compra']=="")
{
require("conexion.php");
$sql="insert into compras( fecha_compra, cliente) VALUES ( Now(), '')";

mysql_query($sql,$conexion);

$_SESSION['id_compra']=mysql_insert_id();

}

if (isset($_POST['nombre_software']) && $_POST['nombre_software']!="" && $_POST['precio_software']!=0)
{
echo 'Unidades sf: '.$_POST['unidades'];
echo 'Nombre sf: '.$_POST['nombre_software'];
echo 'Precio sf: '.$_POST['precio_software'];
echo 'Peso sf: '.$_POST['peso_software'];

$sql=sprintf("insert into lineas_compra(id_comp, numero_compra, software, muebles, escudo, chip, bono, cantidad, descripcion_producto, precio_unitario, precio_tot, peso_total, reembolso) values ('%s','%s','1','0','0','0','0','%s','%s','%s','%s' ,'%s','%s')",$_SESSION['id_compra'], $_SESSION['numero_compra'], $_POST['unidades'], $_POST['nombre_software'], $_POST['precio_software'], ($_POST['unidades'] * $_POST['precio_software']), $_POST['peso_software'], $_SESSION['reembolso']);

mysql_query($sql,$conexion);

}
elseif (isset($_POST['nombre_hardware']) && $_POST['nombre_hardware']!="" && $_POST['precio_hardware']!=0)
{
echo 'Unidades hd: '.$_POST['unidades'];
echo 'Nombre hd: '.$_POST['nombre_hardware'];
echo 'Precio hd: '.$_POST['precio_hardware'];
echo 'Peso hd: '.$_POST['peso_hardware'];

$sql=sprintf("insert into lineas_compra(id_comp, numero_compra, software, muebles, escudo, chip, bono, cantidad
, descripcion_producto, precio_unitario, precio_tot, peso_total, reembolso) values ('%s','%s','1','0','0','0','0','%s','%s','%s','%s' ,'%s','%s')",$_SESSION['id_compra'], $_SESSION['numero_compra'], $_POST['unidades'], $_POST['nombre_hardware'], $_POST['precio_hardware'], ($_POST['unidades'] * $_POST['precio_hardware']), $_POST['peso_hardware'], $_SESSION['reembolso']);
mysql_query($sql,$conexion);
}

La variable $_SESSION['contador'] en el inicio es 0

Y el código de uno de los formularios que envían productos a esta página es este:

<form name="resumen" action="intro_tienda.php?carga=tienda_anadir.php" method="post" style="margin-bottom:0px; margin-top:0px">
<div style=" float:right; width:200px; display:inline; clear: both">
<table width="200" border="0" cellpadding="0" cellspacing="0" height="100%">
<tr>
<td height="17" style="background-image:url(gifs/boxes/cabecera_resumen2.png); background-repeat:no-repeat; height:30px;"></td>
</tr>
<tr>

<td height="100" style="background-image: url(gifs/boxes/mdd.png); background-repeat: repeat-y;">

<div style="width:95%; height:100%; margin-left:7px">

<table style="border:0px; width:100%; height:100%" cellpadding="0" cellspacing="0">
<tr>
<td>
<img src="gifs/arrow_grey.gif"/>
<input name="soft" type="text" value="PC Kiosko 2007 BASIC Edition CD-ROM" size="40" style="font-family:Arial; font-size:7pt; border:0px" readonly="">
<input type="hidden" name="nombre_software" value="PC Kiosko 2007 BASIC Edition CD-ROM">
<input type="hidden" name="precio_software" value="120.00">
<input type="hidden" name="peso_software" value="0.33"> </td>
</tr>


<tr>
<td>
<img src="gifs/arrow_grey.gif"/>
<input name="cust" type="text" size="30" style="font-family:Arial; font-size:7pt; border:0px" value="Sin tarjeta custodius" readonly="">
<input type="hidden" name="nombre_custodius">
<input type="hidden" name="precio_custodius">
<input type="hidden" name="peso_custodius"> </td>
</tr>
<tr>
<td>
<img src="gifs/arrow_grey.gif"/>
<input name="watchd" type="text" size="30" style="font-family:Arial; font-size:7pt; border:0px" value="Sin tarjeta watchdog" readonly="">
<input type="hidden" name="nombre_watchdog">
<input type="hidden" name="precio_watchdog">
<input type="hidden" name="peso_watchdog"> </td>
</tr>
<input type="hidden" name="Software">
<input type="hidden" name="Custodius">
<input type="hidden" name="WatchDog">
</table>
</div>
<div style="width:90%; height:30px; margin-left:7px;margin-top:5px">
<font color="#000000" face="Arial" style="font-size:7pt"><strong>Subtotal:</strong></font>
<input name="precio" type="text" size="20" style="font-family:Arial; font-size:8pt; border:0px" value="120 &euro;" readonly="">
</div> <div align="right" style="width:90%; height:10%; padding:10px 10px 5px 0px;">

<input type="hidden" name="unidades" value="1">
<input type="hidden" name="total_compra" value="120.00">

<input type="image" value="Comprar" src="gifs/boxes/btn_comprar_verde.gif" align="middle" onClick="submit()"/>
</div> </td>
</tr>
<tr>
<td style="background-image: url(gifs/boxes/pie_resumen.png); background-repeat:no-repeat; height:34px;"></td>
</tr>
</table>
</div>
</form>

El problema está en que cuando le doy a comprar, el artículo que selección, se añade por duplicado a la base de datos.

Alguien puede ayudarme, porque no encuentro el error.

Muchas gracias.