How to insert value of an array with $ _SESSION in mysql?

1

Hi, I have a SESSION variable that contains this data (id, name, quantity, price) is a shopping cart, so when I bring this variable with all the content to my other page I do not know how to insert the amount that this array in the specific fields of my table which has these fields (Name, Address, Email, Phone, Taco_pastor, Taco_suadero, Torta_pastor, Torta_suadero, Refreshment, Juice, Total, Date) in that order, what I want is for me to insert the amount that the user has requested and that if I do not have an amount, I put it as zero in my table

I have tried with foreach and ifs to compare them by the value of the inputs (where they look good) but until now this does not work for me and only prints the amount in "Torta_suadero"

foreach($_SESSION['shopping_cart'] as $key => $product):

$cant=$_POST['nombre'];

if ($cant=='Taco_pastor') {

$tacop2= $product['quantity'];

} else {

$tacop2=0;

}

if ($cant=='Taco_suadero') {

$tacosu2= $product['quantity'];

} else {

$tacosu2=0;

}

if ($cant=='Torta_pastor') {

$tortap2= $product['quantity'];

} else {

$tortap2=0;

}

if ($cant=='Torta_suadero') {

$tortasu2= $product['quantity'];

} else {

$tortasu2=0;

}

if ($cant=='Refresco') {

$refresco2= $product['quantity'];

} else {

$refresco2=0;

}

if ($cant=='Jugo') {

$jugo2= $product['quantity'];

} else {

$jugo2=0;

}

endforeach;

if(!$error){
echo '';
$sql = "insert into pedidos(Nombre,Direccion,Email,Telefono, Taco_pastor,Taco_suadero,Torta_pastor,Torta_suadero,Jugo,Refresco,Total,Fecha)
               values('$cliente', '$dir2', '$email2', '$tel2', '$tacop2','$tacosu2','$tortap2','$tortasu2','$jugo2','$refresco2','$total2','$fecha')";
if(mysqli_query($conn, $sql)){
$successMsg = 'SU PEDIDO A SIDO TOMADO EN UN MOMENTO LE LLEGARA A SU DOMICILIO GRACIAS!!!!'; 
}else{
echo 'Error '.mysqli_error($conn);
}
}

    
asked by mrkillmer 13.08.2018 в 03:23
source

2 answers

0

You could manipulate the data something like this:

<?php
$shopping_cart = $_SESSION['shopping_cart'];

foreach($shopping_cart as $element)
{
    $nombre = $element['nombre'];
    $precio = $element['precio'];
    $quantity = $element['quantity'];
    $sql = "insert into pedidos(Nombre,Direccion,Email,Telefono, Taco_pastor,Taco_suadero,Torta_pastor,Torta_suadero,Jugo,Refresco,Total,Fecha)
               values('$cliente', '$dir2', '$email2', '$tel2', '$tacop2','$tacosu2','$tortap2','$tortasu2','$jugo2','$refresco2','$total2','$fecha')";
    if(mysqli_query($conn, $sql))
        $successMsg = 'SU PEDIDO A SIDO TOMADO EN UN MOMENTO LE LLEGARA A SU DOMICILIO GRACIAS!!!!'; 
    else
        echo 'Error '.mysqli_error($conn);
}

I did not add the logic of the problem, but in $ name, $ price, $ quantity, there would already be the variables to make the decision. The code is not entirely optimal since an insert is made for each product, while a single insert could be made in mass, so to speak.

- EDIT -

To complement, in this post , after the if (is_array ()) is implemented the way to make a massive INSERT, in your interest.

    
answered by 13.08.2018 в 03:55
0

The first thing I would say is that seeing the insert query your table has a bad design, you relate the products with the client's name, instead of the unique id, what happens if there are 2 clients that are called equal?

This case should be a many-to-many relationship between usuarios / clientes and detalles_pedidos , resulting in an intermediate table pedidos

In any case when you need to insert in the bd, the recommendation is to use prepared statements, to avoid sql injection problems, prepared statements also make it easier to insert data in bulk.

Once explained, let's take a basic example

<?php
$mysqli = new mysqli("localhost", "mi_usuario", "mi_contraseña", "world");

/* comprobar la conexión */
if (mysqli_connect_errno()) {
    printf("Falló la conexión: %s\n", mysqli_connect_error());
    exit();
}

/* Preparar una sentencia INSERT */
$consulta = "INSERT INTO pedido (id_cliente, id_articulo, cantidad, precio) VALUES (?,?,?,?)";
$sentencia = $mysqli->prepare($consulta);

$sentencia->bind_param("iiis", $id_cliente, $id_articulo, $cantidad,$precio);

//una vez preparada la consulta podemos recorrer el Array he ir insertando
foreach($cesta as $key => $value){
    $id_cliente = $value['id_cliente'];
    $id_articulo = $value['id_articulo'];
    $cantidad = $value['cantidad'];
    $precio = $value['precio'];

    /* Ejecutar la sentencia */
    $sentencia->execute();
}

$mysqli->close();
?>
    
answered by 13.08.2018 в 13:56