Manipulating individual products in MySQL table - php

I am working on a small orders cart and currently I have implemented MySQL to display products from a database. I want these products to have their own images and to be organised into 3 separate tables for starters, mains and desserts.
Currently I have one table for all items and I am unsure how to add an image next to a selected product.
Shown here:
Here is my code:
<?php
session_start();
$page = 'index.php';
mysql_connect('localhost','root','') or die(mysql_error());
mysql_select_db('cart') or die(mysql_error());
if (isset($_GET['add'])) {
$quantity = mysql_query('SELECT id, quantity FROM products WHERE id='.mysql_real_escape_string((int)$_GET['add']));
while ($quantity_row = mysql_fetch_assoc($quantity)) {
if ($quantity_row['quantity']!=$_SESSION['cart_'.(int)$_GET['add']]) {
$_SESSION['cart_' . (int)$_GET['add']] +='1';
header('Location: ' . $page);
}
}
header('Location: ' . $page);
}
if (isset($_GET['remove'])) {
$_SESSION['cart_'.(int)$_GET['remove']]--;
header ('Location: ' . $page);
}
if (isset($_GET['delete'])) {
$_SESSION['cart_' . (int)$_GET['delete']]='0';
header ('Location: ' . $page);
}
function products() {
$get = mysql_query('SELECT id, name, description, price FROM products WHERE quantity > 0 ORDER BY id ASC');
if (mysql_num_rows($get) == 0) {
echo "There are no products to display.";
}
else {
echo "<center>\n";
echo " <table border=2 width=40% cellspacing=5 cellpadding=10 bgcolor=cyan cols=2>\n";
echo " <tr>\n";
echo " <th>View</th>\n";
echo " <th>Dish</th>\n";
echo " <th>Description</th>\n";
echo " <th>Item Price</th>\n";
echo " </tr>\n";
while ($get_row = mysql_fetch_assoc($get)) {
?>
<tr>
<td> <a href="ice.png"</a> </td>
<td> <?echo '<p>'.$get_row['name']?> </td>
<td> <?echo $get_row['description']?> </td>
<td> <?echo '&pound'.number_format($get_row['price'],2).'<br><br> Add</p>';?> </td>
</tr>
<?
}
echo "</table>\n";
echo "</center>\n";
}
}

You can add a field to a table with a type of product.
So you will be able to perform 3 separate queries with the condition.
For Example
SELECT
id,
name,
description,
price
FROM
products
WHERE
quantity > 0
AND type LIKE 'desert'
ORDER BY id ASC
and so on

Related

Only the result result from php query displays in the html table, the rest display outside

I'm building a forum for learning purposes. I'm trying without success to retrieve forum categories from the database and displaying them in a table, but only the first category displays in the table, the rest display outside the table. I will post my code, and a screenshot of the image.
<?php
include 'connect.php';
include 'header.php';
$sql = "SELECT categories.cat_id,categories.cat_name,
categories.cat_description, COUNT(topics.topic_id) AS topics
FROM categories
LEFT JOIN topics ON topics.topic_id = categories.cat_id
GROUP BY categories.cat_name, categories.cat_description,
categories.cat_id";
$result = mysql_query($sql);
if(!$result) {
echo 'The categories could not be displayed, please try again later.';
} else {
if(mysql_num_rows($result) == 0) {
echo 'No categories defined yet.';
} else {
//prepare the table
echo '
<div class="container">
<table class="table forum tale-striped table-hover">
<thead>
<tr>
<th class="cell-stat"></th>
<th><h3>Category</h3></th>
<th><h3>Last topic</h3></th>
</tr>
</thead>';
while($row = mysql_fetch_assoc($result)) {
echo '<tbody>';
echo '<tr >';
echo '<td class="text-center"><i class="fa fa-exclamation fa-2x text-danger"> </i></td>';
echo '<td><h3>' . $row['cat_name'] . '</h3>' . $row['cat_description'].'</td>';
echo '<td class="float-xs-right">';
//fetch last topic for each cat
$topicsql = "SELECT topic_id, topic_subject, topic_date, topic_cat
FROM topics
WHERE topic_cat = " . $row['cat_id'] . "
ORDER BY topic_date DESC
LIMIT 1";
$topicsresult = mysql_query($topicsql);
if(!$topicsresult) {
echo 'Last topic could not be displayed.';
} else {
if(mysql_num_rows($topicsresult) == 0) {
echo 'no topics';
} else {
while($topicrow = mysql_fetch_assoc($topicsresult))
echo '' . $topicrow['topic_subject'] . ' at ' . date('d-m-Y', strtotime($topicrow['topic_date']));
}
}
echo '</td>';
echo '</tr>';
echo '</tbody>';
echo '</table>';
echo '</div>'; //container
}
}
}
include 'footer.php';
?>
https://www.dropbox.com/s/c1dgijuafgv9jzu/Capture.PNG?dl=0
Quite simply you have the closing </table> tag inside the while loop so once the first row is output you close the table. Just move it outside the while loop like this, also the opening <tbody> need moving above the while loop as well
<?php
include 'connect.php';
include 'header.php';
$sql = "SELECT categories.cat_id,categories.cat_name,
categories.cat_description, COUNT(topics.topic_id) AS topics
FROM categories
LEFT JOIN topics ON topics.topic_id = categories.cat_id
GROUP BY categories.cat_name, categories.cat_description,
categories.cat_id";
$result = mysql_query($sql);
if(!$result) {
echo 'The categories could not be displayed, please try again later.';
} else {
if(mysql_num_rows($result) == 0) {
echo 'No categories defined yet.';
} else {
//prepare the table
echo '
<div class="container">
<table class="table forum tale-striped table-hover">
<thead>
<tr>
<th class="cell-stat"></th>
<th><h3>Category</h3></th>
<th><h3>Last topic</h3></th>
</tr>
</thead>
<tbody>'; //<-- moved
while($row = mysql_fetch_assoc($result)) {
echo '<tr >';
echo '<td class="text-center"><i class="fa fa-exclamation fa-2x text-danger"> </i></td>';
echo '<td><h3>' . $row['cat_name'] . '</h3>' . $row['cat_description'].'</td>';
echo '<td class="float-xs-right">';
//fetch last topic for each cat
$topicsql = "SELECT topic_id, topic_subject, topic_date, topic_cat
FROM topics
WHERE topic_cat = " . $row['cat_id'] . "
ORDER BY topic_date DESC
LIMIT 1";
$topicsresult = mysql_query($topicsql);
if(!$topicsresult) {
echo 'Last topic could not be displayed.';
} else {
if(mysql_num_rows($topicsresult) == 0) {
echo 'no topics';
} else {
while($topicrow = mysql_fetch_assoc($topicsresult))
echo '' . $topicrow['topic_subject'] . ' at ' . date('d-m-Y', strtotime($topicrow['topic_date']));
}
}
echo '</td>';
echo '</tr>';
}
}
echo '</tbody>'; //<-- moved
echo '</table>'; //<-- moved
echo '</div>'; //container //<-- moved
}
include 'footer.php';
?>
Becuse you're closing the <table> tag within the while-loop. Also, the <tbody> should be outside the loop as well, since there should only be one <tbody> in a table like this.

Sorting a table that pulls from a database in a custom wordpress plugin

I am building a Wordpress plugin and trying to sort my database tables by last name, first name, address etc when someone clicks on the title of the column.
I am unsure why but when I click a column title link, I get the following message:
"Sorry, you are not allowed to access this page." My filename is lead_db.php.
I have also tried to enter /admin.php?page=lead-db-editor-handler which is what the url reads on the page when I open it in the backend; this brings me to a 404 when I click to sort. I have been at this for hours now and can not figure it out or find the answer online. Does anyone know where I am going wrong? Thanks in advance!
<table border="1">
<tr>
<th data-sort-initial='descending'><a>ID</a></th>
<th>Registered Date</th>
<th>Last Name,
First Name</th>
<th>Address 1</th>
</tr>
<?php
$sql = "SELECT * FROM wp_client_info";
if ($_REQUEST['sort'] == 'id')
{
$sql .= " ORDER BY id";
}
elseif ($_REQUEST['sort'] == 'time')
{
$sql .= " ORDER BY time";
}
elseif ($_REQUEST['sort'] == 'last_name')
{
$sql .= " ORDER BY last_name";
}
elseif($_REQUEST['sort'] == 'first_name')
{
$sql .= " ORDER BY first_name";
}
?>
<?php
$result = mysql_query($sql) or die (mysql_error())
while($row = mysql_fetch_array($result)){
?>
<tr>
<td><?php echo $row['id'] ?></td>
<td><?php echo $row['time'] ?></td>
<td><?php echo $row['last_name'], ", ",$row['first_name'] ?></td>
<td><?php echo $row['address_1'] ?></td>
</tr>
<?php
}
mysql_close();
?>
</table>

Fetching data from the Database, not working correctly

I'm wondering if someone can help me, because I'm stuck:) the script does not work correcly:/
the records in "Naam" are fetched, looped and the table is displayed:
But the data from "product" doesn't show. So my point is: Can someone help me get the products data from the DB, thanks in advance
<?php
require_once('Includes/connect.php')
?>
<?php
if(isset($_GET['action']) && $_GET['action']=="add"){
$id=intval($_GET['id']);
if(isset($_SESSION['cart'][$id])){
$_SESSION['cart'][$id]['quantity']++;
}else{
$sql_s="SELECT * FROM product
WHERE id_product={$id}";
$query_s=mysql_query($sql_s);
if(mysql_num_rows($query_s)!=0){
$row_s=mysql_fetch_array($query_s);
$_SESSION['cart'][$row['id_product']]=array(
"quantity" => 1,
"price" => $row['price']
);
}
else
{
$message="Het product ID is ongeldig!";
}
}
}
echo "<h1>Producten lijst Kerstbomen</h1>";
if(isset($message))
{
echo "<h2>$message</h2>";
}
else
{
$table_thead = '
<table>
<tr>
<th><ID/th>
<th>Id produkt</th>
<th>size</th>
<th>diameter</th>
<th>tips</th>
<th>nr_of_bulb</th>
<th>cu_ft</th>
<th>l</th>
<th>w</th>
<th>h</th>
<th>cbm</th>
<th>g_w</th>
<th>n_w</th>
<th>stand</th>
<th>pack</th>
<th>warehouse</th>
<th>Price</th>
<th>barcode</th>
<th>Quantity</th>
</tr>
';
$sql_getName = "
SELECT *
FROM naam
ORDER BY name ASC
";
$query_getName = mysql_query($sql_getName);
while( $row_name = mysql_fetch_assoc($query_getName) )
{
$curName = $row_name['name'];
$sql_getProduct = "
SELECT *
FROM product
WHERE id_name = '$name';
";
$query_getProduct = mysql_query($sql_getProduct);
echo ($row['id_name']);
print $table_thead;
while( $row_Product = mysql_fetch_assoc($query_getProduct) )
{
echo "
<tr>
<td> ".$row['id_product']."</td>
<td>".$row['size']."</td>
<td>".$row['diameter']."</td>
<td>".$row['tips']."</td>
<td>".$row['nr_of_bulb']."</td>
<td>".$row['cu_ft']."</td>
<td>".$row['l']."</td>
<td>".$row['w']."</td>
<td>".$row['h']."</td>
<td>".$row['cbm']."</td>
<td>".$row['g_w']."</td>
<td>".$row['n_w']."</td>
<td>".$row['stand']."</td>
<td>".$row['pack']."</td>
<td>".$row['warehouse']."</td>
<td>".$row['price']."</td>
<td>".$row['barcode']."</td>
<td>".$row['id_name']."</td>
<td> <b></b> <input class='quantity' type= 'text' name='aantal' size='2' maxlength='2' value='1'/></td>
<td><a href='index.php?page=product&action=add&id=".$row['id_product']."'>Product toevoegen</a></td>
</tr>";
}
echo "</table>";
}
}
?>

not display from database

I'm trying to display list from database but when run that code it said "You have no product listed in your data yet" but actually have list from my data ...
<?php
$con = new mysqli("localhost", "root", "3250", "shopone");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// This block grabs the whole list of product for viewing
$product_list = "";
$sql = "SELECT * FROM product ORDER BY product_id DESC";
$result=mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)){
$product_id = $row["product_id"];
$product_name = $row["product_name"];
$product_category = $row["product_category"];
$product_retail_price = $row["product_retail_price"];
$product_price = $row["product_price"];
$product_detail = $row["product_detail"];
$product_image = $row["screenshot"];
$product_thumbnail = $row["product_thumbnail"];
$product_discount = $row["product_discount"];
$screenshot = $row["screenshot"];
$product_list .= '<table width="80%" border="1">
<tr>
<td width="172" valign="top"> echo <img src="' . GW_UPLOADPATH . $screenshot .'" width="111" height="149" alt="<?php echo $product_name; ?>" /><br />
View Full Size Image</td>
<td width="85" class="product-text">' . $product_id . '</td>
<td width="402" class="product-text">' . $product_name . '</td>
<td width="108" align="center" class="product-text">' . $product_price . '</td>
<td width="34" align="center" class="product-text"><a rel="leanModal" href="edit_product.php?pid=' . $product_id . '">Edit</a></td>
<td width="56" align="center" class="product-text"><a rel="leanModal" href="product.php?deleteid=' . $product_id . '">Delete</a></td>
<td width="56" align="center" class="product-text">View</td>
</tr>
</table> ';
}
$product_list = "You have no product listed in your data yet";
?>
and then the result i get is showing nothing it said "you have no product list in your data" , how can i solve that!
Change the last assignment to:
if (empty($product_list)) {
$product_list = "You have no product listed in your data yet";
}
You were replacing all the results retrieved from the database with that error message.
All the places where you do <?php echo ... ?> in the $product_list assignments should be concatenation -- you can only use that in inline HTML, not in strings.
You probably don't want to start a new <table> for each row from the database. Usually there's just one HTML table, and each database row corresponds to one HTML row within it.
Maybe the privilege problem. But first comment the last line and have a try.
You are overwriting $product_list in the last statement, that´s why you get the value you last gave to the variable.
Also, as a tip, in the while loop you can save the row for each record, as you already have it in the array.
Last, you can open the table before the while, and close it after.
<?php
$con = new mysqli("localhost", "root", "3250", "shopone");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// This block grabs the whole list of product for viewing
$sql = "SELECT * FROM product ORDER BY product_id DESC";
$result = mysqli_query($con,$sql);
//you can add this check to add 'empty table' message if no result
if(mysql_num_rows()>0)
{
$product_list = '<table>';
while($row = mysqli_fetch_array($result))
{
$product_list.= '<tr>'.
'<td>'.$row["product_id"].'</td>'.
// ....
// do the same with all rows
// ....
'<td>'.$row["screenshot"].'</td>'.
'</tr>';
}
$product_list.= '</table>';
}
else
{
$product_list = "You have no product listed in your data yet";
}
//print
echo $product_list;
?>

SESSION Cart adding 2 + or -2 problem

