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);
...
Related
I have a problem, I have to take the domain name from the database, and create a knokpu with which I can sort the database by this domain, that is, if I clicked on the gmail button, then users with gmail should be sorted, if there are yahoo users then when you click on the yahoo button, the sort will also be performed.
The problem is that I don't know how to properly associate a button with sorting ... I will be very grateful for your help.
Here is my php code.
<!DOCTYPE html>
<html>
<head>
<title>Display all records from Database</title>
</head>
<body>
<h2>All emails</h2>
<table border="2">
<tr>
<td>Sr.No.</td>
<td>E-mail</td>
<td>Delete</td>
<td>CSV</td>
</tr>
<a href="?orderBy=email">
<button>Sort By E-mail</button>
</a>
<a href="?orderBy=date">
<button>Sort By Date</button>
</a>
<a href="?orderBy=">
<button>Export as CSV</button>
</a>
</br>
<?php
include "php/server.php"; // Using database connection file here
$orderBy = array('email', 'date'); // create array of sort options
$order='date'; // default sort option
if (isset($_GET['orderBy']) && in_array($_GET['orderBy'], $orderBy)) {
$order = $_GET['orderBy'];
} // change sort option depending on $order
$query = 'SELECT * FROM users ORDER BY '.$order;
$records = mysqli_query($db, $query); // fetch data from database
$dom=array();
include "php/search.php"; //include search engine
if(isset($_POST["submit"])){ //if button is pressed fecth data from search
$records = mysqli_query($db, $searchquery);
}else{ //if not fecth data from db
$records = mysqli_query($db, $query);
}
while($data = mysqli_fetch_array($records))
{
$parts = explode('#', $data['email']);
$domain = array_pop($parts);
?>
<tr>
<td><?php echo $data['email']; ?></td>
<td><?php echo $data['date']; ?></td>
<td>Delete</td>
<td><a><input type="checkbox" id="horns" name="horns"></td>
<td><?php echo $domain; ?></td>
</tr>
<tr>
<input type="submit" name="<?php $domain; array_push($dom,$domain)?>" value="<?php echo $domain; ?>">
</tr>
<?php
}
var_dump($dom);
?>
<h3>Emails is sorting by:<?php echo $order ?></h3>
</table>
</body>
</html>
Design:
I think what you mean is, you want to filter the data based on which the email provider button that clicked?
you can add the HTML like this:
...
<a href="?filterBy=gmail.com">
<button>gmail.com</button>
</a>
<a href="?filterBy=inbox.lv">
<button>inbox.lv</button>
</a>
<a href="?filterBy=yahoo.lv">
<button>yahoo.lv</button>
</a>
...
In PHP you need to update this line:
...
if (isset($_GET['orderBy']) && in_array($_GET['orderBy'], $orderBy)) {
$order = $_GET['orderBy'];
}
$filter = "";
if (isset($_GET['filterBy'])) {
$filter = "WHERE `email` LIKE ?";
}
$query = 'SELECT * FROM users '.$filter.' ORDER BY '.$order;
$prepare = mysqli_prepare($db, $query);
if (isset($_GET['filterBy'])) {
$param = "%".$_GET['filterBy'];
$prepare->bind_param('s', $param);
}
$prepare->execute();
// $records = mysqli_query($db, $query); // change this query to:
$records = $prepare->get_result();
...
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>
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}
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);
I am new to php as well as posting php code. SO if there is any information missing please let me know.
I am trying to have a list of products echo through a foreach loop. I am not sure whether the mistake is just syntax or something bigger than that.
Here is the error message:
Home
Product List
Code Name Version Release Date
( ! ) Warning: Invalid argument supplied for foreach() in /Applications/XAMPP/xamppfiles/htdocs/tech_support/product_manager/product_list.php on line 15
Call Stack
# Time Memory Function Location
1 0.0004 247496 {main}( ) ../index.php:0
2 0.0024 273008 include( '/Applications/XAMPP/xamppfiles/htdocs/tech_support/product_manager/product_list.php' ) ../index.php:20
Here's is product_list php.
<?php include '../view/header.php'; ?>
<h1>Product List</h1>
<!-- display a table of products -->
<h2><?php echo $products; ?></h2>
<tr>
<th>Code</th>
<th>Name</th>
<th>Version</th>
<th>Release Date</th>
<th> </th>
</tr>
<?php foreach ($products as $product) : ?> //<-----line 15
<tr>
<td><?php echo $product['product_id']; ?></td>
<td><?php echo $product['name']; ?></td>
<td class="right"><?php echo $product['version']; ?></td>
<td><form action="." method="post">
<input type="hidden" name="product_id"
value="<?php echo $product['releaseDate']; ?>">
<input type="submit" value="Delete">
</form></td>
</tr>
<?php endforeach; ?>
<p>Add Product</p>
<?php include '../view/footer.php'; ?>
Heres the code I have in my index folder.
<?php
require('../model/database.php');
require('../model/product_db.php');
$action = filter_input(INPUT_POST, 'action');
if ($action === NULL) {
$action = filter_input(INPUT_GET, 'action');
if ($action === NULL) {
$action = 'list_products';
}
}
if ($action == 'list_products') {
$product_id = filter_input(INPUT_GET, 'product_id',
FILTER_VALIDATE_INT);
if ($product_id == NULL || $product_id == FALSE) {
$product_id = 1;
}
$products = get_product($product_id);
include('product_list.php');
}
else if ($action == 'show_add_form') {
$product = get_product($product_id);
include('product_add.php');
.......
Just in case here is my database functions
<?php
function get_product($product_id) {
global $db;
$query = 'SELECT * FROM products
WHERE productCode = :product_id';
$statement = $db->prepare($query);
$statement->bindValue(":product_id", $product_id);
$statement->execute();
$products = $statement->fetch();
$statement->closeCursor();
return $products;
}
?>
If $products is an object you can try to typecast it before use it in loop like this: $array = (array) $yourObject;
as looking into:
PDO Fetch
it returns an array of the data, not an object as expected.
so, try to
var_dump( $products );
to take a look what it really is...an object or an array.
hope that helps.