Ir para conteúdo

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

Taina Coutto

Pegar todos os valores de uma array e inserir no MYSQLI

Recommended Posts

Tenho dificuldade pois não lembro como indica para inserir os dados de uma Array, ela esta assim:

Array
(
[carrinho] => Array
(
[0] => Array
(
[id] => 340
[nome] => ACE TITO
[preco] => 13.99
[qtde] => 1
[descricao] => 9229-0
)

[1] => Array
(
[id] => 337
[nome] => ACE JESUS
[preco] => 11.99
[qtde] => 1
[descricao] => 9223-9
)

)

[total] => 25,98
)

Alguem?

Compartilhar este post


Link para o post
Compartilhar em outros sites

Veja se desta forma te da um norte:

<?php
define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','teste');

$conn = mysqli_connect(HOST, USER, PASS, DB);
if(mysqli_connect_errno()){
    printf("A conexão falhou: %s\n", mysqli_connect_error());
    exit();
}

$teste = Array(
    'carrinho' => Array(
        '0' => Array(
            'id' => '340',
            'nome' => 'ACE TITO',
            'preco' => '13.99',
            'qtde' => '1',
            'descricao' => '9229-0'
        ),

        '1' => Array(
            'id' => '337',
            'nome' => 'ACE JESUS',
            'preco' => '11.99',
            'qtde' => '1',
            'descricao' => '9223-9'
        )
    ),

    'total' => 25, 98
);

foreach ($teste['carrinho'] as $key => $value) {
    $id = $value['id'];
    $nome = $value['nome'];
    $preco = $value['preco'];
    $qtde = $value['qtde'];
    $descricao = $value['descricao'];
    $inserir = "INSERT INTO `teste` (`id`, `nome`, `preco`, `quantidade`, `descricao`) VALUES ('$id', '$nome', '$preco', '$qtde', '$descricao')";
    mysqli_query($conn, $inserir) or die(mysqli_error($conn));
}

Compartilhar este post


Link para o post
Compartilhar em outros sites

e essa aqui

 

Array
(
[carrinho] => Array
(
[0] => Array
(
[id] => 340
[nome] => ACE TITO
[preco] => 13.99
[qtde] => 1
[descricao] => 9229-0
)

[1] => Array
(
[id] => 337
[nome] => ACE JESUS
[preco] => 11.99
[qtde] => 1
[descricao] => 9223-9
)

)

[total] => 25,98
)

Compartilhar este post


Link para o post
Compartilhar em outros sites


<? require "setidioma.php"; ?>

<section>

<div class="c-tabs">

<ul class="tab-list">

<li class="m-active"><?php echo $texto['carrinho_1']; ?></li>

<li><?php echo $texto['carrinho_2']; ?></li>

</ul>

<ul class="content-list">

<li>

<?php

$pedido = $_GET["pedido"];

$area = isset($_GET['area']) ? $_GET['area'] : NULL;

$acao = isset($_GET['acao']) ? $_GET['acao'] : NULL;

$objCar = new Carrinho;

 

if ($area == "carrinho")

