Display <ul> from database - php

I'm making a script that i use my database to show the menu of a page. But i ran into a problem...
The script on my website. : Click here
Problem
So i have this code
UPDATED CODE AND ADDED LINK
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>Heroic Features - Start Bootstrap Template</title>
<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="css/heroic-features.css" rel="stylesheet">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Start Bootstrap</a>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<?php
$db = new mysqli('xxxxxxxxxxx');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
if($_GET['action'] == '')
{
$sql = "SELECT * FROM li_psp";
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
while($row = $result->fetch_assoc()){
echo " <li>
<a href='{$row['li_href']}'>{$row['li_label']}</a>
</li>";
}
}
?>
<?php
$db = new mysqli('xxxxxxxxxxx');
if($db->connect_errno > 0){
die('Unable to connect to database [' . $db->connect_error . ']');
}
if($_GET['action'] == '')
{
$sql = "SELECT * FROM ul_psp";
error_reporting(E_ALL);
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
while($row = $result->fetch_assoc()){
echo "<li class='dropdown'>
<a href='#' class='dropdown-toggle' data-toggle='dropdown'>{$row['ul_name']} <b class='caret'></b></a>
<ul class='dropdown-menu'>";
if($_GET['action'] == '')
{
$sql = "SELECT * FROM ul_data_psp WHERE belong_to = {$row['ul_name']}";
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
}
while($row = $result->fetch_assoc()){
echo " <li>
<a href='{$row['ul_data_href']}'>{$row['ul_data_label']}</a>
</li>";
echo "</ul>
</li>";
}
}
}
}
?>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
But for some reason it does not work. I get this error: Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting identifier (T_STRING) or variable (T_VARIABLE) or number (T_NUM_STRING) in /home/u799303375/public_html/beta/include/menu.php on line 98
What have i tried?
I tried changing all the " to ' but it didn't work, i need help fixing this code, if you have any other things i can do better please tell it! Thanks!

You have malformed quotationmarks. Put variables not directly into strings, concat them with a .:
echo '<li><a href=' . $row['ul_data_href'] . '>' . $row['ul_data_label'] . '</a></li>'

Related

Trouble with pulling data from a dropdown list and checkboxes to be displayed on a table in another page [duplicate]

