PB PHP Quantité produit/panier
Résolu
kimaochris
Messages postés
8
Statut
Membre
-
kimaochris Messages postés 8 Statut Membre -
kimaochris Messages postés 8 Statut Membre -
Bonjour,
Je viens "d'hériter" de la gestion d'un site marchand.
Or en faisant le tour du propriétaire je remarque le pb suivant:
Si on commande un produit (quantité 1) aucun pb.
Si on commande deux produits (quantité 1 chacun) aucun pb, ça s'additionne.
Si je commande un produit mais quantité 2 par exemple le calcul est faussé:
un produit à 8.6€ me donne un total de 17€ au lieu de 17.20€.
Je ne suis vraiment pas doué pour la prog (j'étais juste le webdesigner du site).
Donc si quelqu'un veut bien regarder le code suivant et me dire où est l'erreur,
je lui serais reconnaissant.
<?
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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;
}
include("connect_bdd.php");
$i = 0;
foreach ($_POST['id'] as $id_panier)
$prix_tot = ($_POST['quantite'][$i] * $_POST['prix_unit'][$i];
echo $prix_tot . " - " ;
if ($_POST['quantite'][$i] != 0) {
$sql = sprintf("UPDATE panier SET quantite=%s,prix_tot=%s WHERE id_panier=%s",
GetSQLValueString($_POST['quantite'][$i], "int"),
GetSQLValueString($prix_tot, "int"),
GetSQLValueString($id_panier, "int"));
$rs = mysql_query($sql, $db) or die(mysql_error());
} else {
$del = sprintf("DELETE FROM panier WHERE id_panier = %s",
GetSQLValueString($id_panier, "int"));
$delok = mysql_query($del, $db) or die(mysql_error());
}
$i++;
}
include("panier.php");
?>
Je viens "d'hériter" de la gestion d'un site marchand.
Or en faisant le tour du propriétaire je remarque le pb suivant:
Si on commande un produit (quantité 1) aucun pb.
Si on commande deux produits (quantité 1 chacun) aucun pb, ça s'additionne.
Si je commande un produit mais quantité 2 par exemple le calcul est faussé:
un produit à 8.6€ me donne un total de 17€ au lieu de 17.20€.
Je ne suis vraiment pas doué pour la prog (j'étais juste le webdesigner du site).
Donc si quelqu'un veut bien regarder le code suivant et me dire où est l'erreur,
je lui serais reconnaissant.
<?
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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;
}
include("connect_bdd.php");
$i = 0;
foreach ($_POST['id'] as $id_panier)
$prix_tot = ($_POST['quantite'][$i] * $_POST['prix_unit'][$i];
echo $prix_tot . " - " ;
if ($_POST['quantite'][$i] != 0) {
$sql = sprintf("UPDATE panier SET quantite=%s,prix_tot=%s WHERE id_panier=%s",
GetSQLValueString($_POST['quantite'][$i], "int"),
GetSQLValueString($prix_tot, "int"),
GetSQLValueString($id_panier, "int"));
$rs = mysql_query($sql, $db) or die(mysql_error());
} else {
$del = sprintf("DELETE FROM panier WHERE id_panier = %s",
GetSQLValueString($id_panier, "int"));
$delok = mysql_query($del, $db) or die(mysql_error());
}
$i++;
}
include("panier.php");
?>
Configuration: Windows XP Firefox 3.5.2
4 réponses
-
salut
essaie ça voir
$prix_tot = doubleval($_POST['quantite'][$i]) * doubleval($_POST['prix_unit'][$i]);
cdlt.
;-) -
Salut,
Je te remercie pour ton aide mais hélas, ça ne fonctionne toujours pas. -
Slt
ok je me dit moi que le pb se trouve dans es parametres de ta fnction GetSQLValueString(...)remplace cette section de code GetSQLValueString($_POST['quantite'][$i], "int"), GetSQLValueString($prix_tot, "int"), GetSQLValueString($id_panier, "int")); par celle ci GetSQLValueString($_POST['quantite'][$i], "int"), GetSQLValueString($prix_tot, "double"), <=== origine du probleme GetSQLValueString($id_panier, "int"));
tu fais passer le prix en entier donc il va trocnquer la partie decimal, passe le en double voir
cdlt.
;-) -
Alors là, je dis tout simplement BRAVO
Ca fonctionne nickel, je te remercie sincèrement.