{

 

if ($acao == "adicionar")

{

$id = isset ($_GET['id']) ? (int)$_GET['id'] : NULL;

$objCar->AdicionarProduto ($id);

 

}

 

if ($acao == "remover")

{

$id = isset ($_GET['id']) ? (int)$_GET['id'] : NULL;

$objCar->RemoverProduto ($id);

}

 

if ($acao == "alt_qtde")

{

$id = isset ($_GET['id']) ? (int)$_GET['id'] : NULL;

$n_qtde = isset ($_POST['qtde_'.$id]) ? (int)$_POST['qtde_'.$id] : 1;

$objCar->AlterarQuantidade($id, $n_qtde);

}

 

 

 

echo "

<h2>".$texto['carrinho_titulo']."</h2>

<table width=\"650\" cellpadding=\"2\" cellspacing=\"0\">

<tr>

<td width=\"300\" style=\"text-align:left;background-color: #ec587a;color:snow;\"><strong>".$texto['carrinho_cart_1']."</strong></td>

<td width=\"100\" style=\"text-align:left;background-color: #ec587a;color:snow;\"><strong>".$texto['carrinho_cart_2']."</strong></td>

<td width=\"100\" style=\"text-align:left;background-color: #ec587a;color:snow;\"><strong>".$texto['carrinho_cart_3']."</strong></td>

<td width=\"100\" style=\"text-align:left;background-color: #ec587a;color:snow;\"><strong>".$texto['carrinho_cart_4']."</strong></td>

<td width=\"50\" style=\"text-align:left;background-color: #ec587a;color:snow;\"><strong>".$texto['carrinho_cart_5']."</strong></td>

</tr>

";

 

 

$carrinho = isset ($_SESSION['carrinho']) ? $_SESSION['carrinho'] : array();

 

 

if (count ($carrinho) == 0)

{

echo "

<tr>

<td colspan=\"5\" style=\"text-align:center\"><strong><em>

".$texto['carrinho_if_1']."</em></strong>

</td>

</tr>

</table>

<p><a href=\"?area=produtos&pedido=".$pedido."\">".$texto['carrinho_if_2']."</a></p>

";

}

else

{

for ($a = 0; $a < count($carrinho); $a++)

{

$id = $carrinho[$a]['id'];

$nome = htmlentities ($carrinho[$a]['nome']);

$preco = number_format ($carrinho[$a]['preco'], 2, ",", "");

$desc = htmlentities ($carrinho[$a]['descricao']);

$qtde = $carrinho[$a]['qtde'];

$subtotal = number_format(($carrinho[$a]['preco'] * $qtde), 2, ',', '');

 

 

echo "

<tr>

<td style=\"border-left: dashed 1px black;\" class=\"celulas\">".$nome."</td>

<td class=\"celulas\">R$ ".$preco."</td>

<td class=\"celulas\">

<form action=\"?area=carrinho&acao=alt_qtde&id=".$carrinho[$a]['id']."&pedido=".$pedido."\"method=\"post\">

<input type=\"text\" name=\"qtde_".$id."\" value=\"".$qtde."\" style=\"width: 43px;height: 43px;font-size:13px;text-align:center;border:inset 1px black;\" maxlength=\"2\" />

<input type=\"submit\" value=\"".$texto['carrinho_btn_btn']."\" class=\"c-button m-color-3\" />

</form>

</td>

<td class=\"celulas\">R$ ". $subtotal . "</td>

<td class=\"celulas\"><a class=\"c-button\" href=\"?area=carrinho&acao=remover&id=" .$id. "&pedido=".$pedido."\" onclick=\"return ConfirmarExclusaoProduto()\"><i class=\"fa fa-times\"></i></a></td>

</tr>

";

}

 

echo "

<tr>

<td style=\"text-align:right\"><strong style=\"font-size:18px\">Total:</strong><br />".$texto['carrinho_frete']."</td>

<td style=\"text-align:center\"><strong style=\"font-size:18px\">".$_SESSION['total']."</strong></td>

<td colspan=\"2\"> </td>

</tr>

</table>

<p><a class=\"c-button m-outline m-medium\" href=\"?area=produtos&pedido=".$pedido."\">".$texto['carrinho_pedido_btn_1']."</a> | <a class=\"c-button m-outline m-color-3 m-medium\" href=\"?area=finalizar\">".$texto['carrinho_pedido_btn_2']."</a></p>

";

}

}

?>

<section>

<div class=\"c-cta-message\">

<div class=\"row\">

<div class=\"col-sm-8\">

<p><?php echo $texto['pag']; ?></p>

</div>

<div class=\"col-sm-4\">

<i class="fa fa-cc-paypal fa-3x"></i><img src="images/esp.png" /><i class="fa fa-cc-amex fa-3x"></i><img src="images/esp.png" /><i class="fa fa-cc-discover fa-3x"></i><img src="images/esp.png" /><i class="fa fa-cc-visa fa-3x"></i><img src="images/esp.png" /><i class="fa fa-cc-mastercard fa-3x"></i>

</div>

</div>

</div>

</section>

</li>

<li style="display: none;">

<h2><?php echo $texto['carrinho_titulo']; ?></h2>

 

<form class="default-form" method="post" action="?acao=finalizar&pedido=<?=$pedido?>">

<div class="row">

<div class="col-md-6">

<div class="form-field">

<label for="text-1"><?php echo $texto['carrinho_entrega_nome']; ?></label>

<input id="text-1" name="nome" class="m-required" type="text" data-placeholder="">

</div>

<div class="form-field">

<label for="text-1"><?php echo $texto['carrinho_entrega_email']; ?></label>

<input id="text-1" name="email" class="m-required" type="text" data-placeholder="">

</div>

<div class="form-field">

<label for="text-1"><?php echo $texto['carrinho_entrega_endereco']; ?></label>

<input id="text-1" name="endereco" class="m-required" type="text" data-placeholder="">

</div>

<div class="form-field">

<label for="text-1"><?php echo $texto['carrinho_entrega_numero']; ?></label>

<input id="text-1" name="numero" class="m-required" type="text" data-placeholder="">

</div>