This question already has answers here:
PHP parse/syntax errors; and how to solve them
(20 answers)
Closed 2 years ago.
When I remove the name="prod_info" from the option tag in the first document, I this error: Warning: Undefined array key "prod_info" in C:\xampp\htdocs\ordersummary.php on line 51, which I know is because there is not anything that is defined as prod_info. However, when I add the name="prod_info" line back in to the option tag in the first document, I get this error: Parse error: syntax error, unexpected identifier "prod_info", expecting "," or ";" in C:\xampp\htdocs\products.php on line 64. I've tried using separate echo statements to solve the problem of expecting the , or ; symbols, but I get the same error. I've also tried placing the name="prod_info line in the option tag outside of the php section, but I still just get the Warning: Undefined array key "prod_info" in C:\xampp\htdocs\ordersummary.php on line 51 error. I'm confused as to how to get the second page to recognize where to pull the data from without using the POST technique. Thank you in advance for any help you are able to provide! The code for the two pages mentioned is listed below:
//The page the info is to be pulled from:
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<link href="novusreset.css" rel="stylesheet" />
<link href="novusstyles1.css" rel="stylesheet" />
<link href="novusstyles2.css" rel="stylesheet" />
<link href="novusflex.css" rel="stylesheet" />
<link href="novusnavicon.css" rel="stylesheet" />
<link href="novustables.css" rel="stylesheet" />
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<header>
<img src="novuslogo.png" alt="Novus LLC." />
<nav class="horizontal">
<a id="navicon" href="#"><img src="novusnavicon.png" alt="" /></a>
<ul id="navigation">
<li>Home</li>
<li>Products
<ul id="productlist">
<li>PC Parts</li>
<li>Peripherals</li>
<li>Networking</li>
<li>Drinks</li>
<li>Apparel</li>
</ul>
<li>About</li>
<li>Support</li>
<li>Account</li>
<li>Cart</li>
</ul>
</nav>
</header>
<section id="main">
<article id="overview">
<form action="ordersummary.php" method="post">
<label for="products"><h1>Choose a product</h1></label>
<select name="products" id="products">
<option name="orderinfo">
<?php
include('connection.php');
$query = "SELECT * FROM products";
$result = mysqli_query( $conn, $query );
if(mysqli_num_rows($result) > 0) {
while( $row = mysqli_fetch_assoc($result) ){
echo "<option name="prod_info">" . $row["prod_name"] . " $" . $row["prod_price"] . "</option>";
}
} else{
echo "Ahhh yes, the big empty.";
}
mysqli_close( $conn );
?>
</br>
</br>
</option>
</select>
<label class="container">Gift Wrapping
<input type="checkbox">
<span class="checkmark"></span>
</label>
</br>
<label class="container">Gift Tagging
<input type="checkbox">
<span class="checkmark"></span>
</label>
</br>
</br>
<input type="submit" value="Place Order">
</form>
</article>
</section>
<footer>
©2021-Novus LLC. • 201 Main St. • Galena Il
</footer>
</body>
</html>
//The page that displays the info pulled from the above page in table form for the customer.
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
<link href="novusreset.css" rel="stylesheet" />
<link href="novusstyles1.css" rel="stylesheet" />
<link href="novusstyles2.css" rel="stylesheet" />
<link href="novusflex.css" rel="stylesheet" />
<link href="novusnavicon.css" rel="stylesheet" />
<link href="novustables.css" rel="stylesheet" />
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<header>
<img src="novuslogo.png" alt="Novus LLC." />
<nav class="horizontal">
<a id="navicon" href="#"><img src="novusnavicon.png" alt="" /></a>
<ul id="navigation">
<li>Home</li>
<li>Products
<ul id="productlist">
<li>PC Parts</li>
<li>Peripherals</li>
<li>Networking</li>
<li>Drinks</li>
<li>Apparel</li>
</ul>
<li>About</li>
<li>Support</li>
<li>Account</li>
<li>Cart</li>
</ul>
</nav>
</header>
<section id="main">
<article id="overview">
<h1>Order Summary</h1></br>
<table id="customers">
<?php
$prod_info = $_POST['prod_info'];
?>
<tr>
<th>Order Details:</th>
</tr>
<tr>
<td><?php echo prod_info;?></br></td>
</tr>
</table>
</article>
</section>
<footer>
©2021-Novus LLC. • 201 Main St. • Galena Il
</footer>
</body>
</html>
Let's take a look at this error: "Parse error: syntax error, unexpected identifier "prod_info", expecting "," or ";" in". The offending line is:
echo "<option name="prod_info">" . $row["prod_name"] . " $" . $row["prod_price"] . "</option>";
The issue here is that you are using double quotes nested inside another set of double quotes. Let's just look at the part before the first period [.].
The first thing happening here is that a string is defined and there is no syntax error:
"<option>"
Next, an attribute is added to the html <option> tag:
"<option name="prod_info">"
There is a set of double quotes inside another set of double quotes which introduces a syntax error. I would suggest using single quotes for the outside and double quotes for the inside. The following code should fix your error:
echo '<option name="prod_info">' . $row["prod_name"] . " $" . $row["prod_price"] . "</option>";
Fixing this issue should also fix your undefined index issue however, I should add that it is always best to check if an array key exists before calling it. Instead of:
$prod_info = $_POST['prod_info'];
You might want to do something like:
$prod_info = isset($_POST['prod_info']) ? $_POST['prod_info'] : '';
or, if you are using php 7.4+:
$prod_info = $_POST['prod_info'] ?? '';

Why are the tabs squeezed together and does not change colour?

