Working on Cart, I think its an easy question to answer. (im new to php)

Discussion in 'PHP' started by Raymond_M, Jan 6, 2011.

  1. #1
    Hi, i'm following along on a tutorial to get an idea on how to create a shopping cart from scratch, so i can then make an attempt and building my own custom one.

    I've followed the code exactly (i've spent about 2 hours making sure everything is exact) as it is in the tutorial. I can't contact the tutor, so i'm hoping someone can give me an answer here.


    I've got my index.php and my products.php files set up, and I get this error message:

    Products.php
    <?php
    
    	if(isset($_GET['action']) && $_GET['action'] == "add"){
    		$id = intval($_GET['id']);
    		if(isset($_SESSION['cart'][$id])){
    			$_SESSION['cart'][$id]['quantity']++;
    		} else {
    			$sql2 = "SELECT * FROM products WHERE id_products=($id)";
    			$query2 = mysql_query($sql2);
    			
    Line 11--------------	if(mysql_num_rows($query2) != 0){
    				$row2 = mysql_fetch_array($query2);
    				$_SESSION['cart'][$row2['id_products']] = array("quantity" => 1, "price" => $row['price']);
    				
    			} else {
    				$message = "This product id is invalid";
    			}
    		}
    	}
    
    ?>
    <h1 class="message"><?php if(isset($message)){echo $message;} ?></h1>
    <h1>Products Page</h1>
    <table>
    	<tr>
        	<th>Name</th>
            <th>Description</th>
            <th>Price</th>
            <th>Action</th>
    	</tr>
        <?php
    		$sql = "SELECT * FROM products ORDER BY name ASC";
    		$query = mysql_query($sql);
    		
    		while($row = mysql_fetch_assoc($query)){
    	?>
        	<tr>
            	<td><?php echo $row['name']; ?></td>
            	<td><?php echo $row['description']; ?></td>
                <td>$ <?php echo $row['price']; ?></td>
                <td><a href="http://localhost/shoppingcarttutorial/index.php?page=products&action=add&id=<?php echo $row['id_products']; ?>">Add To Cart</a></td>
            </tr>
            <?php
    		}
    		?>
    </table>
    PHP:
    Index.php
    <?php
    
    	session_start();
    	require_once('includes/connect.php');
    	if(isset($_GET['page'])){
    		$pages = array("products", "learncart");
    		if(in_array($_GET['page'],$pages)){
    			$page = $_GET['page'];
    		} else {
    			$page = "products";
    		}
    	} else {
    		$page = "products";
    	}
    
    ?><html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Creating A Shopping Cart</title>
    <link href="css/reset.css" rel="stylesheet" type="text/css" />	<!-- RESETS -->
    <link href="css/styles.css" rel="stylesheet" type="text/css" /> <!-- STYLES -->
    </head>
    
    <body>
    	<div id="container">
    
        	<div id="main"><?php require($page . ".php"); ?></div>
        	<div id="sidebar">World!</div>
            
    	</div>
    
    </body>
    </html>
    PHP:
    When I open in the browser and click the add to cart button, it gives me this error:

    Inside the address bare it reads:

    http://localhost/shoppingcarttutorial/index.php?page=products&action=add&id=%3Cbr%20/%3E%3Cb%3ENotice%3C/b%3E:%20%20Undefined%20index:%20id_products%20in%20%3Cb%3EC:\wamp\www\shoppingcarttutorial\products.php%3C/b%3E%20on%20line%20%3Cb%3E41%3C/b%3E%3Cbr%20/%3E
    Code (markup):
    I've been messing with it and just can't figure it out. I assume it is because i don't have the knowledge yet.

    I saw a comment that someone else was having this problem too.. it was never responded to :\

    Thank you in advance for any help you can give me.
     
    Raymond_M, Jan 6, 2011 IP
  2. tvoodoo

    tvoodoo Active Member

    Messages:
    239
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    53
    #2
    Look in phpmyadmin (or more precise in the table products).The error is occuring because the index : id_products does not exit or it has a different name.
     
    tvoodoo, Jan 6, 2011 IP
  3. Raymond_M

    Raymond_M Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    ooh man, i feel better now. I thought it was something like that but kept overlooking the issue because i thought i had copied him.. i missed the s in id_products and had mine set as id_product lol

    Thanks alot.. i actually had to go through a couple more errors before i finished it.. small stuff a newbie like me could handle though :D


    thanks alot!
     
    Raymond_M, Jan 7, 2011 IP