PHP error on live website - php

I am having a difficult time displaying a database table on my website. I can't execute the queries that I have on my index.php file, but I can show the table on localhost with XAMPP. I can't even execute the MySQL statement codes that I have in the file. Please help!
This is when I have the statement codes not commented out:
This is when I do have the codes commented out:
I even have data in the tables that I am trying to connect to. Here is the code for index.php:
<?php
require_once('database.php');
//Get Category Id
$category_id = filter_input(INPUT_GET, 'category_id', FILTER_VALIDATE_INT);
if ($category_id == NULL || $category_id == false) {
$category_id = 1;
}
// Get name for selected category
$queryCategory = 'SELECT * FROM categories WHERE categoryID = :category_id';
$statement1 = $link->prepare($queryCategory);
$statement1->bindValue(':category_id', $category_id);
$statement1->execute();
$category = $statement1->fetch();
$category_name = $category['categoryName'];
$statement1->closeCursor();
//Get all categories
$queryAllCategories = 'SELECT * FROM categories ORDER BY categoryID';
$statement2 = $link->prepare($queryAllCategories);
$statement2->execute();
$categories = $statement2->fetchAll();
//Get products fpr selected category
$queryProducts = 'SELECT * FROM products WHERE categoryID = :category_id ORDER BY productID';
$statement3 = $link->prepare($queryProducts);
$statement3->bindValue(':category_id', $category_id);
$statement3->execute();
$products = $statement3->fetchAll();
$statement3->closeCursor();
?>
<!DOCTYPE html>
<HTML>
<head>
<title>My Guitar Shop</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<header><h1>Product Manager</h1></header>
<main>
<hr>
<h1>Product List</h1>
<aside>
<h2>Categories</h2>
<nav>
<ul>
<?php foreach ($categories as $category) : ?>
<li>
<a href=".?category_id=<?php echo $category['categoryID']; ?>">
<?php echo $category['categoryName']; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</nav>
</aside>
<section>
<!-- display a table of products -->
<h2><?php echo $category_name; ?></h2>
<table>
<tr>
<th>Code</th>
<th>Name</th>
<th class="right">Price</th>
<th> </th>
</tr>
<?php foreach ($products as $product) : ?>
<tr>
<td><?php echo $product['productCode']; ?></td>
<td><?php echo $product['productName']; ?></td>
<td><?php echo $product['listPrice']; ?></td>
<td><form action="delete_product.php" method="post">
<!-- Delete Product -->
<input type="hidden" name="product_id" value="<?php echo $product['productID']; ?>">
<input type="hidden" name="category_id" value="<?php echo $product['categoryID']; ?>">
<input type="submit" value="Delete">
</form>
</td>
<!-- Edit Product -->
<td><form action="edit_product_form.php" method="post">
<input type="hidden" name="product_id" value="<?php echo $product['productID']; ?>">
<input type="hidden" name="category_id" value="<?php echo $product['categoryID']; ?>">
<input type="submit" value="Edit">
</form>
</td>
</tr>
<?php endforeach; ?>
</table>
<p>Add Product</p>
<p>List Product</p>
</section>
</main>
<hr>
<footer><p>© <?php echo date("Y"); ?> My Guitar Shop Inc</p></footer>
</body>
</html>
Removed php of my username and password in database.php for security purposes:
<?php
$dsn = 'mysql:host=mysql.cit336.fullerview.net;dbname=cit336my_guitar_shop1';
try {
$db = new PDO($dsn, $username, $password);
} catch (PDOException $e) {
$error_message = $e->getMessage();
include('database_error.php');
exit();
}
?>
UPDATE:
A kind soul helped me with my first problem, but as I have edited the file, the problem is still within the statements. Its almost like the site wants me to remove the SQL statements but I don't want to remove them. They are vital to the site.
UPDATE 2:
I have edited the database.php file to get the PDO exceptions working. But now, as I am getting closer to my goal, I get an access denied error.
UPDATE 3:
I was able to access the database. Thank you all for your help, it is much appreciated. I just made a password typo and a database typo, again thanks for all of your help!

