Hi, how to add a checkout function to this code? In the picture you can see, I had call the member list and cart function together. I want it automatic execute INSERT INTO query when I click the checkout function. I already make a cart and member list and both are work fine. I don't know how to insert the cart item and the selected member detail to the database. Anyone can show me where should I add the code and how to write the code?

I don't know how to call my cart item name,quantity and total when I want to do the INSERT query =(

Untitled-25

<?php
include('./include/config.php');
include('./include/loginverify.php');

?>
<!DOCTYPE html>
<!-- Website template by freewebsitetemplates.com -->
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Artist Website Template</title>
    <link rel="stylesheet" href="images/css/style.css" type="text/css" charset="utf-8" />    
</head>

<body>
    <span id="background"></span>
    <div id="page">
        <div id="sidebar">
            <div id="logo">
                <a href="index.html">Welcome <p><em><?php echo $_SESSION['username']?></em></p></a>
            </div> <!-- /#logo -->
            <ul id="navigation">
                <li class="selected"><a href="home.php">Product List</a></li>
                <li><a href="member_list.php">Member List</a></li>
                <li><a href="top10.php">Top 10</a></li>
                <li><a href="Report">Report</a></li>
                <li><a href="logout.php">log out</a></li>
            </ul> <!-- /#navigation -->
            <ul id="connect">
                <li><a href="http://facebook.com/" target="_blank" class="facebook"></a></li>
                <li><a href="http://twitter.com/" target="_blank" class="twitter"></a></li>
                <li><a href="" class="link-us"></a></li>
            </ul> <!-- /#connect -->
            <div class="footer">
                &copy; Copyright &copy; 2011.<br/>
                <a href="index.html">D&W CD Rental Store</a> all rights reserved.
            </div> <!-- /.footer -->
        </div> <!-- /#sidebar -->

        <div id="contents">
        <!-- HTML Codes by Quackit.com -->
<p style="text-align:center;"><span style="font-family:Arial;font-size:19px;font-style:normal;font-weight:normal;text-decoration:none;text-transform:uppercase;color:000000;background-color:ffffff;">Product List</span>
<table class="hovertable">
<tr>
   <th>ID</th>
   <th>Name</th>
   <th>Quantity</th>
   <th>Subtotal</th>
</tr>
<tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">
<?php


//determind add action
if(isset($_GET['action']) && ($_GET['action'] == 'add')){
    //determind ID from url
    if(isset($_GET['id'])){
        //determind the value is positive or not
        if($_GET['id']> 0){
            //declare ID as $_GET['ID'] from url
        $id =(int) $_GET['id'] ;

        $query = "SELECT * FROM product WHERE ID = $id";
        $result = mysql_query($query,$con);

        if(isset($_SESSION['cart'][$id])){
            $_SESSION['cart'][$id]++;

            echo'<p>Another copy has been added to cart</p>';
        }
        else{
            $_SESSION['cart'][$id] = 1;

            echo'<p>This item has been added to your cart</p>';
        }
        }
    }
}
//if the action is update in url
elseif(isset($_POST['action']) && ($_POST['action'] =='update')){
    //

    foreach ($_POST['qty'] as $id => $v){
            $qty = (int)$v; 
             if ($qty > 0){
                $_SESSION['cart'][$id] =$qty; 


             }
             if($qty == 0){
                 unset($_SESSION['cart'][$id]);
             }
        }


    echo'<p>Your cart has been updated</p>';
}


?>
<?php


