php.de

Zurück   php.de > Webentwicklung > PHP Einsteiger > PHP Tipps 2008

 
 
LinkBack Themen-Optionen Thema bewerten
Alt 20.04.2008, 22:29  
Neuer Benutzer
 
Registriert seit: 20.04.2008
Beiträge: 8
De Sena befindet sich auf einem aufstrebenden Ast
Standard "Add to basket" funktioniert nicht

Hallo

Neulich habe ich mir ein sehr interessantes Buch gekauft, wegen der zahlreichen Beispielen.

(Practical PHP and MySql - Building Eight Dynamic Web Applications - Jono Bacon)

Eins davon geht um ein Warenkorb, das eigentlich nicht so schlecht aussieht, wenn es eigentlich (richtig) funktionieren würde

Gebrauch werden u.a, 2 PHP-Skripts:

"functions.php" und "addtobasket.php"

Der Punkt ist, wenn ich eine Menge auf der Seite "addtobasket" eingebe, bekomme ich immer den Hinweis: "Sie haben bisher noch keine Artikel zu Ihrem Warenkorb hinzugefügt! " (auf Englisch: "You have not added anything to your shopping cart yet.)

Weiß jemand woran das Problem hier liegt?




Die Skripts sehen folgendermass aus:

functions.php
PHP-Code:
 
<?php
 
function pf_validate_number($value$function$redirect) {
if(isset(
$value) == TRUE) {
if(
is_numeric($value) == FALSE) {
$error 1;
}
 
if(
$error == 1) {
header("Location: " $redirect);
}
else {
$final $value;
}
}
else {
if(
$function == 'redirect') {
header("Location: " $redirect);
}
 
if(
$function == "value") {
$final 0;
}
}
 
return 
$final;
}
 
function 
showcart()
{
 
if(
$_SESSION['SESS_ORDERNUM'])
{
if(
$_SESSION['SESS_LOGGEDIN'])
{
$custsql "SELECT id, status from orders WHERE customer_id = " $_SESSION['SESS_USERID'] . " AND status < 2;"
$custres mysql_query($custsql);
$custrow mysql_fetch_assoc($custres);
 
$itemssql "SELECT products.*, orderitems.*, orderitems.id AS itemid FROM products, orderitems WHERE orderitems.product_id = products.id AND order_id = " $custrow['id'];
$itemsres mysql_query($itemssql);
$itemnumrows mysql_num_rows($itemsres);
}
else
{
$custsql "SELECT id, status from orders WHERE session = '" session_id() . "' AND status < 2;"
$custres mysql_query($custsql);
$custrow mysql_fetch_assoc($custres);
 
$itemssql "SELECT products.*, orderitems.*, orderitems.id AS itemid FROM products, orderitems WHERE orderitems.product_id = products.id AND order_id = " $custrow['id'];
$itemsres mysql_query($itemssql);
$itemnumrows mysql_num_rows($itemsres);

}
else
{
$itemnumrows 0;

 
if(
$itemnumrows == 0)
{
echo 
"You have not added anything to your shopping cart yet.";
 
}
else

echo 
"<table cellpadding='10'>";
echo 
"<tr>";
echo 
"<td></td>";
echo 
"<td><strong>Item</strong></td>";
echo 
"<td><strong>Quantity</strong></td>";
echo 
"<td><strong>Unit Price</strong></td>";
echo 
"<td><strong>Total Price</strong></td>";
echo 
"<td></td>";
echo 
"</tr>";
 
while(
$itemsrow mysql_fetch_assoc($itemsres))

$quantitytotal $itemsrow['price'] * $itemsrow['quantity'];
echo 
"<tr>";
 
if(empty(
$itemsrow['image'])) {
echo 
"<td><img src='./productimages/dummy.jpg' width='50' alt='" $itemsrow['name'] . "'></td>";
}
else {
echo 
"<td><img src='./productimages/" $itemsrow['image'] . "' width='50' alt='" $itemsrow['name'] . "'></td>";
}
 
// echo "<td><img src='./productimages/" . $itemsrow['image'] . ".jpg' alt='" . $itemsrow['name'] . "' width='50'></td>";
echo "<td>" $itemsrow['name'] . "</td>";
echo 
"<td>" $itemsrow['quantity'] . "</td>";
echo 
"<td><strong>&pound;" sprintf('%.2f'$itemsrow['price']) . "</strong></td>";
echo 
"<td><strong>&pound;" sprintf('%.2f'$quantitytotal) . "</strong></td>";
echo 
"<td>[<a href='" $config_basedir "delete.php?id=" $itemsrow['itemid'] . "'>X</a>]</td>";
echo 
"</tr>";
 
$total $total $quantitytotal;
$totalsql "UPDATE orders SET total = " $total " WHERE id = " $_SESSION['SESS_ORDERNUM']; 
$totalres mysql_query($totalsql);

 
echo 
"<tr>";
echo 
"<td></td>";
echo 
"<td></td>";
echo 
"<td></td>";
echo 
"<td>TOTAL</td>";
echo 
"<td><strong>&pound;" sprintf('%.2f'$total) . "</strong></td>";
echo 
"<td></td>";
echo 
"</tr>";
 
echo 
"</table>";
 
}
}
und "addtobasket.php":

PHP-Code:
<?php
session_start
();
 
require(
"db.php");
require(
"functions.php");
 
$validid pf_validate_number($_GET['id'], 
"redirect"$config_basedir);
 
$prodsql "SELECT * FROM products WHERE id = " $_GET['id'] . ";";
$prodres mysql_query($prodsql);
$numrows mysql_num_rows($prodres); 
$prodrow mysql_fetch_assoc($prodres);
 
if(
$numrows == 0)
{
header("Location: " $config_basedir);
}
else
{
if(
$_POST['submit'])
{
if(
$_SESSION['SESS_ORDERNUM'])
{
$itemsql "INSERT INTO orderitems(order_id, 
product_id, quantity) VALUES("
$_SESSION['SESS_ORDERNUM'] . ", " 
$_GET['id'] . ", "
$_POST['amountBox'] . ")";
mysql_query($itemsql);
}
else
{
if(
$_SESSION['SESS_LOGGEDIN'])
{
$sql "INSERT INTO orders(customer_id, 
registered, date) VALUES("
$_SESSION['SESS_USERID'] . ", 1, NOW())";
mysql_query($sql);
session_register("SESS_ORDERNUM");
$_SESSION['SESS_ORDERNUM'] = mysql_insert_id();
 
$itemsql "INSERT INTO 
orderitems(order_id, product_id, quantity) VALUES("
$_SESSION['SESS_ORDERNUM'] . ", " $_GET['id'] . ", "
$_POST['amountBox'] . ")";
 
mysql_query($itemsql);
}
else
{
$sql "INSERT INTO orders(registered, 
date, session) VALUES("
 
"0, NOW(), '" session_id() . "')";
mysql_query($sql);
session_register("SESS_ORDERNUM");
$_SESSION['SESS_ORDERNUM'] = mysql_insert_id();
 
$itemsql "INSERT INTO 
orderitems(order_id, product_id, quantity) VALUES("
$_SESSION['SESS_ORDERNUM'] . ", " $_GET['id'] . ", "
$_POST['amountBox'] . ")";
 
mysql_query($itemsql);

}
 
 
$totalprice $prodrow['price'] * $_POST['amountBox'] ;
 
$updsql "UPDATE orders SET total = total + " 
$totalprice " WHERE id = " 
$_SESSION['SESS_ORDERNUM'] . ";";
mysql_query($updres);
 
header("Location: " $config_basedir "showcart.php");
}
else
{
require(
"header.php");
 
echo 
"<form action='addtobasket.php?id=" 
$_GET['id'] . "' method='POST'>";
echo 
"<table cellpadding='10'>";
 
 
echo 
"<tr>";
if(empty(
$prodrow['image'])) {
echo 
"<td><img 
src='./productimages/dummy.jpg' width='50' alt='" 
$prodrow['name'] . "'></td>";
}
else {
echo 
"<td>
<img src='./productimages/" 
$prodrow['image'
"' width='50' alt='" $prodrow['name'
"'></td>";
}
 
echo 
"<td>" $prodrow['name'] . "</td>";
echo 
"<td>Select Quantity <select name='amountBox'>";
 
for(
$i=1;$i<=100;$i++)
{
echo 
"<option>" $i "</option>";
}
 
echo 
"</select></td>";
echo 
"<td><strong>&pound;" 
sprintf('%.2f'$prodrow['price']) 
"</strong></td>";
echo 
"<td><input type='submit' 
name='submit' value='Add to basket'></td>"
;
echo 
"</tr>";
 
echo 
"</table>";
echo 
"</form>";
}
}
 
require(
"footer.php");
?>
?>

Geändert von drieling (21.04.2008 um 08:26 Uhr). Grund: PHP Tags hinzugefügt. Bitte beim nächsten mal dran denken ;)
De Sena ist offline  
Sponsor Mitteilung
PHP Code Flüsterer

Registriert seit: 21.08.2005
Beiträge: 4682
PHP-Kenntnisse:
Fortgeschritten

Alt 20.04.2008, 23:19  
Erfahrener Benutzer
 
Benutzerbild von Montellese
 
Registriert seit: 30.07.2007
Beiträge: 541
Montellese befindet sich auf einem aufstrebenden Ast
Montellese eine Nachricht über MSN schicken
Standard

Könntest du vielleicht die BBCodes für PHP-Code-Hightlighting ([php ] ohne den Leerschlag) verwenden, damit der Code einfacher lesbar ist und am besten auch gleich noch alle Funktionen, die nichts mit dem addtobasket zu tun haben, einfach mal weglassen?

So wie ich dich verstanden habe, schreibst du irgendwo in der addtobasket.php hardcoded einen Wert rein, wieviele Elemente im Warenkorb sind. Wenn dies der Fall ist, dann kann das auch nicht funktionieren, da die Funktion ja überprüft, ob irgendwelche Produkte in der Warenkorbtabelle der Datenbank vorhanden sind, was in diesem Fall nicht der Fall wäre => die genannte Fehlermeldung.
Montellese ist offline  
Alt 20.04.2008, 23:26  
Neuer Benutzer
 
Registriert seit: 20.04.2008
Beiträge: 8
De Sena befindet sich auf einem aufstrebenden Ast
Standard Fehlermeldung

...Am bestens würde ich dir die ganze FOLDER mit Scripts per Email schicken, damit Du dir alles besser analisieren kannst...

Oder sind URL-Links hier erlaubt? (Dann schicke ich sie Dir über meine Webseite)

Grüße!
De Sena ist offline  
Alt 20.04.2008, 23:37  
Neuer Benutzer
 
Registriert seit: 20.04.2008
Beiträge: 8
De Sena befindet sich auf einem aufstrebenden Ast
Standard

Hier ist die Adresse:

http://metatranslations.com/shoppingkarte.zip

Ich hoffe, dass meine ZIP-Software funktioniert wieder
De Sena ist offline  
 


Themen-Optionen
Thema bewerten
Thema bewerten:

Forumregeln
Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are an
Gehe zu

Ähnliche Themen
Thema Autor Forum Antworten Letzter Beitrag
[JS] onClick funktioniert erst beim 2. Mal Klicken. DER_Brain HTML, Usability und Barrierefreiheit 2 08.07.2008 10:47
Thumbnail funktioniert nicht --> GD2 Installation? Plague PHP Tipps 2008 8 14.09.2007 11:05
Session funktioniert nicht bei session.use_cookies = Off Quagga PHP Tipps 2007 13 13.01.2007 18:27
Lokal funktioniert alles -> auf Server fast nichts PsychoEagle Datenbanken 2 14.08.2006 08:43
schleife funktioniert nicht?! $$$ ThiKool $$$ PHP Tipps 2006 3 18.07.2006 07:52
Nach Einfügugng der Sessions funktioniert mein Program nicht PHP-Fortgeschrittene 1 02.10.2005 06:13
[Erledigt] Upload funktioniert manchmal, manchmal nicht HTML, Usability und Barrierefreiheit 9 29.09.2005 12:32
Neues Fenster automatisch öffnen funktioniert nicht ? HTML, Usability und Barrierefreiheit 3 07.08.2005 23:43
Kontakrformular funktioniert nur zum Teil PHP Tipps 2005-2 12 18.07.2005 11:24
pear mime mail funktioniert nicht überall PHP Tipps 2005 4 05.01.2005 20:42
Problem: MySQL Query funktioniert nicht. Wieso? PHP Tipps 2004-2 3 24.12.2004 13:58
Script funktioniert bei include() nicht mehr :-( seb-web HTML, Usability und Barrierefreiheit 9 24.12.2004 12:35
Dateien erstellen oder schreiben funktioniert nicht im Web PHP Tipps 2004-2 1 05.12.2004 19:39
Session funktioniert nicht PHP Tipps 2004 3 15.08.2004 13:08
Header Location funktioniert nicht PHP Tipps 2004 10 12.08.2004 17:11

Besucher kamen über folgende Suchanfragen bei Google auf diese Seite
$itemnumrows = mysql_num_rows($itemsres);, function pf_validate_number($value, $function, $redirect), script für session aus shoppingcard in session warenkorb, session_register(\sess_ordernum\);, echo \<form action=\'addtobasket.php?id=\. $_get[\'id\'] . \\' method=\'post\'>\;, [<a href=\'\ . $config_basedir . \delete.php?id=\ . $itemsrow[\'itemid\'] . \\'>x</a>], jono bacon $_session[\'sess_loggedin\']=1;, showcart.php \you have not added anything to your shopping cart yet.\ problem, addtobasket.php showcart.php, config_basedir practical php and mysql

Alle Zeitangaben in WEZ +2. Es ist jetzt 18:30 Uhr.




Powered by vBulletin® Version 3.7.2 (Deutsch)
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
Aprilia-Forum, Aquaristik-Forum, Liebeskummer-Forum, Zierfisch-Forum, Geizkragen-Forum