Advertisement
Guest User

Untitled

a guest
Mar 13th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use warnings;
  4. use strict;
  5.  
  6. use CGI;
  7. use CGI::Carp qw(fatalsToBrowser);
  8. use DBI;
  9.  
  10. my $query = CGI->new();
  11.  
  12. my $database = 'my_store';
  13. my $db_server = 'localhost';
  14. my $user = 'root';
  15. my $password = 1;
  16.  
  17. my ($session_id, $page_title, $dbh, $sth);
  18. print_page_start();
  19. if ($query->cookie('session_id')) {
  20. $session_id = ($query->cookie('session_id'));
  21. }
  22. my $val = $query->param('action');
  23. print "|<p>$val</p>|\n";
  24. if ($query->param('action')) {
  25. if ($query->param('action') eq 'remove') {
  26. $page_title = "Shopping Cart";
  27. db_connect();
  28. remove_item();
  29. print_page_start();
  30. if ($sth->rows) {
  31. print_cart();
  32. }
  33. else {
  34. print_no_cart();
  35. }
  36. print_page_end();
  37. db_cleanup();
  38. }
  39. } else {
  40. $page_title = "Shopping Cart";
  41. print_page_start();
  42. db_connect();
  43. get_cart_contents();
  44. my $val2 = $sth->rows;
  45. print "|<p>$val2</p>|\n";
  46. if ($sth->rows) {
  47. print_cart();
  48. }
  49. else {
  50. print_no_cart();
  51. }
  52. print_page_end();
  53. db_cleanup();
  54. }
  55.  
  56. sub print_page_start {
  57. print $query->header;
  58. print "<html>\n<head>\n<title>$page_title</title>\n";
  59. print "</head>\n<body>\n";
  60. print "<h1 aligh=\"center\">$page_title</h1>\n";
  61. }
  62.  
  63. sub db_connect {
  64. $dbh = DBI->connect("DBI:mysql:$database:$db_server", $user,$password);
  65. }
  66.  
  67. sub get_cart_contents {
  68. my $sql_select = qq[SELECT cart.product_id AS product_id,
  69. quantity, product_name, product_price
  70. FROM cart, products
  71. where session_id = '$session_id'
  72. AND cart.product_id = products.id];
  73. $sth = $dbh->prepare($sql_select) or die "Couldn’t prepare the query:", $sth->errstr, "\n";
  74. my $rv = $sth->execute or die "Couldn’t prepare the query:", $sth->errstr, "\n";
  75. }
  76.  
  77. sub print_cart {
  78. print "<div align=\"center\">\n<table>\n<tr>\n";
  79. print "<th>Product ID</th>\n<th>Product Name</th>\n";
  80. print "<th>Price</th>\n<th>Quantity</th>\n</tr>\n";
  81. while (my @row = $sth->fetchrow_array) {
  82. my ($rec_product_id, $rec_quantity, $rec_product_name,
  83. $rec_price) = @row;
  84. print "<td>$rec_product_id</td>\n";
  85. print "<td>$rec_product_name</td>\n";
  86. print "<td>$rec_price</td>\n";
  87. print "<td>$rec_quantity</td>\n";
  88. print "<td>\n<form>\n";
  89. print "<input type=\"hidden\" name=\"action\" ";
  90. print "value=\"remove\" />\n";
  91. print "<input type=\"hidden\" name=\"product_id\" ";
  92. print "value=\"$rec_product_id\" />\n";
  93. print "<input type=\"submit\" value=\"remove\" />\n";
  94. print "</td>\n";
  95. print "</tr>\n";
  96. }
  97. print "</table>\n";
  98. }
  99.  
  100. sub print_no_cart {
  101. print "<p>There are no items in your shopping cart.</p>\n";
  102. }
  103.  
  104. sub print_page_end {
  105. print "<center>";
  106. print "<p><b><a href=\"new_catalog.cgi\">return to catalog</a></b></p>\n";
  107. print "<p><b><a href=\"checkout.pl\">check out</a></b></p>\n";
  108. print "</center>";
  109. print "</body>\n</html>\n";
  110. }
  111.  
  112. sub db_cleanup {
  113. my $rc = $dbh->disconnect;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement