get data depend on particular id using php and mysql - php

i just create the simple cart app.in this i add products to the cart,from that i want to buy the one particular product.So i want to get and display product based on product id. In this, i had receiving the error.Can anyone help to solve this problem?
Inserting product to database:
<?php
Include('connect.php');
$productId=$_POST['productId'];
$productName=$_POST['productName'];
$productPrice=$_POST['productPrice'];
$quantity=$_POST['quantity'];
$sql=mysql_query("INSERT INTO products (productId,productName,productPrice,quantity)
VALUES ('$productId','$productName','$productPrice','$quantity')") or die(mysql_error());
if($sql){
header('Location:buy.php');
}
else
{
echo"Failed to add cart !";
}
?>
Adding products to cart:
<?php
include('connect.php');
$sql="SELECT * FROM buy";
$result = mysql_query($sql);
$myArray = array();
echo "<table style='width:50%;'>
<tr>
<th>Product Name</th>
<th>Product Price</th>
<th>Quantity</th>
</tr>";
$index = 0;
while($row = mysql_fetch_assoc($result))
{
$myArray[$index] = $row;
$index++;
echo "<tr>";
echo "<td>" . $row['productName'] . "</td>";
echo "<td>" . $row['productPrice'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo "<td><a href='quantity.php'><button class='btn btn-default col-md- offset-2' value='submit'>Buy Now</button></a></td>" ;
echo "<td><button class='btn btn-danger' value='submit'>Buy Later</button> </td>" ;
echo "</tr>";
}
?>
choose product and display product based on id:
<?php
include('connect.php');
$productId=$_GET['productId'];
$sql="SELECT productName, productPrice, quantity FROM products WHERE productId=" . $productId . "";
$result = mysql_query($sql);
$myArray = array();
echo "<table style='width:50%;'>
<tr>
<th>Product Name</th>
<th>Product Price</th>
<th>Quantity</th>
</tr>";
$index = 0;
while($row = mysql_fetch_assoc($result))
{
$myArray[$index] = $row;
$index++;
echo "<tr>";
echo "<td>" . $row['productName'] . "</td>";
echo "<td>" . $row['productPrice'] . "</td>";
echo "<td>" . $row['quantity'] . "</td>";
echo "</tr>";
}
?>

Related

PHP - Code is create individual tables for each record from database

I'm trying to create one table that will loop through all the records from my database but it seems to be create individual tables for each record instead.
$query = 'select * from images';
$result = mysqli_query($connection,$query);
$row_count = mysqli_num_rows($result);
for($i=0; $i<$row_count; $i++){
$row[] = mysqli_fetch_array($result);
}
echo '<header><h1 class="gallerytitle">Photo Gallery</h1></header>';
foreach($row as $next) {
{
echo "<table border='1'>
<tr>
<th>Imageid</th>
<th>Image name</th>
<th>Description</th>
<th>Image</th>
<th>Caption</th>
</tr>";
echo "<tr>";
echo "<td>" . $next['imageid'] . "</td>";
echo "<td>" . $next['imagename'] . "</td>";
echo "<td>" . $next['description'] . "</td>";
echo "<td><img src='".$next['image']."' width='20%' height='auto'></td>";
echo "<td>" . $next['caption'] . "</td>";
echo "<td> <a class='readmore' href='delete_confirm.php?imageid={$next['imageid']}'>Delete</a></td>";
echo "<td> <a class='readmore' href='update_form.php?imageid={$next['imageid']}'>Update</a></td>";
echo "</tr>";
}
echo "</table>";
echo '</section>';
}
Ideally, you would loop around the tr
echo "<table border='1'>
<tr>
<th>Imageid</th>
<th>Image name</th>
<th>Description</th>
<th>Image</th>
<th>Caption</th>
</tr>";
foreach($row as $next) {
{
echo "<tr>";
echo "<td>" . $next['imageid'] . "</td>";
echo "<td>" . $next['imagename'] . "</td>";
echo "<td>" . $next['description'] . "</td>";
// all the other columns
echo "</tr>";
}
echo "</table>";

view detail of single row of MySQL table in new page after click

I have a page that display all job from MySQL database.
this is the code:
<?php
$result = mysqli_query($conn,"SELECT * FROM job ORDER BY `CreatedTime` DESC");
echo "<table border='0' cellpadding='0' cellspacing='0' class='table-fill'>
<tr>
<th width='250px' position='fixed'>Job Title</th>
<th width='150px'>Company Name</th>
<th width='100px'>Location</th>
<th>Closing Date</th>
</tr>";
while($row = mysqli_fetch_array($result) ) {
echo "<tr>";
echo "<td>" . $row['positiontitle'] . "</td>";
echo "<td>" . $row['companyname'] . "</td>";
echo "<td>" . $row['location'] . "</td>";
echo "<td>" . $row['closingdate'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
I want when i click on one of the job from the table then it should display the whole information in new page.
thanks
Add one more row to your table
<th>View Job</th>
Your code :
$result = mysqli_query($conn,"SELECT * FROM job ORDER BY `CreatedTime` DESC");
echo "<table border='0' cellpadding='0' cellspacing='0' class='table-fill'>
<tr>
<th width='250px' position='fixed'>Job Title</th>
<th width='150px'>Company Name</th>
<th width='100px'>Location</th>
<th>Closing Date</th>
<th>View Job</th>
</tr>";
while($row = mysqli_fetch_array($result) ) {
echo "<tr>";
echo "<td>" . $row['positiontitle'] . "</td>";
echo "<td>" . $row['companyname'] . "</td>";
echo "<td>" . $row['location'] . "</td>";
echo "<td>" . $row['closingdate'] . "</td>";
echo "<td><a href='job_details.php?id=".$row['job_id']."'>View Job</td>";
echo "</tr>";
}
And in job_details.php fetch the details against the id and display the result on that page.
jobdetails.php :
<?php
$result = mysqli_query($conn,"SELECT * FROM job WHERE job_id = '".$_GET['id']."' ORDER BY `CreatedTime` DESC");
$jobdetails = mysqli_fetch_assoc($result);
echo 'Position : '.$jobdetails['positiontitle'].'<br>';
echo 'Company Name: '.$jobdetails['companyname'].'<br>';
echo 'Location: '.$jobdetails['location'].'<br>';
echo 'Closing Date: '.$jobdetails['closingdate'].'<br>';
?>
you can also use this id= instead of id=".$row['job_id']."

unable to sum the values that are fetched from the database and display

I am making a project on proposal management system and I am stuck on the pricing module. Here is my HTML DEMO. I have shown only required field in the output. INSERT queries below are correct.
I am trying to AUTOMATICALLY display the sum of the 'amount' in the 'total' each time the user presses 'save' button . The value of 'total' should increment when user presses 'save' either from 'add services' table or from 'add products' table. I am unsuccessful so far. HOW CAN I DO IT?
I have tried this: (Any other solution will be appreciated).
<td> <input type="text" name="amount" maxlength="15" id="amount" readonly="true" value=" <?php
if(isset($_POST['save'])|| isset($_POST['psave']))
{
$con= mysqli_connect("localhost","root","","my_db")or die("cannot connect");
$sql = "SELECT proposal_services.service_amount AMOUNT,proposal_products.product_amount AMOUNT1, FROM proposal_services INNER JOIN proposal_products ON proposal_products.proposal_id=proposal_services.proposal_id";
$result= mysqli_query($con,$sql);
$row = mysqli_fetch_array($result);
foreach ( $row as $records )
{
$sum = str_replace(",", "", $records['AMOUNT']) + str_replace(",", "", $records['AMOUNT1']);
}
echo number_format( $sum, 2 );
}
?> "/></td>
my php code where I insert and display results is:
insert_services.php
<?php
session_start();
$prop_id = $_SESSION['propl_id'];
$user_id = $_SESSION['sign_user_id'];
$con = mysqli_connect("localhost","root","","my_db");
$name = $_POST['ser_name'];
$desc = $_POST['txt'];
$price = $_POST['price'];
$per = $_POST['per'];
$quantity = $_POST['quantity'];
$amount = $_POST['amount'];
if(mysqli_query($con,"INSERT INTO proposal_services(proposal_id,user_id,service_name,service_description,service_price,per,service_quantity,service_amount) VALUES('$prop_id','$user_id','$name','$desc','$price','$per','$quantity','$amount')"))
$sql= "select * FROM proposal_services WHERE proposal_id='$prop_id' ";
$result= mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th align='center'>Service Name</th>
<th align='center'>Description</th>
<th align='center'>Price</th>
<th align='center'>per</th>
<th align='center'>Quantity</th>
<th align='center'>Amount</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<div id='change'>";
echo "<tr>";
echo "<td align='center'>" . $row['service_name'] . "</td>";
echo "<td align='center'>" . $row['service_description']. "</td>";
echo "<td align='center'>" . $row['service_price']. "</td>";
echo "<td align='center'>" . $row['per']. "</td>";
echo "<td align='center'>" . $row['service_quantity']. "</td>";
echo "<td align='center'>" . $row['service_amount']. "</td>";
echo "</tr>";
echo "</div>";
}
echo "</table>";
?>
insert_products.php
<?php
session_start();
$prop_id = $_SESSION['propl_id'];
$user_id = $_SESSION['sign_user_id'];
$con = mysqli_connect("localhost","root","","my_db");
$name = $_POST['pro_name'];
$desc = $_POST['txt'];
$price = $_POST['price'];
$per = $_POST['per'];
$quantity = $_POST['quantity'];
$amount = $_POST['amount'];
if(mysqli_query($con,"INSERT INTO proposal_products(proposal_id,user_id,product_name,product_description,product_price,per,product_quantity,product_amount) VALUES('$prop_id','$user_id','$name','$desc','$price','$per','$quantity','$amount')"))
$sql= "select * FROM proposal_products WHERE proposal_id='$prop_id'";
$result= mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Product Name</th>
<th>Description</th>
<th>Price</th>
<th>per</th>
<th>Quantity</th>
<th>Amount</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['product_name'] . "</td>";
echo "<td>" . $row['product_description'] . "</td>";
echo "<td>" . $row['product_price'] . "</td>";
echo "<td>" . $row['per'] . "</td>";
echo "<td>" . $row['product_quantity'] . "</td>";
echo "<td>" . $row['product_amount'] . "</td>";
echo "<td align='center'>" . "<input type='button' value= 'edit'>" . "</td>";
echo "</tr>";
}
echo "</table>";
?>

Output not displaying all rows

Having an issue with the output of records from a query run to display records.... It only shows the first row as the code specifies and then the next results all in.. paragraphs? I don't know if it has something to do
<?php
include 'core/init.php';
include 'includes/overall/header.php';
?>
<div class="article" style="width:900px !important">
<?php
$result = $sql = mysql_query("SELECT * FROM ref_employees WHERE employerid={$user_data['user_id']} ")
or die('Error in query : $sql. ' .mysql_error());
echo "<table border='0' class='table'>
<tr>
<th>ID Number</th>
<th>Employee Number</th>
<th>FirstName</th>
<th>LastName</th>
<th>MiddleName</th>
<th>Job Title</th>
<th>Employement Status</th>
<th>Contact</th>
<th>Email</th>
<th>Edit</th>
</tr>";
if (mysql_num_rows($sql) > 0)
{
while ($row = mysql_fetch_array($sql)){
if ($row['employed'] == '1'){
echo "<tr>";
echo "<td>" . $row['idnumber'] . "</td>";
echo "<td>" . $row['empnumber'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['middlename'] . "</td>";
echo "<td>" . $row['jobtitle'] . "</td>";
echo "<td>" . $row['employed'] . "</td>";
echo "<td>" . $row['contactnum'] . "</td>";
echo "<td>" . $row['contactemail'] . "</td>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "</tr>";
echo "</tr>";
echo "</table>";
}
}
}
?>
</div>
<?php include 'includes/overall/footer.php';
?>
You are using closing table tag into loop as
while ($row = mysql_fetch_array($sql)){
....
....
...
echo "</table>";
}
use table closing tag out of loop as
while ($row = mysql_fetch_array($sql)){
....
....
...
}
echo "</table>";

display multiple table as query result using ORDER BY in PHP MYSQL

I need a help on these, where I want to display the query result with multiple table as the assets have different attributes, it will be ORDER BY category. Let's say, Category = Laptop will list all the laptop details, TV will have its own table with its features & so on. All of this will be on the same page but breakdown by tables. How can I achieve this? Here's the part where I suppose the problem lies. Any help is highly appreciated!
$result = mysql_query($sql) or die (mysql_error());
if(mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_array($result))
{
$assetid = $row['assetid'];
$name = $row['name'];
$category = $row['category'];
$manufacturer = $row['manufacturer'];
$type = $row['type'];
$size = $row['size'];
$price = $row['price'];
$warranty = $row['warranty'];
$description = $row['description'];
if ($category == "1 - LAPTOP")
{
echo "<table border='1'>
<tr>
<th>Asset ID</th>
<th>Category</th>
<th>Name | Model</th>
<th>Manufacturer</th>
<th>Type</th>
<th>Price</th>
<th>Warranty</th>
<th>Description</th>
</tr>";
echo "<tr>";
echo "<td>" . $assetid . "</td>";
echo "<td>" . $category . "</td>";
echo "<td>" . $name. "</td>";
echo "<td>" . $manufacturer. "</td>";
echo "<td>" . $type. "</td>";
echo "<td>" . $price . "</td>";
echo "<td>" . $warranty . "</td>";
echo "<td>" . $description . "</td>";
echo "</tr>";
echo "</table>";
}
elseif ($category == "2 - TV")
{
echo "<table border='1'>
<tr>
<th>Asset ID</th>
<th>Category</th>
<th>Name | Model</th>
<th>Manufacturer</th>
<th>Type</th>
<th>Price</th>
<th>Warranty</th>
<th>Description</th>
</tr>";
echo "<tr>";
echo "<td>" . $assetid . "</td>";
echo "<td>" . $category . "</td>";
echo "<td>" . $name. "</td>";
echo "<td>" . $manufacturer. "</td>";
echo "<td>" . $type. "</td>";
echo "<td>" . $price . "</td>";
echo "<td>" . $warranty. "</td>";
echo "<td>" . $description . "</td>";
echo "</tr>";
echo "</table>";
}
elseif ($subassetcategory == "3 - DESK")
{
echo "<table border='1'>
<tr>
<th>Asset ID</th>
<th>Category</th>
<th>Name | Model</th>
<th>Manufacturer</th>
<th>Type</th>
<th>Price</th>
<th>Description</th>
</tr>";
echo "<tr>";
echo "<td>" . $assetid . "</td>";
echo "<td>" . $category . "</td>";
echo "<td>" . $name. "</td>";
echo "<td>" . $manufacturer. "</td>";
echo "<td>" . $type. "</td>";
echo "<td>" . $price . "</td>";
echo "<td>" . $description . "</td>";
echo "</tr>";
echo "</table>";
}
elseif ($subassetcategory == "4 - TELEPHONE")
{
echo "<table border='1'>
<tr>
<th>Asset ID</th>
<th>Category</th>
<th>Name | Model</th>
<th>Manufacturer</th>
<th>Type</th>
<th>Description</th>
</tr>";
echo "<tr>";
echo "<td>" . $assetid . "</td>";
echo "<td>" . $category . "</td>";
echo "<td>" . $name. "</td>";
echo "<td>" . $manufacturer. "</td>";
echo "<td>" . $type. "</td>";
echo "<td>" . $description . "</td>";
echo "</tr>";
echo "</table>";
}
}
}
else
{
echo "<br> No record found </br>";
}
The big problem I see in your code is its repetition, it completely breaks the DRY principle. Also, you have your categories hard coded in your code, and stored in your DB. I modified the script, so that it should create now a generic table header whenever a new category is found in the resultset.
Please try this (instead of all your code) and see if it works for you:
$categ = '';
$result = mysql_query($sql) or die (mysql_error());
if(mysql_num_rows($result) > 0)
{
while($row = mysql_fetch_array($result))
{
$assetid = $row['assetid'];
$name = $row['name'];
$category = $row['category'];
$manufacturer = $row['manufacturer'];
$type = $row['type'];
$size = $row['size'];
$price = $row['price'];
$warranty = $row['warranty'];
$description = $row['description'];
if ($category != $categ)
{
$categ = $category;
echo "<table border='1'>
<tr>
<th>Asset ID</th>
<th>Category</th>
<th>Name | Model</th>
<th>Manufacturer</th>
<th>Type</th>
<th>Price</th>
<th>Warranty</th>
<th>Description</th>
</tr>";
}
echo "<tr>";
echo "<td>" . $assetid . "</td>";
echo "<td>" . $category . "</td>";
echo "<td>" . $name. "</td>";
echo "<td>" . $manufacturer. "</td>";
echo "<td>" . $type. "</td>";
echo "<td>" . $price . "</td>";
echo "<td>" . $warranty . "</td>";
echo "<td>" . $description . "</td>";
echo "</tr>";
echo "</table>";
} //while
} //if
This code assumes that your results are comming with ORDER BY category
What about doing it like this? :
$result = mysql_query($sql) or die (mysql_error());
if(mysql_num_rows($result) > 0)
{
if ($category == "1 - LAPTOP")
{
echo "<table border='1'>
<tr>
<th>Asset ID</th>
<th>Category</th>
<th>Name | Model</th>
<th>Manufacturer</th>
<th>Type</th>
<th>Price</th>
<th>Warranty</th>
<th>Description</th>
</tr>";
}
while($row = mysql_fetch_array($result))
{
$assetid = $row['assetid'];
$name = $row['name'];
$category = $row['category'];
$manufacturer = $row['manufacturer'];
$type = $row['type'];
$size = $row['size'];
$price = $row['price'];
$warranty = $row['warranty'];
$description = $row['description'];
if ($category == "1 - LAPTOP")
{
echo "<tr>";
echo "<td>" . $assetid . "</td>";
echo "<td>" . $category . "</td>";
echo "<td>" . $name. "</td>";
echo "<td>" . $manufacturer. "</td>";
echo "<td>" . $type. "</td>";
echo "<td>" . $price . "</td>";
echo "<td>" . $warranty . "</td>";
echo "<td>" . $description . "</td>";
echo "</tr>";
echo "</table>";
}
}
}
else
{
echo "<br> No record found </br>";
}

Categories