<?php
try{
include("dbconnectie.php");
$query = $db->prepare("SELECT * FROM shop WHERE id_u = :id");
$query->bindParam("id", $_SESSION['id_u']);
$query->execute();
$result = $query->fetchALL(PDO::FETCH_ASSOC);
echo "<table>";
foreach($result as &$data) {
echo "<tr>";
echo "<td>" . $data["brand"] . "</td>";
echo "<td>" . $data["model"] . "</td>";
echo "<td>" . $data["cond"] . "</td>";
echo "<td>" . $data["price"] . "</td>";
echo "</tr>";
}
echo "</table>";
} catch(PDOException $e) {
die("Error!: " . $e->getMessage());
}
?>
<html>
<body>
<div class="poster">
<img src="<?php echo $data['img_url']; ?>" width='400' height='300' ></img>
</div>
</body>
</html>
So in a different file, $_SESSION['id_u'] was defined as id_u which is in the 'account' table in my database. Now in the 'shop' table I have every sell placement written down with the corresponding user id: "id_u"
Now what I'm trying to do it Select all the sell placements that are put under that user id, but it's not working. Now for some reason it just shows a big border with nothing but a broken image icon. Not even the corresponding text.
Do a session_start(); at the top.
Related
I am trying to build a forum for my school project. Now i want to get the data out of my database so i can print it and people can see and react to it. Now i have the problem that it only print the commenters post and not the creators post. I will ad some picture and the code so you guys can undestand. (PS. i have a form on the previous page where you can search)
Database
Website
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="./style.css">
<title><?php echo $_GET["search"];?></title>
</head>
<body>
<?php
//navbar
include "./include/nav.php";
//database connection
include "./include/conn.php";
$search = $_GET["search"];
if($search == ""){
// if empty return to index
echo "
<script>alert('thread does not exist')</script>
<script>window.location = './index.php'</script>
";
} else {
//check if data exist
$sql = "SELECT thread, post, username, status FROM post WHERE thread = ? ";
$query = $conn->prepare($sql);
$query->execute(array($search));
$data = $query->fetch();
if($data){
// print data if exist
echo "<div class='padding'>";
foreach ($query as $test) {
echo "<tr>";
echo "<td>" . $test["thread"] . "</td>";
echo "<td>" . $test["post"] . "</td>";
echo "<td>" . $test["username"] . "</td>";
echo "<td>" . $test["status"] . "</td>";
echo "</tr>" . "</br>";
}
echo "<button onclick='comment()'>Comment</button>";
echo "</div>";
echo "<div id='test'>";
} else {
// if data does not exist return to index
echo "
<script>alert('thread does not exist')</script>
<script>window.location = './index.php'</script>
";
}
}
?>
<script>
// if comment btn is pressed save thread name and send to comment.php
function comment() {
document.getElementById("test").innerHTML =
"<?php
session_start();
$_SESSION['thread'] = $data['thread'];
?>";
window.location.assign("./comment.php")
}
</script>
</body>
</html>
I never used the data that i got out of the database. I should have printed it like this:
if($data){
// print data if exist
echo "<div class='padding'>";
foreach ($data as $test) {
echo "<tr>";
echo "<td>" . $test["thread"] . "</td>";
echo "<td>" . $test["post"] . "</td>";
echo "<td>" . $test["username"] . "</td>";
echo "<td>" . $test["status"] . "</td>";
echo "</tr>" . "</br>";
}
echo "<button onclick='comment()'>Comment</button>";
echo "</div>";
echo "<div id='test'>";
} else {
// if data does not exist return to index
echo "
<script>alert('thread does not exist')</script>
<script>window.location = './index.php'</script>
";
}
}
I am totally new to web development. I am doing a personal project for learning purpose.
I have a php page, which query my database and return the data.
My query is :
SELECT * FROM myTable ORDER BY id;
That works fine from a SQL GUI tool.
When I load my page, the data is not ordered by the ID. Each time I refresh the page, the order is randomly changed.
My HTML header section
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.12.4.min.js"></script>
<script>window.jQuery || document.write('<script src="/js/jquery-1.12.4.min.js">\x3C/script>')</script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
My loadData.php page
try {
$connection = new PDO($dsn, $username, $password, $options);
$sqlData = "SELECT * FROM cc_data_tbl where cc_uuid = '" . $_GET['load_uuid'];
$statement = $connection->query($sqlData);
$results = $statement->fetchAll(PDO::FETCH_ASSOC);
if ($statement->rowCount() == 0){
echo "No Data";
} else { ?>
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="page-header clearfix">
<?php
echo "<table class='table table-bordered table-striped' id=\"transactionList\">";
echo "<thead>";
echo "<tr>";
echo "<th>Transaction Date</th>";
echo "<th>Listing Date</th>";
echo "<th>Description</th>";
echo "<th>Amount</th>";
echo "<th>Category</th>";
echo "<th>Category Type</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
foreach($results as $row){
echo "<tr>";
echo "<td>" . $row['cc_transac_date'] . "</td>";
echo "<td>" . $row['cc_listing_date'] . "</td>";
echo "<td>" . $row['cc_description'] . "</td>";
echo "<td>" . $row['cc_amount'] . "</td>";
echo "<td>" . $row['cc_category'] . "</td>";
echo "<td>" . $row["cc_category_type"] . "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
?>
</div>
</div>
</div>
</div>
</div>
<?php }
} catch (PDOException $error) {
echo $error -> getMessage();
}
I was expecting to have my data sorted by ID, which is not the case.
This is a rather really basic SQL query... so I assume there's something that I really do not understand with HTML / PHP / Bootstrap as all these things are new to me.
if your problem still continue , please check your table class("table table-bordered table-striped") . Sorting variables in table class . I will give you example . I hope it helps for you.
$('#simple-table').DataTable({
"order": [[ 0, "desc" ]],
"autoWidth": true,
"language": {
"url": "//cdn.datatables.net/plug-ins/1.10.16/i18n/Turkish.json"
}
});
<?php
try{
include("dbconnectie.php");
$query = $db->prepare("SELECT * FROM shop WHERE id_img = '3'");
$query->execute();
$result = $query->fetchALL(PDO::FETCH_ASSOC);
$image = $result['img_url'];
echo "<table>";
foreach($result as &$data) {
echo "<tr>";
echo "<td>" . $data["brand"] . "</td>";
echo "<td>" . $data["model"] . "</td>";
echo "<td>" . $data["cond"] . "</td>";
echo "<td>" . $data["price"] . "</td>";
echo '<img src="data:image/png;base64,'.base64_encode( $data["image"] ).'"/>';
echo "</tr>";
}
echo "</table>";
} catch(PDOException $e) {
die("Error!: " . $e->getMessage());
}
?>
<html>
<body>
<a src='<?php echo $image; ?>' border='0'></a>
</body>
</html>
in line 7 u can see that i'm trying to define $image as the url that's saved in the database so i could use it all the way at the bottom to project it as an image.
Complete newbie to PHP/MySQL and HTML so please bear with me if I dont explain myself properly. At present I have an order form which stores the order in my database and shows it on a table in my admin area. I would like to be able to delete the row from the table and my database when I have completed the order.
At present my code looks like this:
<?php
//87229feely.php
include('connection.php');
$result = mysql_query("SELECT * FROM orderform");
echo "<table border='1' >
<tr>
<th><u>ID</th>
<th><u>Date</th>
<th><u>Product</th>
<th><u>Product Comments</th>
<th><u>Name</th>
<th><u>Address</th>
<th><u>Age</th>
<th><u>Delivery</th>
<th><u>Delete</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id']. "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . $row['productcomments'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['delivery'] . "</td>";
echo "<td>delete</td>";
echo "</tr>";
}
echo "</table>";
?>
<?php
//deleterow.php
include('connection.php');
$id = $_GET['id']; //this needs to be sanitized
if(!empty($id)){
$result = mysql_query("DELETE FROM orderform WHERE id=".$id.";");
}
header("Location: 87229feely.php");
?>
Any help? All greatly appreciated. Thanks
This answer does not meet security standards but should do the job:
<?php
//index.php
include('connection.php');
$result = mysql_query("SELECT * FROM orderform");
echo "<table border='1' >
<tr>
<th><u>ID</th>
<th><u>Date</th>
<th><u>Product</th>
<th><u>Product Comments</th>
<th><u>Name</th>
<th><u>Address</th>
<th><u>Age</th>
<th><u>Delivery</th>
<th><u>Delete</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id']. "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . $row['productcomments'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['delivery'] . "</td>";
echo "<td>delete</td>";
echo "</tr>";
}
echo "</table>";
?>
<?php
//delete.php
include('connection.php');
$id = $_GET['id']; //this needs to be sanitized
if(!empty($id)){
$result = mysql_query("DELETE FROM orderform WHERE id=".$id.";");
}
header("Location: index.php");
?>
Many ways to do this. Since you're a beginner, this would probably be the most straight-forward (albeit not how I would do it)
Create a form around the table
Add a button on the delete column for each row and assign an id to it (the ID should be the ID from your database) <input type="submit" name="submit" value"/*YOUR ID*/">
Add some processing script before the table is being parsed
if (isset($_POST['submit'])) {
$sql = "DELETE FROM table WHERE id='/*YOUR ID*/'";
mysql_query($sql);
}
First of all ,you need to send ID of completed order to next form. You cam do this by adding:
echo("<input type='hidden' name='orderID' value='".$row['id']."'/>");
That will create a hidden field with value of ID.
If your form uses POST:
then:
if(isset($_POST['submit'])){
$orderID = $_POST['orderID'];
mysql_query("DELETE FROM table WHERE id=$oderID");
}
If you are using GET method:
<form method="GET">
You could either use hidden field as mentioned above or you could parse ID of order in GET url:
<form action='action.php?id=".$row['id']."'>
and after submitting:
if(isset($_GET['submit']) && isset($_GET['id')){
$orderID = $_GET['id'];
mysql_query("DELETE FROM table WHERE id=$orderID");
}
Maybe something like this with PDO
<?php
include('connection.php');
?>
<form name="" action="delete.php" method="post">
<table border='1' >
<tr>
<th> </th>
<th><u>ID</th>
<th><u>Date</th>
<th><u>Product</th>
<th><u>Product Comments</th>
<th><u>Name</th>
<th><u>Address</th>
<th><u>Age</th>
<th><u>Delivery</th>
<th><u>Delete</th>
</tr>
<?php
try {
$conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$row = $conn->query('SELECT * FROM orderform');
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>";
echo "<td><input type=\"checkbox\" name=\"id[]\" id=\"checkAll\" value=\"".$row['id']."\" /></td>"
echo "<td>" . $row['id']. "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['product'] . "</td>";
echo "<td>" . $row['productcomments'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['age'] . "</td>";
echo "<td>" . $row['delivery'] . "</td>";
echo "</tr>";
}
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
?>
</table>
<input type="submit" name="submit" value="submit" />
</form>
delete.php
<?php
$id
if(isset($_POST['submit']) {
include('connection.php');
try {
$conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql="DELETE FROM orderform WHERE id IN (".implode(',',$conn->quote($id)).")";
$stmt->exec($sql);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
}
i have two table:
category
id | name
products
id | cat_id | name..
Like the title, I want to create a menu of category, when I click on one category, it will display all products in that category.I have try several way but it won't works! This is my menu.php
try {
$dbh = new PDO("mysql:host=$hostname;dbname=...", $username, $password);
$sql = "select * from category";
echo "<table>";
echo "<tr><th>ID</th><th>Name</th></tr>";
foreach ($dbh->query($sql) as $row)
{
$c_id=$row['c_id'];
echo "<tr>";
echo "<td>" . $row['c_id'] ."</td>";
echo "<td><a href='deal_list.php?cat_id=$c_id'>" . $row['c_name'] . "</td>";
echo "</tr>";
}
echo "</table>";
$dbh = null;
}
catch(PDOException $e) { echo $e->getMessage(); }
?>
and this is my product list
try {
$cat_id = $_GET['cat_id'];
$dbh = new PDO("mysql:host=$hostname;dbname=..", $username, $password);
$sql = "select id,name,description,price,groupbuy_price, CEIL((groupbuy_price/price)*100) AS saving,current_buyer,maximum_buyer,expired_time,status,sum_img,cat_id,c_name from groupbuy,category where c_id=cat_id";
echo "<table border=2px>";
echo "<tr><th>Name</th><th>Description</th><th>Price</th><th>Group buy price</th><th>Saving</th><th>Number of current buyer</th><th>Maximum Buyer</th><th>Expired Time</th><th>Status</th><th>Category</th><th>Sumary image</th></tr>";
foreach ($dbh->query($sql) as $row)
{
$id=$row['id'];
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['description'] ."</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>" . $row['groupbuy_price'] ."</td>";
echo "<td>" . $row['saving'] .'%' ."</td>";
echo "<td>" . $row['current_buyer'] ."</td>";
echo "<td>" . $row['maximum_buyer'] ."</td>";
echo "<td>" . $row['expired_time'] ."</td>";
echo "<td>" . $row['status'] ."</td>";
echo "<td>" . $row['c_name'] . "</td>";
?>
<td><img src="<?php echo $row['sum_img']?>" width="50px" /></td>
<?php
echo "<td><a href='delete_deal.php?id=$id'>Delete</a></td>";
echo "<td><a href='deal_detail.php?id=$id'>Detail</a></td>";
echo "<td><a href='edit_deal.php?id=$id'>Edit</a></td>";
echo "</tr>";
}
echo "</table>";
$dbh = null;
}
catch(PDOException $e) { echo $e->getMessage(); }
?>
looks like you just need to replace
$sql = "select id,name,description,price,groupbuy_price, CEIL((groupbuy_price/price)*100) AS saving,current_buyer,maximum_buyer,expired_time,status,sum_img,cat_id,c_name from groupbuy,category where c_id=cat_id";
with
$sql = "select id,name,description,price,groupbuy_price, CEIL((groupbuy_price/price)*100) AS saving,current_buyer,maximum_buyer,expired_time,status,sum_img,cat_id,c_name from groupbuy,category where c_id={$cat_id}";
but for secure reasons change also in begining
$cat_id = (int) $_GET['cat_id'];