<div class="form-field">

<label for="text-1"><?php echo $texto['carrinho_entrega_bairro']; ?></label>

<input id="text-1" name="bairro" class="m-required" type="text" data-placeholder="">

</div>

<div class="form-field">

<label for="text-1"><?php echo $texto['carrinho_entrega_cidade']; ?></label>

<input id="text-1" name="cidade" class="m-required" type="text" data-placeholder="">

</div>

<div class="form-field">

<label for="text-1"><?php echo $texto['carrinho_entrega_estado']; ?></label>

<input id="text-1" name="estado" class="m-required" type="text" data-placeholder="">

</div>

<div class="form-field">

<label for="text-1"><?php echo $texto['carrinho_entrega_pais']; ?></label>

<input id="text-1" name="pais" class="m-required" type="text" data-placeholder="">

</div>

<div class="form-field">

<label for="text-1"><?php echo $texto['carrinho_entrega_zip']; ?></label>

<input id="text-1" name="zip" class="m-required" type="text" data-placeholder="">

</div>

<button name="submit" type="submit" class="c-button m-medium" id="submit"><?php echo $texto['carrinho_btn']; ?></button>

</div>

</div>

</form>

</li>

</ul>

</div>

</section>

Compartilhar este post


Link para o post
Compartilhar em outros sites
<?php
define('HOST','...');
define('USER','...');
define('PASS','...');
define('DB','...');

$conn = mysqli_connect(HOST, USER, PASS, DB);
if(mysqli_connect_errno()){
    printf("A conexão falhou: %s\n", mysqli_connect_error());
    exit();
}
$nome     		 =   stripslashes(trim($_POST['nome']));
$endereco        =   stripslashes(trim($_POST['endereco']));
$numero      	 =   stripslashes(trim($_POST['numero']));
$bairro    	     =   stripslashes(trim($_POST['bairro']));
$cidade  	     =   stripslashes(trim($_POST['cidade']));
$estado    	     =   stripslashes(trim($_POST['estado']));
$pais   	     =   stripslashes(trim($_POST['pais']));
$zip     	     =   stripslashes(trim($_POST['zip']));
$email  	     =   stripslashes(trim($_POST['email']));
$entrega = "".$endereco."-".$numero.", ".$bairro."-".$cidade."-".$estado.",".$pais."-".$zip."";

foreach ($carrinho as $key => $value) {
    $qtde = $value['qtde'];
	$total = $value['total'];
	$idPedido = $_GET['pedido'];
    $descricao = $value['descricao'];
    $inserir = "INSERT INTO `itempedido` (`idItem`, `Referencia`, `idPedido`, `quantidade`) VALUES ('', '$descricao', '$idPedido', '$qtde')";"UPDATE `pedidos` SET  emailRevendedor = ".$email." entrega = ".$entrega." dataPedido = "NOW()"  valorPedido = ".$total." WHERE idPedido=".$idPedido."";
    mysqli_query($conn, $inserir) or die(mysqli_error($conn));
}
	

você acha que assim daria certo?

esse seria o codigo que salva as arrays no mysql

Compartilhar este post


Link para o post
Compartilhar em outros sites

ja resolvi so tenho mais uma ultima duvida qual o erro aqui

$inserir = "INSERT INTO `itempedido` (`idItem`, `Referencia`, `idPedido`, `quantidade`) VALUES ('', '$descricao', '$idPedido', '$qtde')";"UPDATE pedidos SET  emailRevendedor = ".$email." entrega = ".$entrega." dataPedido = "NOW()"  valorPedido = ".$total." WHERE idPedido=".$idPedido."";

Parse error: syntax error, unexpected T_STRING in/home/storage/e/4a/40/..../public_html/...../finalizapedido.php on line 28

Compartilhar este post


Link para o post
Compartilhar em outros sites

Você não tem uma variável em atualizar, vamos separar as linhas:

$inserir = "INSERT INTO `itempedido` (`idItem`, `Referencia`, `idPedido`, `quantidade`) VALUES ('', '$descricao', '$idPedido', '$qtde')";
$data_pedido = NOW();
$atualizar = "UPDATE `pedidos` SET `emailRevendedor` = '$email', `entrega` = '$entrega', `dataPedido` = '$data_pedido', `valorPedido` = '$total' WHERE `idPedido` = $idPedido";

Compartilhar este post


Link para o post
Compartilhar em outros sites

olha aqui agora allex_carvalho nunca vi esse erro

 

Warning: mysqli_query() expects parameter 3 to be long, string given in/home/storage/e/4a/40/.../public_html/..../finalizapedido.php on line 31

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.