Scholieren.com forum

Scholieren.com forum (https://forum.scholieren.com/index.php)
-   Software & Hardware (https://forum.scholieren.com/forumdisplay.php?f=20)
-   -   [PHP] Wat gaat er mis? (https://forum.scholieren.com/showthread.php?t=437346)

-niels- 04-04-2003 13:24

[PHP] Wat gaat er mis?
 
Een expert ben ik nog lang niet, maar ik krijg wel verdacht veel foutmeldingen hier als ik naar 't mandje wil...

Ik heb een tutorial doorlopen, deze: http://www.devarticles.com/art/1/132/0

en opzich ging dat allemaal lekker tot de 1 na laatste pagina, de ShowCart-function, nah begrijpelijk dat ze in een tutorial niet alles voorkauwen maar ik kom er niet uit... het script wat ik er nu uit krijg (en die foutmeldingen geeft) is als volgt:

PHP-code:

<?php 

    
include("db.php"); 
    
    switch(
$_GET["action"]) 
    { 
    case 
"add_item"
        { 
        
AddItem($_GET["id"], $_GET["qty"]); 
        
ShowCart(); 
        break; 
        } 
    case 
"update_item"
        { 
        
UpdateItem($_GET["id"], $_GET["qty"]); 
        
ShowCart(); 
        break; 
        } 
    case 
"remove_item"
        { 
        
RemoveItem($_GET["id"]); 
        
ShowCart(); 
        break; 
        } 
    default: 
        { 
        
ShowCart(); 
        } 
    } 

function 
AddItem($itemId$qty) {
    
$result mysql_query("select count(*) from cart where cookieId = '" GetCartId() . "' and itemId = $itemId"); 
    
$row mysql_fetch_row($result); 
    
$numRows $row[0]; 
    if(
$numRows == 0
        { 
        
// This item doesn't exist in the users cart, 
        // we will add it with an insert query 
        
@mysql_query("insert into cart(cookieId, itemId, qty) values('" GetCartId() . "', $itemId$qty)"); 
        } 
    else 
        { 
        
// This item already exists in the users cart, 
        // we will update it instead 
        
UpdateItem($itemId$qty); 
        } }

function 
UpdateItem($itemId$qty) {
    
mysql_query("update cart set qty = $qty where cookieId = '" GetCartId() . "' and itemId = $itemId"); 
}
function 
RemoveItem($itemId) {
    
mysql_query("delete from cart where cookieId = '" GetCartId() . "' and itemId = $itemId"); 
}
function 
ShowCart() {
    
$result mysql_query("select * from cart inner join items on cart.itemId = items.itemId where cart.cookieId = '" GetCartId() . "' order by items.itemName asc"); 
    while(
$row mysql_fetch_array($result)) 

// Increment the total cost of all items 
$totalCost += ($row["qty"] * $row["itemPrice"]); 
?>
<script language="JavaScript"> 

function UpdateQty(item) 

itemId = item.name; 
newQty = item.options[item.selectedIndex].text; 

document.location.href = 'cart.php?action=update_item&id='+itemId+'&qty='+newQty; 


</script> 

<tr> 
<td width="15%" height="25"> 
<font face="verdana" size="1" color="black"> 
<select name="<?php echo $row["itemId"]; ?>" onChange="UpdateQty(this)"> 
<?php 

for($i 1$i <= 20$i++) 

echo 
"<option "
if(
$row["qty"] == $i

echo 
" SELECTED "

echo 
">" $i "</option>"

?> 
</select> 
</font> 
</td> 
<td width="55%" height="25"> 
<font face="verdana" size="1" color="black"> 
<?php echo $row["itemName"]; ?> 
</font> 
</td> 
<td width="20%" height="25"> 
<font face="verdana" size="1" color="black"> 
$<?php echo number_format($row["itemPrice"], 2"."","); ?> 
</font> 
</td> 
<td width="10%" height="25"> 
<font face="verdana" size="1" color="black"> 
<a href="cart.php?action=remove_item&id=<?php echo $row["itemId"]; ?>">Remove</a> 
</font> 
</td> 
</tr> 
<?php 

// Increment the total cost of all items 
$totalCost += ($row["qty"] * $row["itemPrice"]); ?>

<select name="<?php echo $row["itemId"]; ?>" onChange="UpdateQty(this)"> 
<?php 
for($i 1$i <= 20$i++) 

echo 
"<option "
if(
$row["qty"] == $i

echo 
" SELECTED "

echo 
">" $i "</option>"

?> 
</select> 
<tr> 
<td width="100%" colspan="4"> 
<hr size="1" color="red" NOSHADE> 
</td> 
</tr> 
<tr> 
<td width="70%" colspan="2"> 
<font face="verdana" size="1" color="black"> 
<a href="products.php">&lt;&lt; Keep Shopping</a> 
</font> 
</td> 
<td width="30%" colspan="2"> 
<font face="verdana" size="2" color="black"> 
<b>Total: $<?php echo number_format($totalCost2"."","); ?></b> 
</font> 
</td> 
</tr> <?PHP ?>

Wat vergeet ik?
of wat doe ik fout?
of als dat nogal onmogelijk is om 1-2-3 te zien mss een beetje verklaring van de foutmeldingen (of een verwijzingen naar een site waar dat een beetje staat uitgelegd)

Koen 04-04-2003 13:28

Die hele pagina bestaat niet, zonder foutmelding wordt 't wel heel erg zoeken naar een speld in een hooiberg.. :)

-niels- 04-04-2003 13:45

Citaat:

Koen schreef op 04-04-2003 @ 14:28:
Die hele pagina bestaat niet, zonder foutmelding wordt 't wel heel erg zoeken naar een speld in een hooiberg.. :)
fixed

Manuzhai 04-04-2003 14:08

Ten eerste gaat de verbinding met je MySQL verkeerd (je hebt bijvoorbeeld geen call naar mysql_connect(), of alleen eentje zonder argumenten), omdat je een verkeerde gebruiker opgeeft. Ten tweede ben je kennelijk dingen naar je output aan het schrijven alvorens je sessie te starten of header()'s aan te roepen. Dit kan bijvoorbeeld alleen al komen doordat je een newline voor je <?php hebt staan.

-niels- 04-04-2003 14:18

nah ik heb dat db.php bestand wat wordt aangeroepen in het begin... gewoon van die tutorial-page gehaald... met mijn eigen gegevens...

PHP-code:

 <?php 

// This page contains the connection routine for the 
// database as well as getting the ID of the cart, etc 

$dbServer "localhost"
$dbUser "juh"
$dbPass "juh"
$dbName "juh"

function 
ConnectToDb($server$user$pass$database

// Connect to the database and return 
// true/false depending on whether or 
// not a connection could be made. 

$s = @mysql_connect($server$user$pass); 
$d = @mysql_select_db($database$s); 

if(!
$s || !$d
return 
false
else 
return 
true


function 
GetCartId() 

// This function will generate an encrypted string and 
// will set it as a cookie using set_cookie. This will 
// also be used as the cookieId field in the cart table 

if(isset($_COOKIE["cartId"])) 

return 
$_COOKIE["cartId"]; 

else 

// There is no cookie set. We will set the cookie 
// and return the value of the users session ID 

session_start(); 
setcookie("cartId"session_id(), time() + ((3600 24) * 30)); 
return 
session_id(); 



?>

en daar staan toch die connect en die sessions in??? :confused:

Martin 04-04-2003 14:25

PHP-code:

 <?php 

// This page contains the connection routine for the 
// database as well as getting the ID of the cart, etc 

$server "localhost"
$user "juh"
$pass "juh"
$database "juh"

function 
ConnectToDb($server$user$pass$database

// Connect to the database and return 
// true/false depending on whether or 
// not a connection could be made. 

$s = @mysql_connect($server$user$pass); 
$d = @mysql_select_db($database$s);

Dit zal wel werken :)

-niels- 04-04-2003 15:08

Citaat:

Martin schreef op 04-04-2003 @ 15:25:
PHP-code:

 <?php 

// This page contains the connection routine for the 
// database as well as getting the ID of the cart, etc 

$server "localhost"
$user "juh"
$pass "juh"
$database "juh"

function 
ConnectToDb($server$user$pass$database

// Connect to the database and return 
// true/false depending on whether or 
// not a connection could be made. 

$s = @mysql_connect($server$user$pass); 
$d = @mysql_select_db($database$s);

Dit zal wel werken :)


Ok 't zal wel aan de kleurtjes liggen maar ik zie 't verschil niet met wat ik opgaf :/

[edit] zie 't al... ik zal ff proberen[/edit]
[edit] maar dan werkt die products.php zelfs niet... :/ [/edit]

Manuzhai 04-04-2003 18:11

Citaat:

Martin schreef op 04-04-2003 @ 15:25:
Dit zal wel werken :)
Uhm, je hebt alleen de namen van de variabelen binnen de functies aangepast? Dat maakt uiteraard geen fuck uit, het gaat erom welke variabelen je meegeeft aan de functie.

-niels- 04-04-2003 18:13

voor de geinteresseerde... products.php

PHP-code:

<?php 

// This page will list all of the items 
// from the items table. Each item will have 
// a link to add it to the cart 

include("db.php"); 

// Get a connection to the database 
$cxn = @ConnectToDb($dbServer$dbUser$dbPass$dbName); 
$result mysql_query("select * from items order by itemName asc"); 
?> 

<table width='75%'>

<?php 
while($row mysql_fetch_array($result)) 
?>
<tr> 
<td width="30%" height="25"> 
<font face="verdana" size="1" color="black"> 
<?php echo $row["itemName"]; ?> 
</font> 
</td> 
<td width="10%" height="25"> 
<font face="verdana" size="1" color="black"> 
$<?php echo $row["itemPrice"]; ?> 
</font> 
</td> 
<td width="50%" height="25"> 
<font face="verdana" size="1" color="black"> 
<?php echo $row["itemDesc"]; ?> 
</font> 
</td> 
<td width="10%" height="25"> 
<font face="verdana" size="1" color="black"> 
<a href="cart.php?action=add_item&id=<?php echo $row["itemId"]; ?>&qty=1">Add Item</a> 
</font> 
</td> 
<?php ?>
</tr> 
<tr> 
<td width="100%" colspan="4"> 
<hr size="1" color="red" NOSHADE> 
</td> 
</tr> 
<tr> 
<td width="100%" colspan="4"> 
<font face="verdana" size="1" color="black"> 
<a href="cart.php">Your Shopping Cart &gt;&gt;</a> 
</font> 
</td> 
</tr> 


</table> 
</body> 
</html>

als iemand een antwoord weet... help me :o

Manuzhai 04-04-2003 18:15

Doe eens een zipje ofzo ergens plakken, ik kan in mijn eigen editor beter lezen. :)

-niels- 04-04-2003 18:53

Citaat:

Manuzhai schreef op 04-04-2003 @ 19:15:
Doe eens een zipje ofzo ergens plakken, ik kan in mijn eigen editor beter lezen. :)
komt eraan vanaaf... :)

-niels- 05-04-2003 08:29

http://paperz.endoria.net/cart.zip

voor de database (mocht dat nodig zijn) moet je ff op die tutorial-page kijken...

Manuzhai 05-04-2003 13:14

Okay, ten eerste moet je in cart.php wel ergens (in het begin) ConnectToDb() aanroepen, anders kun je uiteraard geen mysql_query()'s gebruiken. Ten tweede lijkt het me beter als je een keer helemaal in het begin van cart.php de waarde uit GetCartId() opslaat in een variabele, waarna je die variabele in de rest van je script kunt gebruiken.

-niels- 08-04-2003 09:12

Ok ik heb 'm nou zover dat ie nog maar 4 foutmeldingen geeft...
maar ik heb geen idee waarom, 't heeft te maken met die session...

PHP-code:


// There is no cookie set. We will set the cookie 
// and return the value of the users session ID 
        
session_start(); //regel 41
setcookie("cartId"session_id(), time() + ((3600 24) * 30));  //regel 42
return session_id(); 



-niels- 08-04-2003 10:16

fixed...
er zaten wat spaties in me script..

en er stond in me php boek:
"The single most common error in using cookies is trying to set a cookie after some regular HTML content" (in dit geval spaties)

Manuzhai 08-04-2003 11:11

Citaat:

-niels- schreef op 08-04-2003 @ 11:16:
en er stond in me php boek:
"The single most common error in using cookies is trying to set a cookie after some regular HTML content" (in dit geval spaties)

Hmhm, daarom zei ik dat je alleen helemaal bovenaan die getcartid() moest aanroepen, en daarna niet meer.

-niels- 08-04-2003 14:14

Nah goed het werkt allemaal...
alleen nou wil ik een stapje verder...
is 't mogelijk om het winkelwagentje op dezelfde pagina te krijgen als de producten lijst...
dus kan ik zonder al teveel moeite dat script onder het productenscript uitvoeren?

(jaja lekker vaag )

Manuzhai 08-04-2003 17:44

Vast wel... Ik kan niet direct een reden bedenken waarom het niet zou kunnen. :)


Alle tijden zijn GMT +1. Het is nu 13:28.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.