if($_SESSION['cart']) { //if the cart isn't empty
//show the cart
echo'To remove the item just change the quantity to 0 ';
$total = 0;
    //iterate through the cart, the $product_id is the key and $quantity is the value
    foreach($_SESSION['cart'] as $id => $v) { 

        //get the name, description and price from the database - this will depend on your database implementation.
        //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection

        $sql = sprintf("SELECT * FROM product WHERE id = %d;",
$id); 

        $result = mysql_query($sql);

        //Only display the row if there is a product (though there should always be as we have already checked)
        if(mysql_num_rows($result) > 0) {

            //list($Name, $Rent_price, $Category) = mysql_fetch_row($result);
            while($row = mysql_fetch_array($result)){

            $subtotal = $_SESSION['cart'][$row['ID']] * $row['Rent_price']; //work out the line cost
            $total +=$subtotal;
             //add to the total cost
echo'<form action="cart.php" method="post">
     <input type="hidden" name="action" value="update" />';
            echo '<td>' . $row['ID'] . '</td>';
            //show this information in table cells
            echo '<td>' . $row['Name']. '</td>';
            //along with a 'remove' link next to the quantity - which links to this page, but with an action of remove, and the id of the current product
            echo "<td><input type = \"text\" size=\"3\" name=\"qty[$id]\" value =\"{$_SESSION['cart'][$id]}\"> </td>";
            echo '<td>'.$subtotal.'</td>';

            echo "</tr>";
            }
        }

    }

    //show the total
    echo "<tr>";
    echo "<td colspan=\"3\" align=\"right\">Total</td>";
    echo "<td align=\"left\">$total</td>";
    echo "</tr>";
    echo "<tr>";
    echo "<td colspan=\"4\" align=\"right\"><input type=\"submit\" value=\"update\"></td>";
    echo'</form>';
    echo'</table>';
    echo'<br/>';

}else{
//otherwise tell the user they have no items in their cart
    echo "<p>You have no items in your shopping cart.</p>";


} 
function productExists($id) {
    //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
    $sql = sprintf("SELECT * FROM product WHERE id = %d;", $id); 

    return mysql_num_rows(mysql_query($sql)) > 0;
}



?>
    <br/>
    <br/>
    <div align="left">
<table>
<tr>
<th>
<FORM>
<INPUT TYPE="BUTTON" VALUE="Add Member" ONCLICK="window.location.href='./addmember.php'"> 
</FORM></th>
<th>
<form action="cart.php" method="POST" >
 <input type='text' name='searchmem' maxlength="50" value="Search IC" />
 <input name="submit" type="submit" value="Search" />

</form>
</th>
</tr>
</table>
</div>
    <br/>
    <table class="hovertable">
    <tr>
        <th>Member ID</th>
        <th>Name</th>
        <th>IC Number</th>
        <th>Contact</th>
        <th>Email</th>
    </tr>
<tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">

<form action="cart.php" method="post">
<input type="hidden" name="checkout" value="checkout"/>

<?php

//if never submit anything in search form

if(!isset($_POST['searchmem']) || (empty($_POST['searchmem']))){

    $result = mysql_query("SELECT * FROM members"); 


while($data = mysql_fetch_array($result)){

        $id = $data['ID'];
        $name = $data ['Name'];
        $ic = $data ['IC'];
        $contact = $data ['Contact'];
        $email = $data['Email'];


  echo "<td>$id</td><td>$name</td><td>$ic</td><td>$contact</td><td>$email</td><td><input type=\"submit\" name=\"submit\" value=\"checkout\"></td></tr>";
}

//if submit something in search form
}
if(isset($_POST['searchmem'])){

$sql = "SELECT * FROM members WHERE Name ='$_POST[searchmem]' OR IC='$_POST[searchmem]'";   
$result = mysql_query($sql,$con) or die (mysql_error());    

while($data = mysql_fetch_array($result)){

        $id = $data['ID'];
        $name = $data ['Name'];
        $ic = $data ['IC'];
        $contact = $data ['Contact'];
        $email = $data['Email'];


 echo "<td>$id</td><td>$name</td><td>$ic</td><td>$contact</td><td>$email</td><td><input type=\"submit\" name=\"submit\" value=\"checkout\"></td></tr>";
}
}   else{
echo'No result found';
}       
echo'</form>';

//if checkout submitted

?>
    </table>



    </div> <!-- /#contents -->
    </div> <!-- /#page -->
</body>
</html>
</body>
</html>
Member Avatar for LastMitch

I don't know how to insert the cart item and the selected member detail to the database. Anyone can show me where should I add the code and how to write the code?

If you got the code working it tells me that you already creating a database and it is working. The issue you are having is intergrating the Shopping Cart. Read this:

http://help.eventbrite.com/customer/portal/articles/426132-the-checkout-process---paypal

You have to write an insert query for the product, quantity, and the product type. For instance, I used this code and it worked;

// Insert the records into the cart_products a user has purchased
// Session is the session assigned to each user, and should be destroyed after a user logs out.
$insert = "INSERT INTO cart_products VALUES('','$item_id','$name','$user','$session','$quantity','$date_now')";

// Update the existing products by reducing them by the quantity ordered by the user
$update_quantity = "UPDATE items SET product_quantity='$qtty_after_shopping' WHERE item_id='$item_id'";

// Execute the insert and update queries
$execute_update = mysql_query($update_quantity) or die("Unable to update quantity".mysql_error());
$result = mysql_query($insert);

Hope that helps

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.