How to put php variable in an sql query - php

So this is my code:
<nav class="navbar navbar-inverse">
<div class="container-fluid">
<div class="navbar-header">
<a class="navbar-brand" href="#">WebSiteName</a>
</div>
<ul class="nav navbar-nav">
<li class="active">Home</li>
<?php
$dbconn= new PDO('sqlite:negozio.db');
$sqlcate = "SELECT * FROM categoria";
foreach($dbconn->query($sqlcate) as $row) { ?>
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#"><?php echo $row['des_categoria']; ?> <span class="caret"></span></a>
<ul class="dropdown-menu">
<?php
$sqltipocate = "SELECT tipo.des_tipo FROM tipo, categoria, tipo_cate WHERE tipo_cate.id_cate = categoria.id_categoria AND tipo_cate.id_tipo = tipo.id_tipo AND tipo_cate.id_categoria = " . $row['id_categoria'] . " "" ";
foreach($dbconn->query($sqltipocate) as $row1) { ?>
<li><?php echo $row1['des_tipo']; ?></li>
<?php } ?>
</ul>
</li>
<?php } ?>
</ul>
</div>
</nav>
Everything is working fine until $sqltipocate blabla...
This error always appears:
syntax error, unexpected T_CONSTANT_ENCAPSED_STRING
Can someone help me?

It is concatenation problem as regarded in previous answers. I can add something may make your life easier. It is sprintf
Your sql query string will be the format parameter of sprintf as follows:
$sqltipocate = sprintf("SELECT tipo.des_tipo FROM tipo, categoria, tipo_cate WHERE tipo_cate.id_cate = categoria.id_categoria AND tipo_cate.id_tipo = tipo.id_tipo AND tipo_cate.id_categoria = %d", $row['id_categoria']);

Use curly braces.
Example:
$id = 1;
$sql = "SELECT * FROM something WHERE id = {$id}";
echo $sql;
//SELECT * FROM something WHERE id = 1
Link to code: http://codepad.org/V9QasGHH

Store your $row['id_categoria'] in variable like
$id = $row['id_categoria'];
Then put $id in query like:
$sqltipocate = "SELECT tipo.des_tipo FROM tipo, categoria, tipo_cate WHERE tipo_cate.id_cate = categoria.id_categoria AND tipo_cate.id_tipo = tipo.id_tipo AND tipo_cate.id_categoria = '".$id."'";
See this tipo_cate.id_categoria = ' " $id " '

Related

how to change the way the url looks but keep the function

