guardar datos de datos de un vector en bd
Publicado por Rey (74 intervenciones) el 07/05/2018 21:23:57
Actualmente me guarda la compra en la base datos sin el nombre del que inicio sesion , tengo el login hay en ese archivo se trae en un vector los datos de la tabla usuario .
tengo
carritodecompras.php
compras.php
login.php
tengo
1
2
$_SESSION['carrito'] // aca guarda lo que se compra
$_SESSION['Usuario'] //aca guarda los datos del usuario
carritodecompras.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
session_start();
include './conexion.php';
if(isset($_SESSION['carrito'])){
if(isset($_GET['id'])){
$arreglo=$_SESSION['carrito'];
$encontro=false;
$numero=0;
for($i=0;$i<count($arreglo);$i++){
if($arreglo[$i]['Id']==$_GET['id']){
$encontro=true;
$numero=$i;
}
}
if($encontro==true){
$arreglo[$numero]['Cantidad']=$arreglo[$numero]['Cantidad']+1;
$_SESSION['carrito']=$arreglo;
}else{
$nombre="";
$precio=0;
$imagen="";
$link = mysqli_connect("localhost","root", "","carrito");
mysqli_select_db($link, "carrito");
$tildes = $link->query("SET NAMES 'utf8'"); //Para que se muestren las tildes correctamente
$re = mysqli_query($link, "select * from productos where id=".$_GET['id'])or die(mysqli_error()
);
while ($f=mysqli_fetch_array($re)) {
$nombre=$f['nombre'];
$precio=$f['precio'];
$imagen=$f['imagen'];
}
$datosNuevos=array('Id'=>$_GET['id'],
'Nombre'=>$nombre,
'Precio'=>$precio,
'Imagen'=>$imagen,
'Cantidad'=>1);
array_push($arreglo, $datosNuevos);
$_SESSION['carrito']=$arreglo;
}
}
}else{
if(isset($_GET['id'])){
$nombre="";
$precio=0;
$imagen="";
$link = mysqli_connect("localhost","root", "","carrito");
mysqli_select_db($link, "carrito");
$tildes = $link->query("SET NAMES 'utf8'"); //Para que se muestren las tildes correctamente
$re = mysqli_query($link, "select * from productos where id=".$_GET['id'])or die(mysqli_error());
while ($f=mysqli_fetch_array($re)) {
$nombre=$f['nombre'];
$precio=$f['precio'];
$imagen=$f['imagen'];
}
$arreglo[]=array('Id'=>$_GET['id'],
'Nombre'=>$nombre,
'Precio'=>$precio,
'Imagen'=>$imagen,
'Cantidad'=>1);
$_SESSION['carrito']=$arreglo;
}
}
?>
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="utf-8"/>
<title>Carrito de Compras</title>
<link rel="stylesheet" type="text/css" href="./css/estilos.css">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="./js/scripts.js"></script>
</head>
<body>
<header>
<img src="./imagenes/logo.png" id="logo">
<a href="./carritodecompras.php" title="ver carrito de compras">
<img src="./imagenes/carrito.png">
</a>
</header>
<section>
<?php
$total=0;
if(isset($_SESSION['carrito'])){
$datos=$_SESSION['carrito'];
$total=0;
for($i=0;$i<count($datos);$i++){
?>
<div class="producto">
<center>
<img src="./productos/<?php echo $datos[$i]['Imagen'];?>"><br>
<span ><?php echo $datos[$i]['Nombre'];?></span><br>
<span>Precio: <?php echo $datos[$i]['Precio'];?></span><br>
<span>Cantidad:
<input type="text" value="<?php echo $datos[$i]['Cantidad'];?>"
data-precio="<?php echo $datos[$i]['Precio'];?>"
data-id="<?php echo $datos[$i]['Id'];?>"
class="cantidad">
</span><br>
<span class="subtotal">Subtotal:<?php echo $datos[$i]['Cantidad']*$datos[$i]['Precio'];?></span><br>
<a href="#" class="eliminar" data-id="<?php echo $datos[$i]['Id']?>">Eliminar</a>
</center>
</div>
<?php
$total=($datos[$i]['Cantidad']*$datos[$i]['Precio'])+$total;
}
}else{
echo '<center><h2>No has añadido ningun producto</h2></center>';
}
echo '<center><h2 id="total">Total: '.$total.'</h2></center>';
if($total!=0){
echo '<center><a href="./compras/compras.php" class="aceptar">Comprar</a></center>;';
}
?>
<center><a href="./">Ver catalogo</a></center>
</section>
</body>
</html>
compras.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
session_start();
include "../conexion.php";
$arreglo=$_SESSION['carrito'];
$numeroventa=0;
$link = mysqli_connect("localhost", "root","","carrito");
mysqli_select_db($link, "carrito");
$tildes = $link->query("SET NAMES 'utf8'"); //Para que se muestren las tildes correctamente
$re=mysqli_query($link, "select * from compras order by numeroventa DESC limit 1") or die(mysqli_error());
while ( $f=mysqli_fetch_array($re)) {
$numeroventa=$f['numeroventa'];
}
if($numeroventa==0){
$numeroventa=1;
}else{
$numeroventa=$numeroventa+1;
}
for($i=0; $i<count($arreglo);$i++){
mysqli_query($link,"insert into compras (numeroventa, imagen,nombre,precio,cantidad,subtotal) values(
".$numeroventa.",
'".$arreglo[$i]['Imagen']."',
'".$arreglo[$i]['Nombre']."',
'".$arreglo[$i]['Precio']."',
'".$arreglo[$i]['Cantidad']."',
'".($arreglo[$i]['Precio']*$arreglo[$i]['Cantidad'])."'
)");
}
unset($_SESSION['carrito']);
header("Location: ../index.php");
?>
login.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php
session_start();
include "../conexion.php";
$link = mysqli_connect("localhost", "root","","carrito");
mysqli_select_db($link, "carrito");
$tildes = $link->query("SET NAMES 'utf8'"); //Para que se muestren las tildes correctamente
$re=mysqli_query($link,"select * from usuarios where Usuario='".$_POST['Usuario']."' AND Password='".$_POST['Password']."'") or die(mysqli_error());
while ($f=mysqli_fetch_array($re)) {
$arreglo[]=array('Nombre'=>$f['Nombre'],
'Apellido'=>$f['Apellido'],'Imagen'=>$f['Imagen']);
}
if(isset($arreglo)){
$_SESSION['Usuario']=$arreglo;
header("Location: ../admin.php");
}else{
header("Location: ../login.php?error=datos no validos");
}
?>
Valora esta pregunta
0