Since you are using the procedural function of mysqli to connect shouldn't ->prepare be mysqli_prepare?.
Also the bindValue is for PDO your code could be
$statement1 = mysqli_prepare($link, "SELECT * FROM categories WHERE categoryID =?");
mysqli_stmt_bind_param($statement1, "s", $category_id);
mysqli_stmt_execute($statement1);
mysqli_stmt_bind_result($statement1, $category);
mysqli_stmt_fetch($statement1);

Related

hidden input value do not display in foreach loop in php

Now this display in this way
I'm developing a simple shopping cart, using session variable called ($_SESSION['shopping_cart']. and I use 2 hidden field for price, and item name. I try to display the summary of cart details , but price and item name do not display in the table. But quantity is displayed.I tried a lot to solve this, but it does not display the item name, and price properly.
Please if some one can help me to solve this problem I highly appreciate your guidance, Thanks in advance.
<?php
//database connect
$connect = mysqli_connect('localhost', 'root','', 'carta');
//if session variable is set
if (isset($_POST['add_to_cart'])) {
if (isset($_SESSION['shopping_cart']))
{
$item_id_array = array_column($_SESSION['shopping_cart'], 'item_id');
if (!in_array($_GET['id'], $item_id_array)) {
// counter to track the number of products in cart
$count = count($_SESSION['shopping_cart']);
//add new items to cart
$item_array = array(
"item_id" => $_GET['id'],
"item_name" => $_POST['hidden_name'],
"item_price" => $_POST['hidden_price'],
"item_quantity" => $_POST['quantity']
);
$_SESSION['shopping_cart'][$count] = $item_array;
}else{
echo "<script>alert('Item Already Added...');</script>";
}
}else{
//if shopping cart session variable doesn't exit, create array & store item details
$item_array = array(
"item_id" => $_GET['id'],
"item_name" => $_POST['hidden_name'],
"item_price" => $_POST['hidden_price'],
"item_quantity" => $_POST['quantity']
);
// store item details into session variable
$_SESSION['shopping_cart'][0] = $item_array;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Shopping Cart</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<!-- custom css -->
<link rel="stylesheet" type="text/css" href="cartcustom.css">
</head>
<body>
<div class="container">
<h3 class="text-center">Shopping Cart</h3>
<?php
//get data from database
$query = 'SELECT * FROM products ORDER BY id ASC';
$result = mysqli_query($connect, $query);
if ($result){
if (mysqli_num_rows($result)>0){
while ($row = mysqli_fetch_assoc($result)){
// print_r($row);
?>
//form
<div class="col-sm-2 col-md-3">
<form method="post" action="index.php?action=add&id=<?php echo $row['id']; ?>">
<img src="<?php echo $row['image']; ?>" class="img-responsive">
<h6 class="text-info"><?php echo $row['name']; ?></h6>
<h6 class="text-danger"><?php echo "Rs:" . $row['price'] . ".00" ?></h6>
<input type="text" name="quantity" class="form-control" value="1">
<input type="hidden" name="hidden_name" value="<?php echo $row['name']; ?>"/>
<input type="hidden" name="hidden_price" value="<?php echo $row['price']; ?>"/>
<input type="submit" name="add_to_cart" class="btn btn-success" value="Add to Cart">
</form>
</div>
<?php
}
}
}
?>
</div>
//table of displaying final cart details
<div class="container">
<h4 class="text-center">Order Details</h5>
<div class="table_responsive">
<table class="table table-bordered">
<tr>
<th>Item</th>
<th>Quantity</th>
<th>Price</th>
<th>Total</th>
<th>Action</th>
</tr>
<?php
if (!empty($_SESSION['shopping_cart'])) {
$total = 0;
foreach ($_SESSION['shopping_cart'] as $keys => $values) {
?>
//display the cart details to customer using a table
<tr>
<td><?php echo $values["item_name"]; ?></td> // this does not display
<td><?php echo $values["item_quantity"]; ?></td>
<td>$ <?php echo $values["item_price"]; ?></td> //this does not display
<td> <?php echo number_format($values['item_quantity'] * $values['item_price'], 2 ) ?></td>
</tr>
<?php
}
}
?>
</table>
</div>
</div>
</body>
</html>

PHP Displaying data from database into a table when a link is clicked

Just to start off, I am new to php so i might have missed something obvious so please bear with me.
I have hyperlinks (planes, ships trains etc) and when I click the hyperlink "planes" i want all of the planes records to be displayed in a table. When I click a different vehicle i want it to refresh the table with new data.
The problem is, when i click the link "trains" it does not refresh the table and display relevant data, it keeps the same data. How do i tell php when i click the link "planes" i want to display all the records with that productLine.
Thanks for any help
As you can see in the pic, i clicked train but it still displays vintage cars
Here is my code:
<?php
require_once('dbconfig.php');
//get productLine
if (!isset($productLine)) {
$productLine = filter_input(INPUT_GET, 'productLine', FILTER_VALIDATE_INT);
if ($productLine == null || $productLine == FALSE) {
$productLine = 'Trains';
}
}
//get all product lines
$query = 'SELECT * FROM productlines';
$statement = $db->prepare($query);
$statement->execute();
$productLines = $statement->fetchAll();
$statement->closeCursor();
//Get products for product line
$queryProducts = 'SELECT * FROM products WHERE productLine = :productLine ORDER BY productCode';
$statement1 = $db->prepare($queryProducts);
$statement1->bindValue(':productLine', $productLine);
$statement1->execute();
$products = $statement1->fetchAll();
$statement1->closeCursor();
?>
<!DOCTYPE html>
<html>
<head>
<title>Classic Models Online</title>
<link rel="stylesheet" type="text/css" href="main.css" />
</head>
<body>
<header><h1>ClassicModels Online</h1>
<p>Classic models for all automobile enthusiasts</p>
</header>
<main>
<h1>Classic Models Product List</h1>
<aside>
<!--Display list of product lines-->
<h2>Product Lines</h2>
<nav>
<ul>
<?php foreach ($productLines as $productLine) : ?>
<li>
<a href=".?productLine=<?php echo $productLine['productLine']; ?>">
<?php echo $productLine['productLine']; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</nav>
</aside>
<br>
<section>
<!--Display a table of products for product line-->
<h2><?php echo $productLine['productLine']; ?></h2>
<table>
<tr>
<th>Code</th>
<th>Name</th>
<th>Scale</th>
<th>Price</th>
<th>Total Sold</th>
<th> </th>
</tr>
<?php foreach ($products as $product) :?>
<tr>
<td> <?php echo $product['productCode']; ?> </td>
<td> <?php echo $product['productName']; ?> </td>
<td> <?php echo $product['productScale']; ?> </td>
<td> <?php echo $product['MSRP']; ?> </td>
<td> <?php echo $product['quantityInStock']; ?> </td>
<td> <form action="update_product.php" method="post">
<input type="hidden" name="productCode" value="<?php echo $product['productCode']; ?>">
<input type ="hidden" name="productLine" value="<?php echo $product['productLine']; ?>">
<input type="submit" value="Update">
</form> </td>
</tr>
<?php endforeach; ?>
</table>
<p>Add Product</p>
</section>
</main>
<footer>
<p>© <?php echo date("Y"); ?> Classic Models Online.</p>
</footer>
</body>
</html>
You need to get the productLine variable from the query string you have in the url. You need to add something like
if(isset($_GET['productLine'])){
$productLine = $_GET['productLine'];
}
Just do if(isset($_GET["productLine"])){//do your stuff}

Unable to delete product from table using PHP

I am in a pickle here. Whenever I try to press the delete button on my project, it goes straight to the PHP file and does nothing to the database that I have set up for it.
Here are some visuals to help you:
Visual number 2
Here is the code for index.php:
<?php
require_once('database.php');
//Get Category Id
$category_id= filter_input(INPUT_GET, 'category_id', FILTER_VALIDATE_INT);
if ($category_id == NULL || $category_id == false){
$category_id = 1;
}
// Get name for selected category
$queryCategory = 'SELECT * FROM categories
WHERE categoryID = :category_id';
$statement1 = $db->prepare($queryCategory);
$statement1->bindValue(':category_id', $category_id);
$statement1->execute();
$category = $statement1->fetch();
$category_name = $category['categoryName'];
$statement1->closeCursor();
//Get all categories
$queryAllCategories = 'SELECT * FROM categories
ORDER BY categoryID';
$statement2 = $db -> prepare($queryAllCategories);
$statement2->execute();
$categories = $statement2->fetchAll();
//Get products fpr selected category
$queryProducts = 'SELECT * FROM products
WHERE categoryID = :category_id
ORDER BY productID';
$statement3 = $db -> prepare($queryProducts);
$statement3 -> bindValue(':category_id', $category_id);
$statement3 -> execute();
$products = $statement3 -> fetchAll();
$statement3 ->closeCursor();
?>
<!DOCTYPE html>
<HTML>
<head>
<title>My Guitar Shop</title>
<link rel="stylesheet" type="text/css" href="../main1.css"/>
</head>
<body>
<header><h1>Product Manager</h1></header>
<main>
<hr>
<h1>Product List</h1>
<aside>
<h2>Categories</h2>
<nav>
<ul>
<?php foreach ($categories as $category) : ?>
<li>
<a href=".?category_id=<?php echo $category['categoryID']; ?>">
<?php echo $category['categoryName'];?>
</a>
</li>
<?php endforeach; ?>
</ul>
</nav>
</aside>
<section>
<!-- display a table of products -->
<h2><?php echo $category_name; ?></h2>
<table>
<tr>
<th>Code</th>
<th>Name</th>
<th class="right">Price</th>
<th> </th>
</tr>
<?php foreach ($products as $product) : ?>
<tr>
<td><?php echo $product['productCode']; ?></td>
<td><?php echo $product['productName']; ?></td>
<td><?php echo $product['listPrice']; ?></td>
<td><form action="delete_product.php" method="post">
<input type="hidden" name="product_id" value="<?php echo $product['productID']; ?>">
<input type="hidden" name="category_id" value="<?php echo $product['categoryID']; ?>">
<input type="submit" value="Delete">
</form></td>
</tr>
<?php endforeach; ?>
</table>
<p>Add Product</p>
</section>
</main>
<hr>
<footer><p>$copy; <?php echo date("Y"); ?> My Guitar Shop Inc</p></footer>
</body>
</html>
code for delete_product.php:
<?php
require_once('database.php');
$product_id= filter_input(INPUT_GET, 'product_id', FILTER_VALIDATE_INT);
$category_id= filter_input(INPUT_GET, 'category_id', FILTER_VALIDATE_INT);
//Delete the product from the datavase
if ($product_id != false && $category_id != false){
$query = 'DELETE FROM products
WHERE productID = :product_id';
$statement = $db -> prepare($query);
$statement -> bindValue(':product_id', $product_id);
$success = $statement->execute();
$statement -> closeCursor();
}
//Display the Product List Page
include('index.php');
?>
Thank you for helping me out! I really appreciate it!
Your are submitting delete button in post method. So you have to receive these value in post method in delete page.
Just change INPUT_GET to INPUT_POST in two lines in delete_product.php
$product_id= filter_input(INPUT_POST, 'product_id', FILTER_VALIDATE_INT);
$category_id= filter_input(INPUT_POST, 'category_id', FILTER_VALIDATE_INT);
instead of
$product_id= filter_input(INPUT_GET, 'product_id', FILTER_VALIDATE_INT);
$category_id= filter_input(INPUT_GET, 'category_id', FILTER_VALIDATE_INT);
You should try with this :
...
$product_id= filter_input(INPUT_POST, 'product_id', FILTER_VALIDATE_INT);
$category_id= filter_input(INPUT_POST, 'category_id', FILTER_VALIDATE_INT);
...

Why does my form get replaced with echo "0"?

I got an error on my page that I can't identify, and when I view the site online it replaces all my site content with the value "0".
How come I can't see the forms, and is it communicating with the SQL server properly?
PS: Login works and session is created, so the connect.php does work properly.
Here is the code for members.php:
<?php
// starting session
session_start();
// check if user is logged in
if (!isset($_SESSION['username']))
{
header('Location: http://wwww.gjertgjersund.com/');
exit();
}
else
{
// database connection
require ('connect.php');
//post record count
$post_count = mysql_query("SELECT * FROM posts");
$post_count_result = mysql_num_rows($post_count);
//comment count
$comment_count = mysql_query("SELECT * FROM comments");
$comment_count_result = mysql_num_rows($comment_count);
if(isset($_POST['submit']))
{
$newcategory = $_POST['newcategory'];
if(!empty($newcategory))
{
$query = mysql_query("INSERT * INTO categories (category) VALUES ('$newcategory')";
if($query)
{
echo 'New category added';
}
}
else
{
echo 'Error';
}
}
else
{
echo 'Missing newcategory';
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<title> Folder </title>
<body>
<div class="wrap">
<div id="menu">
<ul>
<li><a href='#'>Home</a></li>
<li><a href='#'>Create New Post</a></li>
<li><a href='#'>Delete Post</a></li>
<li><a href='logout.php'>Log Out</a></li>
<li><a href='#'>Blog Home Page</a></li>
</ul>
</div>
<div id="maincontent">
<table>
<tr>
<td>Total Blog Post</td>
<td><?php echo $post_count_result ?></td>
</tr>
<tr>
<td>Total Comments</td>
<td><?php echo $comment_count_result ?></td>
</tr>
</table>
<div id="categoryform">
<form action="members.php" method="post">
<label for="category">Add New Category</label>
<input type="text" name="newcategory"/>
<input type="submit" name="submit" value="Create"/>
</form>
</div>
</div>
</div>
</body>
</html>
$query = mysql_query("INSERT * INTO categories (category) VALUES ('$newcategory')";
should be
$query = mysql_query("INSERT INTO categories (category) VALUES ('$newcategory')";

Special configuration on xampp vs xampplite?

I've tried to run a php file on xampp which is installed in Ubuntu and WinXP. Both result in error, but it is success when tried to run on xampplite in WinXP. Any special configuration is need to be set on the full xampp version? The php code is as the following:
<?php
require_once('database.php');
// Get category ID
if(!isset($category_id)) {
$category_id = $_GET['category_id'];
if (!isset($category_id)) {
$category_id = 1;
}
}
// Get name for current category
$query = "SELECT * FROM categories
WHERE categoryID = $category_id";
$category = $db->query($query);
$category = $category->fetch();
$category_name = $category['categoryName'];
// Get all categories
$query = 'SELECT * FROM categories
ORDER BY categoryID';
$categories = $db->query($query);
// Get products for selected category
$query = "SELECT * FROM products
WHERE categoryID = $category_id
ORDER BY productID";
$products = $db->query($query);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<!-- the head section -->
<head>
<title>My Guitar Shop</title>
<link rel="stylesheet" type="text/css" href="main.css" />
</head>
<!-- the body section -->
<body>
<div id="page">
<div id="header">
<h1>Product Manager</h1>
</div>
<div id="main">
<h1>Product List</h1>
<div id="sidebar">
<!-- display a list of categories -->
<h2>Categories</h2>
<ul class="nav">
<?php foreach ($categories as $category) : ?>
<li>
<a href="?category_id=<?php echo $category['categoryID']; ?>">
<?php echo $category['categoryName']; ?>
</a>
</li>
<?php endforeach; ?>
</ul>
</div>
<div id="content">
<!-- display a table of products -->
<h2><?php echo $category_name; ?></h2>
<table>
<tr>
<th>Code</th>
<th>Name</th>
<th class="right">Price</th>
<th> </th>
</tr>
<?php foreach ($products as $product) : ?>
<tr>
<td><?php echo $product['productCode']; ?></td>
<td><?php echo $product['productName']; ?></td>
<td class="right"><?php echo $product['listPrice']; ?></td>
<td><form action="delete_product.php" method="post"
id="delete_product_form">
<input type="hidden" name="product_id"
value="<?php echo $product['productID']; ?>" />
<input type="hidden" name="category_id"
value="<?php echo $product['categoryID']; ?>" />
<input type="submit" value="Delete" />
</form></td>
</tr>
<?php endforeach; ?>
</table>
<p>Add Product</p>
</div>
</div>
<div id="footer">
<p>© <?php echo date("Y"); ?> My Guitar Shop, Inc.</p>
</div>
</div><!-- end page -->
</body>
</html>
The database has been created without any problem. Thanks

Categories