How do I change my URL's function for calling up on pages?
Here's an example: https://exaplesitename.com/index.php?id=21
What it looks like now but I want it to be something like this: https://exaplesitename.com/Entertainment
This is the navigation code:
<?php
function find_parent_categories($options=[]) {
global $db;
$Active = $options['Active'] ?? 'false';
$sql = "SELECT * FROM Parent_Categories ";
if($Active){
$sql .= "WHERE Active = true ";
}
$sql .= "ORDER BY Category_Order ASC";
$result = mysqli_query($db, $sql);
confirm_result_set($result);
return $result;
}
function find_categories($id_Category_Parent, $options=[]){
global $db;
$Active = $options['Active'] ?? 'false';
$sql = "SELECT * FROM Categories ";
$sql .= "WHERE id_Category_Parent='" . db_escape($db, $id_Category_Parent) . "' ";
if($Active){
$sql .= "AND Active = true ";
}
$sql .= "ORDER BY Category_Order ASC";
$result = mysqli_query($db, $sql);
confirm_result_set($result);
return $result;
} ?>
<nav class="navbar fixed-top navbar-expand-lg bg-white navbar-custom">
<a class="navbar-brand" href="<?php echo url_for('/index.php'); ?>">
<img src="../../images/Logo1.png" alt="Nantucket" class="img-fixed"/>
</a>
<button class="navbar-toggler custom-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent1" aria-controls="navbarSupportedContent1" aria-expanded="false" aria-label="Toggle navigation">Menu
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent1">
<?php $nav_categories = find_parent_categories(['Active' => true]); ?>
<ul class="navbar-nav mr-auto">
<?php while($nav_category = mysqli_fetch_assoc($nav_categories)) { ?>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="<?php echo url_for('index.php?id=' . h(u($nav_category['id']))); ?>" id="navbarDropdown1" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false"><?php echo h($nav_category['Category_Name']); ?></a>
<?php $nav_sub_categories = find_categories($nav_category[id], ['Active' => true]); ?>
<ul class="dropdown-menu" aria-labelledby="navbarDropdown1">
<?php while($nav_sub_category = mysqli_fetch_assoc($nav_sub_categories)) { ?>
<li>
<a class="dropdown-item" href="<?php echo url_for('index.php?id=' . h(u($nav_sub_category['id']))); ?>"> <?php echo h($nav_sub_category['Category_Name']); ?></a>
</li>
<?php } //while $nav_sub_categories ?>
</ul>
<?php mysqli_free_result($nav_sub_categories); ?>
<?php mysqli_free_result($nav_categories); ?>
</li>
<?php } ?>
</ul>
</div>
</nav>
Thank you very much for any help and kindness.
Any change in url can be achieved by changing .htaccess file.
RewriteEngine On
RewriteRule https://exaplesitename.com/Entertainment https://exaplesitename.com/index.php?id=21
For more information about .htaccess please refer to:
https://www.smashingmagazine.com/2011/11/introduction-to-url-rewriting/

