Ir para conteúdo

POWERED BY:

Arquivado

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

Piaui

Formulario modo isset POST para finalizar comprar

Recommended Posts

Olá Galera tenho esse carrinho de compras em php estou tentando gravar do dados dele quando clica no botão Finalizar para a tabela saida_produto. obs: ID_VENDA_SAIDA VEM DO GET.


TABELA:



CREATE TABLE IF NOT EXISTS `saida_produto` (

`id_saida` int(11) NOT NULL AUTO_INCREMENT,

`id_venda_saida` int(11) NOT NULL,

`produto_saida` varchar(100) NOT NULL,

`quantidade_vendida` varchar(100) NOT NULL,

PRIMARY KEY (`id_saida`)

) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=21 ;





CODIGO DO CARRINHO:


<?php require_once('Connections/VM.php'); ?>

<?php

if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")

{

$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;


$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);


switch ($theType) {

case "text":

$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

break;

case "long":

case "int":

$theValue = ($theValue != "") ? intval($theValue) : "NULL";

break;

case "double":

$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";

break;

case "date":

$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";

break;

case "defined":

$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;

break;

}

return $theValue;

}

}


$colname_Recordset1 = "-1";

if (isset($_GET['id_venda'])) {

$colname_Recordset1 = $_GET['id_venda'];

}

mysql_select_db($database_VM, $VM);

$query_Recordset1 = sprintf("SELECT * FROM vendas_cartao WHERE id_venda = %s", GetSQLValueString($colname_Recordset1, "int"));

$Recordset1 = mysql_query($query_Recordset1, $VM) or die(mysql_error());

$row_Recordset1 = mysql_fetch_assoc($Recordset1);

$totalRows_Recordset1 = mysql_num_rows($Recordset1);


$colname_v_p = "-1";

if (isset($_GET['id_venda'])) {

$colname_v_p = $_GET['id_venda'];

}

mysql_select_db($database_VM, $VM);

$query_v_p = sprintf("SELECT * FROM saida_produto WHERE id_venda_saida = %s", GetSQLValueString($colname_v_p, "int"));

$v_p = mysql_query($query_v_p, $VM) or die(mysql_error());

$row_v_p = mysql_fetch_assoc($v_p);

$totalRows_v_p = mysql_num_rows($v_p);


session_start();


if(!isset($_SESSION['carrinho'])){

$_SESSION['carrinho'] = array();

}


//adiciona produto


if(isset($_GET['acao'])){


//ADICIONAR CARRINHO

if($_GET['acao'] == 'add'){

$id = intval($_GET['id']);

if(!isset($_SESSION['carrinho'][$id])){

$_SESSION['carrinho'][$id] = 1;

}else{

$_SESSION['carrinho'][$id] += 1;

}

}


//REMOVER CARRINHO

if($_GET['acao'] == 'del'){

$id = intval($_GET['id']);

if(isset($_SESSION['carrinho'][$id])){

unset($_SESSION['carrinho'][$id]);

}

}


//ALTERAR QUANTIDADE

if($_GET['acao'] == 'up'){

if(is_array($_POST['prod'])){

foreach($_POST['prod'] as $id => $qtd){

$id = intval($id);

$qtd = intval($qtd);

if(!empty($qtd) || $qtd <> 0){

$_SESSION['carrinho'][$id] = $qtd;

}else{

unset($_SESSION['carrinho'][$id]);

}

}

}

}


}



?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Carrinho de Compras</title>

</head>


<body>

<table>

<caption>

Carrinho de Compras

</caption>

<thead>

<tr>

<th width="244">Produto</th>

<th width="79">Quantidade</th>

<th width="89">Preço</th>

<th width="100">SubTotal</th>

<th width="64">Remover</th>

</tr>

</thead>


<tfoot>

<tr>


<tr>


</tfoot>


<tbody>

<?php

if(count($_SESSION['carrinho']) == 0){

echo '<tr><td colspan="5">Não há produto no carrinho</td></tr>';

}else{

require("conexao.php");

$total = 0;

foreach($_SESSION['carrinho'] as $id => $qtd){

$sql = "SELECT * FROM produtos WHERE id= '$id'";

$qr = mysql_query($sql) or die(mysql_error());

$ln = mysql_fetch_assoc($qr);


$nome = $ln['nome'];

$preco = number_format($ln['preco'], 2, ',', '.');

$sub = number_format($ln['preco'] * $qtd, 2, ',', '.');


$total += $ln['preco'] * $qtd;


echo '<tr>

<td>'.$nome.'</td>

<td><input type="text" size="3" name="prod['.$id.']" value="'.$qtd.'" /></td>

<td>R$ '.$preco.'</td>

<td>R$ '.$sub.'</td>

<td><a href="?acao=del&id='.$id.'">Remove</a></td>

</tr>';

}

$total = number_format($total, 2, ',', '.');

echo '<tr>

<td colspan="4">Total</td>

<td>R$ '.$total.'</td>

</tr>';

}

?>

</tbody>

</form>

</table>

<?php

require("conexao.php");


$sql = "SELECT * FROM produtos";

$qr = mysql_query($sql) or die(mysql_error());

while($ln = mysql_fetch_assoc($qr)){

echo '<br>'.$ln['nome'].'</br>';

echo 'Preço : R$ '.number_format($ln['preco'], 2, ',', '.').'<br />';

echo '<a href="carrinho.php?id_venda='.$row_Recordset1['id_venda'].'&acao=add&id='.$ln['id'].'">ADICIONAR</a>';

echo '<br /><hr />';

}

?>

<?php

if(isset($_POST['enviar'])){


$query_Recordset1 = mysql_query("INSERT INTO saida_produto(id_venda_saida, quantidade_vendida) VALUES('$GET.id_venda', '$qtd')");


echo "<script>alert('OK')</script>;


}


?>


<form action="" enctype="multipart/form-data" method="post">

<input align="right" type="submit" name="enviar" value="FINALIZAR" />

</form>

</body>

</html>


<?php

mysql_free_result($Recordset1);


mysql_free_result($v_p);

?>

Compartilhar este post


Link para o post
Compartilhar em outros sites

Parse error: syntax error, unexpected '" enctype="' (T_CONSTANT_ENCAPSED_STRING), expecting ',' or ';' in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\VM\carrinho.php on line 187

Compartilhar este post


Link para o post
Compartilhar em outros sites

Esta faltando fechar as aspas:

echo "<script>alert('OK')</script>;

Compartilhar este post


Link para o post
Compartilhar em outros sites
foreach($_SESSION['carrinho'] as $prod => $qtd);
$query_Recordset1 = mysql_query("INSERT INTO saida_produto(id_venda_saida, produto_saida, quantidade_vendida) VALUES('$id_venda', '$id', '$qtd')");
endforeach;

 

echo "<script>alert('OK')</script>";

 

Parse error: syntax error, unexpected 'endforeach' (T_ENDFOREACH) in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\VM\carrinho.php on line 183

Compartilhar este post


Link para o post
Compartilhar em outros sites

o problema é que quando eu seleciono dois produtos ex id=1 e id=2 ele grava a penas id do produto 2. preciso que grave os dois produtos...

Compartilhar este post


Link para o post
Compartilhar em outros sites

muda o ; do foreach por :

 

de:

foreach($_SESSION['carrinho'] as $prod => $qtd);

para:

foreach($_SESSION['carrinho'] as $prod => $qtd):

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.