So i edited my own shop but im having some issues with it, for example it add 2 instead of 1 or it removes 2 instead of 1,
you can see how it looks on www.neobotmx.org/test/tienda.php <<< not opwn for the public yet >> thats why its on a test folder
The shop code :
<?php
$product_id = $_GET[id]; //the product id from the URL
$action = $_GET[action]; //the action from the URL
//if there is an product_id and that product_id doesn't exist display an error message
if($product_id && !productExists($product_id)) {
die("Error. Product Doesn't Exist");
}
switch($action) { //decide what to do
case "add":
$_SESSION['cart'][$product_id]++; //add one to the quantity of the product with id $product_id
break;
case "remove":
$_SESSION['cart'][$product_id]--; //remove one from the quantity of the product with id $product_id
if($_SESSION['cart'][$product_id] == 0) unset($_SESSION['cart'][$product_id]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise is will show zero, then -1, -2 etc when the user keeps removing items.
break;
case "empty":
unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart.
break;
}
?>
<?php
if($_SESSION['cart']) { //if the cart isn't empty
//show the cart
echo "<table border=\"1\" align=\"center\" padding=\"3\" width=\"70%\">";
echo "<tr>";
//show this information in table cells
echo "<td align=\"center\"><strong>Producto</strong></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 align=\"center\"><strong>Cantidad</strong></td>";
echo "<td align=\"center\"><strong>Costo</strong></td>";
echo "</tr>";//format the cart using a HTML table
//iterate through the cart, the $product_id is the key and $quantity is the value
foreach($_SESSION['cart'] as $product_id => $quantity) {
//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 name, description, price FROM products WHERE id = %d;",
$product_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, $description, $price) = mysql_fetch_row($result);
$line_cost = $price * $quantity; //work out the line cost
$total = $total + $line_cost; //add to the total cost
echo "<tr>";
//show this information in table cells
echo "<td align=\"center\"><strong>$name</strong></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 align=\"center\"><strong>$quantity </strong>Borrar</td>";
echo "<td align=\"center\"><strong>$line_cost</strong></td>";
echo "</tr>";
}
}
//show the total
echo "<tr>";
echo "<td colspan=\"2\" align=\"right\"><strong>Total</strong></td>";
echo "<td align=\"right\"><strong>$total</strong></td>";
echo "</tr>";
echo "</table>";
}else{
//otherwise tell the user they have no items in their cart
echo "No tiene articulos en compra.";
}
//function to check if a product exists
function productExists($product_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 products WHERE id = %d;",
$product_id);
return mysql_num_rows(mysql_query($sql)) > 0;
}
?>
</p>
<p><strong>Seguir Comprando</strong></p>
<?php
and now the display of the books / items / whatever you want.
<?php
define('MAX_REC_PER_PAGE', 1);
$sql = "SELECT id, name, description, price FROM products;";
$rs = mysql_query("SELECT COUNT(*) FROM products") or die("Imposible Realizar Operacion");
list($total) = mysql_fetch_row($rs);
$total_pages = ceil($total / MAX_REC_PER_PAGE);
$page = intval(#$_GET["page"]);
if (0 == $page){
$page = 1;
}
$start = MAX_REC_PER_PAGE * ($page - 1);
$max = MAX_REC_PER_PAGE;
$rs = mysql_query("SELECT id, name, description, price FROM products ORDER BY id
ASC LIMIT $start, $max") or die("Imposible Realizar Operacion");
?>
<table width="100%" height="404" border="0" cellpadding="12">
<?php
while (list($id, $name, $description, $price) = mysql_fetch_row($rs)) {
?>
<tr>
<td height="46" align="left" valign="middle"><p><strong> Producto :
<?= htmlspecialchars($name) ?>
</strong>
</p></td>
</tr>
<tr>
<td height="172" align="left" valign="middle"><p><strong>Descripcion :</strong></p>
<p>
<strong>
<?= htmlspecialchars($description) ?>
</strong></p></td>
</tr>
<tr>
<td height="67" align="left" valign="middle"><p><strong>Precio :
<?= htmlspecialchars($price) ?> </strong>
</p></td>
</tr>
<tr>
<td height="109" align="center" valign="middle"><strong><? echo "Comprar" ?> </strong></td>
</tr>
<?php
}
?>
</table>
<table border="0" cellpadding="5" align="center">
<tr>
<td><strong>Pagina : </strong></td>
<?php
for ($i = 1; $i <= $total_pages; $i++) {
$txt = $i;
if ($page != $i)
$txt = "$txt";
?>
<td align="center"><?= $txt ?></td>
<?php
}
?>
</table>
I have no idea where's the error on it...
Ty for the help :)
Obiusly you have to :
<?php session_start();?>
include your database
etc
You have in the style:
body {
background-image: url();
}
which is causing the browser to request the page again, which adds it to the cart again.
Instead of rendering the cart page, Once the code has modified the cart it should send a redirect to the cart page.

Categories