How to echo out 'include_once' inside html [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 5 years ago.
Let's say I have a file which goes like this:
<?php
if(isset($_GET['cat_id'])){
$cat_id = $_GET['cat_id'];
$get_cat_pro = "select * from products where cat_id='$cat_id'";
$run_cat_pro = mysqli_query($con,$get_cat_pro);
while($row_cat_pro=mysqli_fetch_array($run_cat_pro)){
$pro_id = $row_cat_pro['product_id'];
$pro_title = $row_cat_pro['product_title'];
$pro_cat = $row_cat_pro['cat_id'];
$pro_brand = $row_cat_pro['brand_id'];
$pro_desc = $row_cat_pro['product_desc'];
$pro_price = $row_cat_pro['product_price'];
$pro_image = $row_cat_pro['product_img1'];
echo "
<section>
<div class='container'>
<div class='row'>
"include_once 'php/includes/overall/widget.php'"
<div class='col-sm-9 padding-right'>
<div class='features_items'><!--features_items-->
<h2 class='title text-center'>Products</h2>
<ul class='pagination'>
<li class='active'>1</li>
<li><a href=''>2</a></li>
<li><a href=''>3</a></li>
<li><a href=''>»</a></li>
</ul>
</div><!--features_items-->
</div>
</div>
</div>
</section>
";
}
}
?>
And in this file I want to echo out an include_once command but I don't know the proper way to do this while echoing some html text.. So I get this error message as well:
Parse error: syntax error, unexpected 'include_once' (T_INCLUDE_ONCE), expecting ',' or ';' on line 19
And the line 19 is this:
"include_once 'php/includes/overall/widget.php'"
So how to correct this issue?
When you want to get into PHP context, you have to put <?php ?> tags in your HTML. Here you are using echo to output HTML from PHP, and requiring to do PHP again. If you want the HTML to switch to PHP to execute the include_once, you have to put the tags again. In your code, the include_once is viewed as HTML, which is not correct, hence the error.
But it gets a bit crazy with php that echos HTML, that requires PHP. So why not simply exit the PHP context to output your html, and get back in for the include_once?
Like so:
<?php
if(isset($_GET['cat_id']))
{
$cat_id = $_GET['cat_id'];
$get_cat_pro = "select * from products where cat_id='$cat_id'";
$run_cat_pro = mysqli_query($con,$get_cat_pro);
while($row_cat_pro=mysqli_fetch_array($run_cat_pro))
{
$pro_id = $row_cat_pro['product_id'];
$pro_title = $row_cat_pro['product_title'];
$pro_cat = $row_cat_pro['cat_id'];
$pro_brand = $row_cat_pro['brand_id'];
$pro_desc = $row_cat_pro['product_desc'];
$pro_price = $row_cat_pro['product_price'];
$pro_image = $row_cat_pro['product_img1'];
?>
<section>
<div class='container'>
<div class='row'>
<?php include_once('php/includes/overall/widget.php'); ?>
<div class='col-sm-9 padding-right'>
<div class='features_items'><!--features_items-->
<h2 class='title text-center'>Products</h2>
<ul class='pagination'>
<li class='active'>1</li>
<li><a href=''>2</a></li>
<li><a href=''>3</a></li>
<li><a href=''>»</a></li>
</ul>
</div><!--features_items-->
</div>
</div>
</div>
</section>
<?php
}
}
?>
Try the following...
Because you started the echo with " you can't have " inside it, you can only use " again when the echo is ending or when you're connecting it to another string.
As you can see I connected it by using periods like so:
...ss='row'>" . include_once ('php/includes/overall/widget.php'); ."<div clas...
<?php
if(isset($_GET['cat_id'])){
$cat_id = $_GET['cat_id'];
$get_cat_pro = "select * from products where cat_id='$cat_id'";
$run_cat_pro = mysqli_query($con,$get_cat_pro);
while($row_cat_pro=mysqli_fetch_array($run_cat_pro)){
$pro_id = $row_cat_pro['product_id'];
$pro_title = $row_cat_pro['product_title'];
$pro_cat = $row_cat_pro['cat_id'];
$pro_brand = $row_cat_pro['brand_id'];
$pro_desc = $row_cat_pro['product_desc'];
$pro_price = $row_cat_pro['product_price'];
$pro_image = $row_cat_pro['product_img1'];
echo "
<section>
<div class='container'>
<div class='row'>".
include_once ('php/includes/overall/widget.php');
."<div class='col-sm-9 padding-right'>
<div class='features_items'><!--features_items-->
<h2 class='title text-center'>Products</h2>
<ul class='pagination'>
<li class='active'>1</li>
<li><a href=''>2</a></li>
<li><a href=''>3</a></li>
<li><a href=''>»</a></li>
</ul>
</div><!--features_items-->
</div>
</div>
</div>
</section>
";
}
}
echo "
<section>
<div class='container'>
<div class='row'>";
include_once 'php/includes/overall/widget.php';
echo " <div class='col-sm-9 padding-right'>
<div class='features_items'><!--features_items-->
<h2 class='title text-center'>Products</h2>
<ul class='pagination'>
<li class='active'>1</li>
<li><a href=''>2</a></li>
<li><a href=''>3</a></li>
<li><a href=''>»</a></li>
</ul>
</div><!--features_items-->
</div>
</div>
</div>
</section>
";
First off, since you want to echo a multiline string, I'd use Heredoc.
You could use fread(). Here's an example of what that would look like.
$str = fread(fopen('php/includes/overall/widget.php', 'a+'), filesize('php/includes/overall/widget.php'));
echo "
ALL YOUR HTML
{$str}
SOME MORE HTML";

Fetch subcategory in fetched category

What i am trying to build is next. Fetch all my menu categories and display them, then fetch all subcategories for each category special all display it also. I hope you understand what i want and saying. I got lost in code and don't now how to do this. Here is my code, hope you can help me.
Here is table structure.
subcategory table
| id | subcategory_name | id_category |
category table
| id | category |
id_category is connected with id in category table
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-left">
<?php
$pdo = connect();
$sql = "SELECT * FROM category";
$query = $pdo->prepare($sql);
$query->execute();
$row = $query->fetchAll();
foreach ($row as $rs) { ?>
<li class="dropdown"><?php echo $rs['category'] ?><span class="caret"></span>
<ul class="dropdown-menu" role="menu">
<?php
$sql = "SELECT subcategory.subcategory_name, subcategory.id_category, category.id, category.category
FROM subcategory
INNER JOIN category
WHERE subcategory.id_category = category.category";
$query = $pdo->prepare($sql);
$query->execute();
$subcat = $query->fetchAll();
foreach ($subcat as $sub) { ?>
<li><?php echo $sub['subcategory_name'] ?></li>
<?php } ?>
</ul>
</li>
<?php } ?>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>Logout</li>
</ul>
</div><!-- /.navbar-collapse -->
</div>
I think you just didn't understand the INNER JOIN sql select statement. You have to declare the relation of the second table with the first one:
SELECT column_list
FROM t1
INNER JOIN t2 ON join_condition1
WHERE where_conditions;
Join condition in where you want to put the "subcategory.id_category = category.category" part of your query. Maybe leave out the actual WHERE clause?
Hope it helps.
Look here
Edit:
WHat I was proposing pointed to rewrite the complete code to do it in one query. But your code could be simplified like this quickly using the already captured variable with the first sql query:
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-left">
<?php
$pdo = connect();
$sql = "SELECT * FROM category";
$query = $pdo->prepare($sql);
$query->execute();
$row = $query->fetchAll(PDO::FETCH_ASSOC);
foreach ($row as $rs) { ?>
<li class="dropdown"><?php echo $rs['category'] ?><span class="caret"></span>
<ul class="dropdown-menu" role="menu">
<?php
$sql = "SELECT subcategory_name, id_category
FROM subcategory
WHERE id_category = '".$rs['id']."'";
$query = $pdo->prepare($sql);
$query->execute();
$subcat = $query->fetchAll(PDO::FETCH_ASSOC);
foreach ($subcat as $sub) { ?>
<li><?php echo $sub['subcategory_name'] ?></li>
<?php } ?>
</ul>
</li>
<?php } ?>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>Logout</li>
</ul>
</div><!-- /.navbar-collapse -->
</div>

Nidified mysqli_fetch_assoc while

I have a problem with the following code:
$query = "SELECT * FROM movie_list WHERE id=$id";
$result_q = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result_q);
if ($row['movie_type'] == 'E' || $row['movie_type'] == 'S'){
$query_serie = "SELECT * FROM movie_list WHERE id_serie=$row[id_serie]";
$result_q_serie = mysqli_query($conn, $query_serie);
$query_serie_cont = "SELECT serie_number FROM movie_list WHERE id_serie = $row[id_serie] AND serie_number IS NOT null GROUP BY serie_number";
$result_q_serie_cont = mysqli_query($conn, $query_serie_cont);
[...]
<?php
while($row_serie_cont = mysqli_fetch_assoc($result_q_serie_cont)){ ?>
<li>
<a class="collapsible-header collapsible-header waves-effect waves-teal bold">Season <?php echo $row_serie_cont['serie_number']?></a>
<div class="collapsible-body">
<ul>
<?php
while($row_serie = mysqli_fetch_assoc($result_q_serie)){
if ($row_serie['serie_number'] == $row_serie_cont['serie_number']){
echo "<li>".$row_serie['episode_number']."</li>";
}
}
echo "</ul>";
echo "</div>";
}
?>
</li>
It works perfect the first while but the second time the while($row_serie) variable are missing. Debugging the page I see the $row_serie variable disappear after completed all the first while($row_serie_cont) but not reappearing at all when it was triggered the second time.
What I've miss in the code?
you close li after loop make it correct
while($row_serie_cont = mysqli_fetch_assoc($result_q_serie_cont)){ ?>
<li>
<a class="collapsible-header collapsible-header waves-effect waves-teal bold">Season <?php echo $row_serie_cont['serie_number']?></a>
<div class="collapsible-body">
<ul>
<?php
while($row_serie = mysqli_fetch_assoc($result_q_serie)){
if ($row_serie['serie_number'] == $row_serie_cont['serie_number']){
echo "<li>".$row_serie['episode_number']."</li>";
}
}
echo "</ul>";
echo "</div>";
echo "</li>"// inside the loop
}
?>
Seems that insert $result_q_serie = mysqli_query($conn, $query_serie);into the second while do the trick.
I don't understand why i need to repopulate result_q_serie is needed but now it works.
If someone can propose a better solution I'm hearing
<?php
while($row_serie_cont = mysqli_fetch_assoc($result_q_serie_cont)){ ?>
<li class="no-padding">
<ul class="collapsible collapsible-accordion">
<li>
<a class="collapsible-header collapsible-header waves-effect waves-teal bold">Season <?php echo $row_serie_cont['serie_number']?>
</a>
<div class="collapsible-body">
<ul>
<?php
$result_q_serie = mysqli_query($conn, $query_serie);
while($row_serie = mysqli_fetch_assoc($result_q_serie)){
if ($row_serie['serie_number'] == $row_serie_cont['serie_number']){
echo "<li>
".$row_serie['episode_number']."</li>
"; } } echo "
</ul>
"; echo "
</div>
</li>
</ul>
</li>
"; } ?>
</ul>
seems that use mysqli_data_seek($result, 0); do the trick the right way.

Query for display catergories from database php mysql

I'm new to coding with php and using MySQL. I am having trouble to display a list of categories by their ID so that each category is displayed individually as a heading. Instead I got it to display a category name but its only echoing out a category name twice that's the same. Here is my code...
$sql= "SELECT * FROM categories ";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query))
{
$id=$row['id'];
$cat_name=$row['cat_name'];
}
?>
<ul class="nav nav-list">
<li class="nav-header"><?php echo $cat_name;?></li>
<li class="nav-header"><?php echo $cat_name;?></li>
</ul>
Your li tag is outside the while loop. So the $id and $cat_name is only the last record in the DB, then you echo them twice. That's way you got the same name twice.
Try echo the li tag in the loop (but not the ul):
<ul class="nav nav-list">
<?php
while($row = mysql_fetch_array($query))
{
$id=$row['id'];
$cat_name=$row['cat_name'];
echo '<li class="nav-header">' .$cat_name. '</li>';
}
?>
</ul>
You can use this code as-is :
$sql= "SELECT * FROM categories ";
$query = mysql_query($sql); ?>
<ul class="nav nav-list">
<?php while($row = mysql_fetch_array($query)) : ?>
<li class="nav-header"><?php echo $row['cat_name'];?></li>
<?php endwhile; ?>
</ul>
This will loop through your records and for each record, it will print an entire li with the required data.
Note that separating your PHP code from your HTML code like this has several benefits. It will be better colored in your editor and it is also easier to integrate.
The reason you are printing the same value out twice is because $cat_name is a string variable and will only hold one value at a time you may want to save the items an array and loop at a seperate time, like such
<?php
$sql= "SELECT * FROM categories ";
$query = mysql_query($sql);
$category = array();
while($row = mysql_fetch_array($query))
{
$array = array(
id => $row['id'],
name => $row['cat_name']
);
array_push($category,$array);
}
?>
<ul class="nav nav-list">
<?php
foreach($category as $c)
{
echo '<li class="nav-header">'.$c['name'].'</li>';
};
?>
</ul>
$sql= "SELECT * FROM categories ";
$query = mysql_query($sql);
while($row = mysql_fetch_assoc($query))
{
?>
<ul class="nav nav-list">
<li class="nav-header"><?php echo $row['id']; ?></li>
<li class="nav-header"><?php echo $row['cat_name']; ?></li>
</ul>
<?php } ?>

Categories