I am trying to create a dynamic tab with PHP and MySQL using the Bootstrap Framework.
I am wondering why the dynamic tab is squeezed together. In addition, when I click the tab, it does not change color to show that I clicked that tab.
Actual Result:
The tabs 'Now Showing' and 'Coming Soon' are squeezed together. The selected tab does not change color even when it is clicked.
Expected Result:
The tabs 'Now Showing' and 'Coming Soon' should not be squeezed together. When either of the tab is clicked, that particular tab should change color.
How do I solve this problem such that the tabs will not be squeezed together AND the tabs will change color when it is clicked (to differentiate it from the tabs that are not clicked ---> as shown in the Expected Result)
Here are my codes:
<?php
$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'movie';
$conn = new mysqli($servername, $username, $password, $dbname);
$tab_query = "SELECT * FROM category ORDER BY Category_ID";
$tab_result = mysqli_query($conn, $tab_query);
$tab_menu = '';
$count = 0;
while ($row = mysqli_fetch_array($tab_result)) {
if ($count == 0) {
$tab_menu .= '<li class="active">' . $row["Category_Name"] . '</li>';
} else {
$tab_menu .= '<li>' . $row["Category_Name"] . '</li>';
}
$count++;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Testing Movie Website</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="HandheldFriendly" content="true">
<!--Bootstrap CSS-->
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity=
"sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<!--Custom CSS-->
<link rel="stylesheet" href="css/main.css">
<!--jQuery-->
<script defer
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<!--Bootstrap JS-->
<script defer
src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.bundle.min.js"
integrity="sha384-6khuMg9gaYr5AxOqhkVIODVIvm9ynTT5J4V1cfthmT+emCG6yVmEZsRHdxlotUnm"
crossorigin="anonymous"></script>
<script defer src="js/main.js"></script>
</head>
<body>
<?php include "nav.inc.php"; ?>
<div class="container">
<br>
<ul class="nav nav-tabs">
<?php echo $tab_menu; ?>
</ul>
</div>
</body>
</html>
I followed this video: https://www.youtube.com/watch?v=qkmqncXTiLU
The structure and classes for .tabs is:
<ul class="nav nav-tabs">
<li class="nav-item">
<a class="nav-link active" href="#">Active</a>
</li>
</ul>
Looks like you missed some classes.
The active class needs to go to <a> tag and the <li> tag needs a class nav-item. This will make it work. So it should essentially be:
<?php
while ($row = mysqli_fetch_array($tab_result)) {
if ($count == 0) {
$tab_menu .= '<li class="nav-item"><a class="nav-link active" href="#' . $row["Category_ID"] . '" data-toggle="tab">' . $row["Category_Name"] . '</a></li>';
} else {
$tab_menu .= '<li class="nav-item"><a class="nav-link" href="#' . $row["Category_ID"] . '" data-toggle="tab">' . $row["Category_Name"] . '</a></li>';
}
$count++;
}

The dynamic tabs are presenting the same records

I am trying to create a dynamic tab with PHP and MySQL using Bootstrap Framework for the movie website that I am creating. The movies will be segregated into Now Showing and Coming Soon.
Expected Result: The movies should be displayed based on the category (Now Showing or Coming Soon).
Actual Result: The dynamic tab is not working. Movies that are under “Coming Soon” are displayed in “Now Showing”. In addition, the website only displays 2nd record onwards (from the database). The first record for “Now Showing” which is theand “Coming Soon” are not displayed.
The same content is displayed regardless of clicking “Now Showing” or “Coming Soon” tab (The “Coming Soon” tab can only be seen if I hover which is another issue that I need to fix). The movie 'The cursed lesson' should be shown in Coming Soon. The first record for 'Now Showing' and 'Coming Soon' are not shown.
I truly appreciate your help and advice in fixing these problems. I have been trying to solve these for days but I just can't seem to figure it out.
Here are my codes:
<?php
$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'movie';
$conn = new mysqli($servername, $username, $password, $dbname);
$tab_query = "SELECT * FROM category ORDER BY Category_ID";
$tab_result = mysqli_query($conn, $tab_query);
$tab_menu = '';
$tab_content = '';
$count = 0;
while ($row = mysqli_fetch_array($tab_result)) {
if ($count == 0) {
$tab_menu .= '<li class="nav-item"><a class="nav-link active" href="#' . $row["Category_ID"] . '" data-toggle="tab">' . $row["Category_Name"] . '</a></li>';
$tab_content .= '<div id="'.$row["Category_ID"].'" class="tab-pane fade in active"';
} else {
$tab_menu .= '<li class="nav-item"><a class="nav-link" href="#' . $row["Category_ID"] . '" data-toggle="tab">' . $row["Category_Name"] . '</a></li>';
$tab_content .= '<div id="'.$row["Category_ID"].'" class="tab-pane fade"';
}
$product_query = "SELECT * FROM movie WHERE Category_ID = '".$row["Category_ID"]."'";
$product_result = mysqli_query($conn, $product_query);
while($sub_row = mysqli_fetch_array($product_result))
{
$tab_content .= '
<div class="col-md-3" style="margin-bottom:36px;">
<img src="'.$sub_row["Image_URL"].'" class="img-responsive img-thumbnail" />
<h4>'.$sub_row["Title"].'</h4>
</div>
';
}
$tab_content .= '<div style="clear:both"></div></div>';
$count++;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Movie Testing Website</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="HandheldFriendly" content="true">
<!--Bootstrap CSS-->
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity=
"sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<!--Custom CSS-->
<link rel="stylesheet" href="css/main.css">
<!--jQuery-->
<script defer
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<!--Bootstrap JS-->
<script defer
src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.bundle.min.js"
integrity="sha384-6khuMg9gaYr5AxOqhkVIODVIvm9ynTT5J4V1cfthmT+emCG6yVmEZsRHdxlotUnm"
crossorigin="anonymous"></script>
<script defer src="js/main.js"></script>
</head>
<body>
<div class="container">
<ul class="nav nav-tabs">
<?php echo $tab_menu; ?>
</ul>
<div class="tab-content">
<?php echo $tab_content; ?>
</div>
</div>
</body>
</html>
Here are snippets of my database:
Movie Table:
Category Table:
I was found the problem in the view and not in data collection from db.
I modified the html structure based on this sample: https://bootsnipp.com/snippets/exE6D
Find out more about Bootstrap tab navigations: https://www.w3schools.com/bootstrap4/bootstrap_navs.asp
<?php
$servername = 'localhost';
$username = 'root';
$password = '';
$dbname = 'movie';
$conn = new mysqli($servername, $username, $password, $dbname);
$tab_query = "SELECT * FROM category ORDER BY Category_ID";
$tab_result = mysqli_query($conn, $tab_query);
$tab_menu = '';
$tab_content = '';
$count = 0;
while ($row = mysqli_fetch_array($tab_result)) {
if ($count == 0) {
$tab_menu .= '<a class="nav-item nav-link active" id="' . $row["Category_ID"] . '" data-toggle="tab" href="#nav-' . $row["Category_ID"] . '" role="tab" aria-controls="nav-' . $row["Category_ID"] . '" aria-selected="true">' . $row["Category_Name"] . '</a>';
$tab_content .= '<div class="tab-pane fade show active" id="nav-' . $row["Category_ID"] . '" role="tabpanel" aria-labelledby="nav-' . $row["Category_ID"] . '-tab">';
} else {
$tab_menu .= '<a class="nav-item nav-link" id="' . $row["Category_ID"] . '" data-toggle="tab" href="#nav-' . $row["Category_ID"] . '" role="tab" aria-controls="nav-' . $row["Category_ID"] . '" aria-selected="false">' . $row["Category_Name"] . '</a>';
$tab_content .= '<div class="tab-pane fade show" id="nav-' . $row["Category_ID"] . '" role="tabpanel" aria-labelledby="nav-' . $row["Category_ID"] . '-tab">';
}
$product_query = "SELECT * FROM movie WHERE Category_ID = '".$row["Category_ID"]."'";
$product_result = mysqli_query($conn, $product_query);
while($sub_row = mysqli_fetch_array($product_result))
{
$tab_content .= '
<div class="col-md-3" style="margin-bottom:36px;">
<img src="'.$sub_row["Image_URL"].'" class="img-responsive img-thumbnail" />
<h4>'.$sub_row["Title"].'</h4>
</div>';
}
$tab_content .= '<div style="clear:both"></div></div>';
$count++;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Movie Testing Website</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, shrink-to-fit=no">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="HandheldFriendly" content="true">
<!--Bootstrap CSS-->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh"crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<!--Custom CSS-->
<link rel="stylesheet" href="css/main.css">
<!--jQuery-->
<script defer
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<!--Bootstrap JS-->
<script defer src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.bundle.min.js"
integrity="sha384-6khuMg9gaYr5AxOqhkVIODVIvm9ynTT5J4V1cfthmT+emCG6yVmEZsRHdxlotUnm"
crossorigin="anonymous"></script>
<script defer src="js/main.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-xs-12 ">
<nav>
<div class="nav nav-tabs nav-fill" id="nav-tab" role="tablist">
<?php echo $tab_menu; ?>
</div>
</nav>
<div class="tab-content py-3 px-3 px-sm-0" id="nav-tabContent">
<?php echo $tab_content; ?>
</div>
</div>
</div>
</div>
</body>
</html>

Issue with foreach loop updating every record, rather than the ones just selected in a checkbox

I have a page with a dropdown box at the top populated from the database, when I select an item from the dropdown it gives a list of results with a checkbox at the end of each line, I want to be able to select, using each checkbox, any number of results, and submit a value back into a field in the database for each result selected.
I sort of have this working, but it submits the value to every field in the database, rather than just the ones selected
<?php
require_once("models/config.php");
if (!securePage($_SERVER['PHP_SELF'])){die();}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="shortcut icon" href="favicon.ico">
<title>Stock Items</title>
<!-- Bootstrap CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- bootstrap theme -->
<link href="css/bootstrap-theme.css" rel="stylesheet">
<!--external css-->
<!-- font icon -->
<link href="css/elegant-icons-style.css" rel="stylesheet" />
<link href="css/font-awesome.min.css" rel="stylesheet" />
<!-- full calendar css-->
<link href="assets/fullcalendar/fullcalendar/bootstrap-fullcalendar.css" rel="stylesheet" />
<link href="assets/fullcalendar/fullcalendar/fullcalendar.css" rel="stylesheet" />
<!-- easy pie chart-->
<link href="assets/jquery-easy-pie-chart/jquery.easy-pie-chart.css" rel="stylesheet" type="text/css" media="screen"/>
<!-- owl carousel -->
<link rel="stylesheet" href="css/owl.carousel.css" type="text/css">
<link href="css/jquery-jvectormap-1.2.2.css" rel="stylesheet">
<!-- Custom styles -->
<link rel="stylesheet" href="css/fullcalendar.css">
<link href="css/widgets.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<link href="css/style-responsive.css" rel="stylesheet" />
<link href="css/xcharts.min.css" rel=" stylesheet">
<link href="css/jquery-ui-1.10.4.min.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js IE8 support of HTML5 -->
<!--[if lt IE 9]>
<script src="js/html5shiv.js"></script>
<script src="js/respond.min.js"></script>
<script src="js/lte-ie7.js"></script>
<![endif]-->
</head>
<body>
<!-- container section start -->
<section id="container" class="">
<?php include("navigation.php"); ?>
<!--main content start-->
<section id="main-content">
<section class="wrapper">
<!--overview start-->
<div class="row">
<div class="col-lg-12">
<h3 class="page-header"><i class="fa fa-lightbulb-o"> </i>Stock</h3>
<ol class="breadcrumb">
<li><i class="fa fa-home"></i>Home</li>
<li><i class="fa fa-lightbulb-o"></i>Stock</li>
</ol>
</div>
</div>
<div class="row">
<div class="col-lg-12">
<section class="panel">
<header class="panel-heading">
All Stock
</header>
<div class="panel-body">
<form class="form-horizontal" method="post" action="">
<div class="form-group">
<div class="col-lg-8">
<select name="search" class="form-control" required >
<?php
// connect to the database
require_once('models/db-settings.php');
$conn = mysql_connect($db_host, $db_user, $db_pass, $db_name) or die("Error " .mysql_error($conn));
mysql_select_db($db_name);
$query = "SELECT `id`, `description` FROM `stock_templates`";
$stock_templates = mysql_query($query);
echo "<option value=''>Select Stock Template</option>";
while ($description=mysql_fetch_assoc($stock_templates)) {
echo "<option value='" . $description['id'] . "'>" . $description['description'] . "</option>";
}
?>
</select>
</div>
<div class="col-lg-1">
<input type="submit" name="filter" value="Search" class="btn btn-success" />
</div>
</div>
</form><br><br>
<?php
if(isset($_POST['formSubmit']))
{
$aDoor = $_POST['check_list'];
if(empty($aDoor))
{
echo("<p>You didn't select any items to add to lease.</p>\n");
}
else
{
$N = count($aDoor);
echo("<p>You selected $N item(s) to add to lease: ");
for($i=0; $i < $N; $i++)
{
echo($aDoor[$i] . " ");
}
echo("</p>");
}
}
function IsChecked($chkname,$value)
{
if(!empty($_POST[$chkname]))
{
foreach($_POST[$chkname] as $chkval)
{
if($chkval == $value)
{
return true;
}
}
}
return false;
}
?>
<?php
ob_start( );
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
$query = mysql_query("UPDATE stock SET lease_id = $lease_id");
$result2 = mysql_query($query);
// check if sent
if ($result2) {
?>
<div class="alert alert-success fade in">
<button data-dismiss="alert" class="close close-sm" type="button">
<i class="icon-remove"></i>
</button>
<strong>Well done!</strong> Your lease items have been successfully saved.
</div>
<?php
} else {
?>
<div class="alert alert-block alert-danger fade in">
<button data-dismiss="alert" class="close close-sm" type="button">
<i class="icon-remove"></i>
</button>
<strong>Oh snap!</strong> We could not save your lease items.
</div>
<?php
}
}
}
?>
<?php
// connect to the database
require_once('models/db-settings.php');
$conn = mysql_connect($db_host, $db_user, $db_pass, $db_name) or die("Error " .mysql_error($conn));
mysql_select_db($db_name);
// Extract filter information
$count = 0;
$search = mysql_escape_string(#$_POST['search']);
// select data from the database
$query2 = "SELECT * FROM `stock_templates` ORDER BY `stock_templates`.`id` DESC LIMIT 0";
// Perform Logic
if (array_key_exists("filter", $_POST)) {
// query based on search term
$query2 = "SELECT * FROM `stock` WHERE $search=stocktemplate_id AND lease_id=0";
}
$result2 = mysql_query($query2);
// Result
if (mysql_num_rows($result2) < 1) {
echo "<div align='center'><h2>Please select a stock template above and click search</h2></div>";
}
?>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" class="form-horizontal" name="check_list[]">
<div class="form-group">
<label class="control-label col-lg-1" for="lease_id">Lease Number *</label>
<div class="col-lg-10">
<select name="lease_id" class="form-control">
<?php
// connect to the database
require_once('models/db-settings.php');
$conn = mysql_connect($db_host, $db_user, $db_pass, $db_name) or die("Error " .mysql_error($conn));
mysql_select_db($db_name);
$query = "SELECT `id`, `leasenumber` FROM `lease`";
$leases = mysql_query($query);
echo "<option value=''>Select Lease..........</option>";
while ($lease=mysql_fetch_assoc($leases)) {
echo "<option value='" . $lease['id'] . "'>" . $lease['leasenumber'] . "</option>";
}
?>
</select>
</div>
</div>
<table class="table table-hover">
<thead>
<tr>
<th>Item ID</th>
<th>Description</th>
<th>Barcode</th>
<th>Serial</th>
<th>Add To Lease</th>
</tr>
</thead>
<?php
while ($row = mysql_fetch_array($result2))
{
$id = $row["id"];
$lease_id = $row["lease_id"];
$barcode = $row["barcode"];
$serial = $row["serial"];
$stocktemplate_id = $row["stocktemplate_id"];
$qa = 0;
?>
<tbody>
<tr>
<td><?php print $id ?></td>
<td><?php $q = mysql_query("SELECT description FROM stock_templates WHERE id = '$stocktemplate_id'"); while ($row = mysql_fetch_array($q)){$qa = $row["description"];} print $qa ?></td>
<td><?php print $barcode ?></td>
<td><?php print $serial ?></td>
<td><input type="checkbox" name="check_list[]" value="<?php print $id ?>" /></td>
<td></td>
</td>
</tr>
</tbody>
<?php
}
?>
</table>
<input class="btn btn-danger" type="submit" name="formSubmit" value="Add Selected To Lease" />
</div>
</form>
</section>
</div>
</div>
</section>
</section>
<!--main content end-->
</section>
<!-- container section end -->
<!-- javascripts -->
<script src="js/jquery.js"></script>
<script src="js/bootstrap.min.js"></script>
<!-- nice scroll -->
<script src="js/jquery.scrollTo.min.js"></script>
<script src="js/jquery.nicescroll.js" type="text/javascript"></script>
<!-- jquery ui -->
<script src="js/jquery-ui-1.9.2.custom.min.js"></script>
<!--custom checkbox & radio-->
<script type="text/javascript" src="js/ga.js"></script>
<!--custom switch-->
<script src="js/bootstrap-switch.js"></script>
<!--custom tagsinput-->
<script src="js/jquery.tagsinput.js"></script>
<!-- colorpicker -->
<!-- bootstrap-wysiwyg -->
<script src="js/jquery.hotkeys.js"></script>
<script src="js/bootstrap-wysiwyg.js"></script>
<script src="js/bootstrap-wysiwyg-custom.js"></script>
<!-- ck editor -->
<script type="text/javascript" src="assets/ckeditor/ckeditor.js"> </script>
<!-- custom form component script for this page-->
<script src="js/form-component.js"></script>
<!-- custome script for all page -->
<script src="js/scripts.js"></script>
</body>
</html>
No worries. Just put the code of insert query inside foreach() loop. Like this:
foreach($_POST['check_list'] as $item)
{
$sql="INSERT/UPDATE Query";
//for example
$sql = "INSERT INTO table_demo (field_1, TARGET_FIELD, field_2, field_3) VALUES (val_1, $item, val_2, val_3)";
$insert = mysqli_query($connection,$sql);
}
//next code of your choice
That is really Easy to give a go.
See this is same as you want.
In this link the first answer by Sean Valsh is your solution.
Giving the array as name of every checkbox will give you only chacked option's id in array while submitted.
https://stackoverflow.com/a/4997271/6834980
Ask if still have the problem. Happy to help.

Php code not displaying the data right from database

I'm working on a movie database project and I got my database the way it needs to be, but having issues with the front-end part of it. I finally got it to show all the information and have it be clickable, but I'm pretty sure this isn't the way to do it.
I'm having trouble displaying the genres. I either can groupconcat and display all of them like that, but then I can't have them be links or I could do them individual and have the links, but not get them all. I finally got them all, but then when I try it on the phone or tablet I only see the last genre and not the others. So I did another database and pull request to get all the data over again, but I'm pretty sure that isn't right.
<?php
require '../database.php';
$Code = null;
if ( !empty($_GET['Code'])) {
$Code = $_REQUEST['Code'];
}
if ( null==$Code ) {
header("Location: index.php");
} else {
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT Movies.Code, Title, Plot, Movies.Type, Movies.Category, Image, Score, Rated, Alt, Status, YearReleased, Duration, SUBSTRING(Duration, 1, CHAR_LENGTH(Duration) - 3) AS Duration2, TotalEps, Types.code as tcode, Types.Type as ttype, Categories.Code as ccode, Categories.Category as ca, Ratings.Code as rc, Ratings.Rating as rr, Genre, GenreCode FROM Movies, Types, Categories, Ratings, MovieGenres, Genres WHERE Movies.Type=Types.Code AND Movies.Rated=Ratings.Code AND Movies.Category=Categories.Code AND Movies.Code=MovieGenres.MovieCode AND MovieGenres.GenreCode=Genres.Code AND Movies.Code = ?";
$q = $pdo->prepare($sql);
$q->execute(array($Code));
$data = $q->fetch(PDO::FETCH_ASSOC);
Database::disconnect();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="description" content="">
<meta name="author" content="">
<title>MovieDB - <?php echo $data['Title'];?></title>
<!-- Bootstrap Core CSS -->
<link href="../css/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="../css/modern-business.css" rel="stylesheet">
<link href="../css/custom.css" rel="stylesheet">
<!-- Custom Fonts -->
<link href="../font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<!-- Navigation -->
<?php include '../include/nav.php';?>
<!-- Page Content -->
<div class="container">
<!-- Page Heading/Breadcrumbs -->
<div class="row">
<div class="col-lg-12">
<h2 class="page-header"><?php echo $data['Title'];?>
</h2>
</div>
</div>
<!-- /.row -->
<!-- Portfolio Item Row -->
<div class="row">
<div class="col-md-4">
<img class="img-responsive" src="../Images/<?php echo $data['Image'];?>" alt="">
</div>
<div class="col-md-8 hidden-sm hidden-xs">
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" href="#Plot">Plot</a></li>
<li><a data-toggle="tab" href="#Details">Details</a></li>
</ul>
<div class="tab-content">
<div id="Plot" class="tab-pane fade in active">
<p><?php echo $data['Plot'];?></p>
</div>
<div id="Details" class="tab-pane fade">
<?php
echo'<li>Alternative Title: '. $data['Alt'] .'</li>';
echo'<li>Type: '. $data['ttype'] .'</li>';
echo'<li>Rated: '. $data['rr'] .'</li>';
echo'<li>Episodes: '. $data['TotalEps'] .'</li>';
echo'<li>Duration: '. $data['Duration2'] .'</li>';
echo'<li>Genre: ';
echo''. $data['Genre'] .'';
foreach($q as $data){
echo", ";
echo''. $data['Genre'] .'';
}
echo '</li>';
echo'<li>Status: '. $data['Status'] .'</li>';
echo'<li>Category: '. $data['ca'] .'</li>';
echo'<li>Year Released: '. $data['YearReleased'] .'</li>';
echo'<li>Score: '. $data['Score'] .'</li>';
?>
</div>
</div>
</div>
<div class="col-md-4 hidden-md hidden=-lg">
<h3>Plot</h3>
<p><?php echo $data['Plot'];?></p>
</div>
<div class="col-md-4 hidden-md hidden=-lg ">
<h3>Details</h3>
<ul>
<?php
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "SELECT Movies.Code, Title, Plot, Movies.Type, Movies.Category, Image, Score, Rated, Alt, Status, YearReleased, Duration, SUBSTRING(Duration, 1, CHAR_LENGTH(Duration) - 3) AS Duration2, TotalEps, Types.code as tcode, Types.Type as ttype, Categories.Code as ccode, Categories.Category as ca, Ratings.Code as rc, Ratings.Rating as rr, Genre, GenreCode FROM Movies, Types, Categories, Ratings, MovieGenres, Genres WHERE Movies.Type=Types.Code AND Movies.Rated=Ratings.Code AND Movies.Category=Categories.Code AND Movies.Code=MovieGenres.MovieCode AND MovieGenres.GenreCode=Genres.Code AND Movies.Code = ?";
$q = $pdo->prepare($sql);
$q->execute(array($Code));
$data = $q->fetch(PDO::FETCH_ASSOC);
Database::disconnect();
echo'<li>Alternative Title: '. $data['Alt'] .'</li>';
echo'<li>Type: '. $data['ttype'] .'</li>';
echo'<li>Rated: '. $data['rr'] .'</li>';
echo'<li>Episodes: '. $data['TotalEps'] .'</li>';
echo'<li>Duration: '. $data['Duration2'] .'</li>';
echo'<li>Genre: ';
echo''. $data['Genre'] .'';
foreach($q as $data){
echo", ";
echo''. $data['Genre'] .'';
}
echo '</li>';
echo'<li>Status: '. $data['Status'] .'</li>';
echo'<li>Category: '. $data['ca'] .'</li>';
echo'<li>Year Released: '. $data['YearReleased'] .'</li>';
echo'<li>Score: '. $data['Score'] .'</li>';
?>
</ul>
</div>
</div>
<!-- /.row -->
<hr>
<!-- Footer -->
<?php include '../include/footer.php';?>
<!-- /.container -->
<!-- jQuery -->
<script src="../js/jquery.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="../js/bootstrap.min.js"></script>
</body>
</html>
formated:
SELECT Movies.Code, Title, Plot, Movies.Type, Movies.Category, Image, Score, Rated, Alt, Status, YearReleased, Duration, SUBSTRING(Duration, 1, CHAR_LENGTH(Duration) - 3) AS Duration2, TotalEps, Types.code as tcode, Types.Type as ttype, Categories.Code as ccode, Categories.Category as ca, Ratings.Code as rc, Ratings.Rating as rr, Genre, GenreCode
FROM Movies, Types, Categories, Ratings, MovieGenres, Genres
WHERE Movies.Type=Types.Code
AND Movies.Rated=Ratings.Code
AND Movies.Category=Categories.Code
AND Movies.Code=MovieGenres.MovieCode
AND MovieGenres.GenreCode=Genres.Code
AND Movies.Code = ?;
SELECT Genre, GenreCode
FROM MovieGenres, Genres
WHERE MovieGenres.GenreCode=Genres.Code
AND MovieCode = ?;
You aren't looping over the rows returned by the query.
Have a look at this answer https://stackoverflow.com/a/160365/6632744
You'll want something like this:
while ($row = $q->fetch(PDO::FETCH_ASSOC)) {
echo''. $row['Genre'] .'';
}
And you'll want to strip away your initial fetch.
$data = $q->fetch(PDO::FETCH_ASSOC); // remove this line

Categories