Use database information as dropdownlist option - php

I'm making a website for school about MLB with a database. But now I want information in the database as an option in a dropdownlist. This is my HTML Code:
<!DOCTYPE html>
<html>
<head>
<link rel="icon" type="image/png" href="./images/favicon-32x32.png"
sizes="32x32" />
<link rel="icon" type="image/png" href="./images/favicon-16x16.png"
sizes="16x16" />
<title>MLB: Major League Baseball</title>
<link href="css folder/MLBstylesheet.css" rel="stylesheet"
type="text/css"/>
</head>
<body>
<div id="container">
<div id="titel">
<img class="MLBTitel" src="./images/MLBtitel.jpg" alt="MLBTitel" >
<div id="titeltekst">
MAJOR LEAGUE BASEBALL
<br>
</div>
<nav>
<ul>
<li><a class= "menu" href="index.html">Home</a></li>
<li><a class= "menu" href="spelers.php">Spelers</a></li>
<li><a id = "active" class= "menu" href="teams.php">Teams</a></li>
<li><a class= "menu" href="wedstrijden.html">Wedstrijden</a></li>
<li><a class= "menu" href="contact.html">Contact</a></li>
</ul>
</nav>
<br><br>
</div>
<div id="teamtabel">
<?php
$servername = "localhost";
$username = "id1419279_root";
$password = "*******";
$dbname = "id1419279_mlb";
// Create connection
$conn = new mysqli($localhost, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM teams";
$result = $conn->query($sql);
echo "<select name='naamteam'>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['teamnaam'] ."'>" . $row['teamnaam'] ."
</option>";
}
echo "</select>";
?>
</div>
</div>
</body>
</html>
The options would be the teamnames, but it seems like my dropdownlist is still empty: picture of the dropdownlist on the website
This is what my database looks like:
picture of my database
I hope someone could help me and I would really appreciate it.

<!DOCTYPE html>
<html>
<head>
<link rel="icon" type="image/png" href="./images/favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="./images/favicon-16x16.png" sizes="16x16" />
<title>MLB: Major League Baseball</title>
<link href="css folder/MLBstylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="container">
<div id="titel">
<img class="MLBTitel" src="./images/MLBtitel.jpg" alt="MLBTitel" >
<div id="titeltekst">
MAJOR LEAGUE BASEBALL
<br>
</div>
<nav>
<ul>
<li><a class= "menu" href="index.html">Home</a></li>
<li><a class= "menu" href="spelers.php">Spelers</a></li>
<li><a id = "active" class= "menu" href="teams.php">Teams</a></li>
<li><a class= "menu" href="wedstrijden.html">Wedstrijden</a></li>
<li><a class= "menu" href="contact.html">Contact</a></li>
</ul>
</nav>
<br><br>
</div>
<div id="teamtabel">
<?php
$servername = "localhost";
$username = "id1419279_root";
$password = "*******";
$dbname = "id1419279_mlb";
// Create connection
$conn = new mysqli($localhost, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM teams";
$result = $conn->query($sql);
$dropdownlist = '';
while($row = mysqli_fetch_array($result)) {
$teamnaam = $row['teamnaam'];
$dropdownlist .="<option value='" . $teamnaam . "'>" . $teamnaam . "</option>";
}
if(isset($dropdownlist)){
echo "<select name='naamteam'>";
echo $dropdownlist;
echo "</select>";
}
?>
</div>
</div>
</body>
</html>

Related

Filling boostrap cards with data from a mySQL database

I am trying to fill bootstrap cards with data from a database.
I am very close.
I have the mainphp file called project.php
A file with a function called component to fill them, called component.php.
And a connectDB.php file, that connect the DB and extracts the information from a table.
I am aware this is alot of code so I highlighted 2 areas the problem is definitely in...Apologies if too much, I can remove the function if necessary as it works..
here is an extract from project.php
<?php
require_once('connect/connectDB.php');
require_once('component.php');
?>
<!doctype html>
<html lang="en">
<head>
<title>The Wild Music Shop</title>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!--Font awesome-->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.3/css/all.min.css" integrity="sha512-iBBXm8fW90+nuLcSKlbmrPcLa0OT92xO1BIsZ+ywDWZCvqsWgccV3gFoRBv0z+8dLJgyAHIhR35VZc2oM/gI1w==" crossorigin="anonymous" />
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap#4.6.0/dist/css/bootstrap.min.css" integrity="sha384-B0vP5xmATw1+K9KRQjQERJvTumQW0nPEzvF6L/Z6nronJ3oUOFUFpCjEUQouq2+l" crossorigin="anonymous">
<!-- i have no idea why it won't link -->
<!-- <link type="text/css" rel="stylesheet" href="myCSS/somecss.css"> -->
<link rel="stylesheet" href="myCSS/somecss.css">
</head>
<body>
<!-- the cards -->
<div class = "container" id="thecards">
<div class = "row text-center py-5">
//Definite problem here
<?php
$result = getData();
while ($row = mysqli_fetch_assoc($result)){
component($row['InstName'], $row['Price'], $row['ProductImg'], $row['description']);
}
?>
</div>
</div>
</html>
This component definitely works as I tested it
<?php
function component($pInstName, $pPrice, $pProductImg, $pdescription){
$element = "
<div class=\"col-md-3 col-sm-6 my-3 my-md-0\">
<form action=\"index.php\" method=\"post\">
<div class=\"card shadow\">
<div>
<img src=\ $pProductImg\" alt=\"Image1\" class=\"img-fluid card-img-top\">
</div>
<div class=\"card-body\">
<h5 class=\"card-title\">Our $pInstName</h5>
<h6>
<i class=\"fas fa-star\"></i>
<i class=\"fas fa-star\"></i>
<i class=\"fas fa-star\"></i>
<i class=\"fas fa-star\"></i>
<i class=\"far fa-star\"></i>
</h6>
<p class=\"card-text\">
$pdescription
</p>
<h5>
<span class=\"price\">$$pPrice</span>
</h5>
<button type=\"submit\" class=\"btn btn-warning my-3\" name=\"add\">Add to Cart </button>
</div>
</div>
</form>
</div>
";
echo $element;
}
?>
and finally here is the connectDB.php
<?php
//Create a database connection
$dbhost = "localhost";
$dbuser = "root";
$dbpassword = "";
$dbname = "g00398295";
$connection = mysqli_connect($dbhost,$dbuser,$dbpassword,$dbname);
//Test if connection occoured
if(mysqli_connect_errno()){
die("DB connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
if (!$connection)
{
die('Could not connect: ' . mysqli_error());
}
$sql = "select * from Instruments where itemno = 1; ";
$result = mysqli_query($connection,$sql);
$row=mysqli_fetch_assoc($result);
echo "Pls work".$row['InstName'];
mysqli_close($connection);
//get product from Database
//Definite problem here
function getData(){
$sql = "select * from $this->tablename";
$result = mysqli_query($this->con,$sql);
if(mysqli_num_rows($result) > 0){
return $result;
}
}
?>
I am awar the problem is somewhere in the connection step. I am very new to php in general and have been at this very long, I would appreciate help!
You have a function getData() that has "$this->table". Dont copy paste the codes without understanding what it does.
Clean up your connectDB.php:
<?php
//Create a database connection
$dbhost = "localhost";
$dbuser = "root";
$dbpassword = "";
$dbname = "g00398295";
$connection = mysqli_connect($dbhost, $dbuser, $dbpassword, $dbname);
//Test if connection occoured
if(mysqli_connect_errno()){
die("DB connection failed: " .
mysqli_connect_error() .
" (" . mysqli_connect_errno() . ")"
);
}
?>
In project.php:
<div class = "row text-center py-5">
//Definite problem here
<?php
$sql = "select * from Instruments where itemno = 1; ";
$result = mysqli_query($connection,$sql);
while ($row = mysqli_fetch_assoc($result)){
component($row['InstName'], $row['Price'], $row['ProductImg'], $row['description']);
}
?>
</div>

I can't get info pulled from a database to not display end to end on one line in a dropdown list

I'm a student building a PHP project trying to get product names and their respective prices (from a mysql database table) to display one after the other in a dropdown list. The customer will select one of the items, and check one or both of the check boxes before pressing the place order button, at which point their item selection, order total, and chosen gift option will display in table form on a separate page. I've gotten all of the correct info to display in the dropdown list, as well as getting the check boxes to function properly, but all of the product data is on one line, and end to end. I don't get any errors at all when I load the page. I tried it another way in which I defined multiple instances of the echo statement, which formatted correctly, but only listed the first item in the database table repeatedly. These two different approaches are displayed below:
<!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">
<label for="products">Choose a product:</label>
<select name="products" id="products">
<option>
<?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 $row["prod_name"] . " " . "$" . $row["prod_price"];
}
} else{
echo "Ahhh yes, the big empty.";
}
mysqli_close( $conn );
?>
</option></select>
<label class="container">Gift Wrapping
<input type="checkbox">
<span class="checkmark"></span>
</label>
</br>
<label class="container">Gift Tag
<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>
This next bit of code is the second attempt which displays things in the correct formatting I want it to, but it only displays the first item in the database table repeatedly:
<!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">
<?php
include('connection.php');
$query = "SELECT * FROM products";
$result = mysqli_query( $conn, $query );
$row = mysqli_fetch_assoc($result);
?>
<h2>Choose your product and gift options</h2></br> <form action="ordersummary.php">
<label for="products">Choose a product:</label>
<select name="products" id="products">
<option><?php echo $row["prod_name"] . " " . "$" . $row["prod_price"] ?></option>
<option value="$row["prod_name"]"><?php echo $row["prod_name"] . " " . "$" .
$row["prod_price"] ?></option>
<option value="$row["prod_name"]"><?php echo $row["prod_name"] . " " . "$" . $row["prod_price"] ?></option>
<option value="$row["prod_name"]"><?php echo $row["prod_name"] . " " . "$" . $row["prod_price"] ?></option>
</select>
<br><br>
<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="Submit">
</form>
<!--}
} else{
echo "Ahhh yes, the big empty.";
}
mysqli_close( $conn );
?>-->
</article>
</section>
<footer>
©2021-Novus LLC. • 201 Main St. • Galena Il
</footer>
</body>
</html>
Thank you in advance for your help!
Your first attempt has this issue:
All the products texts are output in one <option> element:
<select name="products" id="products">
<option>
<?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 $row["prod_name"] . " " . "$" . $row["prod_price"];
}
} else{
echo "Ahhh yes, the big empty.";
}
mysqli_close( $conn );
?>
</option></select>
I assume you want each product to appear in its own <option> element -- which will mean they also appear on separate lines. So remove the <option> open and close tag from the static part (outside of the PHP snippet), and include them inside the loop, in the echo statement:
<select name="products" id="products">
<?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>" . $row["prod_name"] . " $" . $row["prod_price"] . "</option>";
}
} else{
echo "<option>Ahhh yes, the big empty.</option>";
}
mysqli_close( $conn );
?>
</select>

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 MySQL: boolean Value works on local server but returns empty on live server?

I have a boolean ( bit ) value stored in my database. The problem is when I run the PHP script on local server I get the value either 0 or 1 depending upon the value stored in Database against the id using mySQL query. However the same script on live server returns absolutely nothing neither 0 nor 1. My variable remains empty. Any possible explanation. The value of $status remains empty on live server but works on local
<?php
session_start();
require_once __DIR__ . '/connect.php';
if (!$_SESSION['loginadmin'])
{
header("location:error.php");
}
else
{
?>
<!DOCTYPE html>
<!---->
<html lang="en">
<head>
<title></title>
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400italic,400,300,600,700' rel='stylesheet' type='text/css'>
<link href="bootstrapTheme.css" rel="stylesheet">
<!-- Owl Carousel Assets -->
<link href="owl.carousel.css" rel="stylesheet">
<link rel="stylesheet" href="css/reset.css" type="text/css" media="all">
<link rel="stylesheet" href="css/layout.css" type="text/css" media="all">
<link rel="stylesheet" href="css/style.css" type="text/css" media="all">
<script src="js/Forum_400.font.js"></script>
<script src="js/script.js"></script>
<!--Login Button scripts-->
<script type="application/x-javascript"> addEventListener("load", function() { setTimeout(hideURLbar, 0); }, false); function hideURLbar(){ window.scrollTo(0,1); } </script>
<script src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/megamenu.js"></script>
<script src="js/menu_jquery.js"></script>
<!--**********-->
</head>
<body id="page1">
<div class="body6">
<div class="body1">
<div class="body5">
<div class="main">
<!-- header -->
<header>
<h1><img src="images/logo.png" / ></div></h1>
<nav>
<ul id="top_nav">
<li><img src="images/icon_1.gif" alt="">HOME</li>
<li class="login" >
<div id="loginContainer"><img src="images/icon_2.png" alt=""><span>SIGN OUT</span>
</div>
</li>
<li class="end"><img src="images/icon_3.gif" alt="">CONTACT US</li>
</ul>
</nav>
</header>
<!---->
<div class="contact-bg">
<div class="container">
<div class="contact-us">
<div class="contact_right">
<div class="contact-form">
<h3 class="style">ADMINISTRATOR PANEL</h3>
<form method="post" action="status.php">
<div>
<span><label>Membership ID</label></span>
<span><input name="membership" type="text" class="textbox"></span>
</div>
<div>
<input name="submit" type="submit" value="Check">
</div>
<?php
if(isset($_POST['submit']))
{
$_SESSION['id']=$_POST['membership'];
$membership=$_POST['membership'];
$first_id=0;
if(($membership=="" && $membership==NULL))
{
echo "<script type='text/javascript'>alert('We are sorry ! But you have missed the field');</script>";
}
else
{
$query0 = "LOCK TABLES card WRITE";
$result0 = mysql_query($query0);
$query1 = "LOCK TABLES card as card1 READ";
$result1 = mysql_query($query0);
if( $result0 && $result1 )
{
$query = "SELECT status FROM card WHERE id='$membership'";
$result = mysql_query($query);
$numberofrows = mysql_num_rows ($result);
$query_ = "UNLOCK TABLES";
mysql_query($query_);
if($numberofrows)
{
$row=mysql_fetch_row($result,MYSQL_ASSOC);
$status = $row['status'];
$sql = "SELECT name FROM customers WHERE mem_id='$membership'";
$res = mysql_query($sql);
$numberofrows = mysql_num_rows($res);
if (!$status)
{
?>
<div>
<?php
if ($numberofrows)
{
$row=mysql_fetch_row($res,MYSQL_ASSOC);
$memname = $row['name'];
echo "<span><label>Name: $memname $status</label></span>";
}
else
echo "<span><label>No User Against this Membership ID</label></span>";
?>
<span><label>Current Status: Not Activated</label></span>
</div>
<div>
<input type="submit" name="activate" value="Activate">
</div>
<?php
}
else
{
?>
<div>
<?php if ($numberofrows)
{
$row=mysql_fetch_row($res,MYSQL_ASSOC);
$memname = $row['name'];
echo "<span><label>Name: $memname $status</label></span>";
}
else
echo "<span><label>No User Against this Membership ID</label></span>";
?>
</div>
<div>
<span><label>Current Status: Activated </label></span>
<input type="submit" name="deactivate" value="Deactivate">
</div>
<?php
}
}
else
echo "<script type='text/javascript'>alert('No such membership id');</script>";
$query_ = "UNLOCK TABLES";
mysql_query($query_);
}
else
die ("Database access failed: " . mysql_error());
}
// }
}
if(isset($_POST['activate']))
{
$query="UPDATE card SET status=TRUE WHERE id='$_SESSION[id]'";
$result = mysql_query($query);
if($result)
echo "<script type='text/javascript'>alert('membership id: $_SESSION[id] Status Changed to ACTIVATED');</script>";
}
if(isset($_POST['deactivate']))
{
$query="UPDATE card SET status=FALSE WHERE id='$_SESSION[id]'";
$result = mysql_query($query);
if($result)
echo "<script type='text/javascript'>alert('membership id: $_SESSION[id] Status Changed to Deactivated');</script>";
}
}
?>
</form>
</div>
</div>
<div class="clear"></div>
</div>
</div>
</div>
<!---->
<footer>
<div id="wrapper">
<section class="col1 pad_left1">
<div id="end">
<div id="end_menu">
Home . About . Contact . Terms of Use . Sponsors
</div>
<div id="social">
<ul id="icons">
<li><img src="images/icon1.gif" alt=""></li>
<li><img src="images/icon2.gif" alt=""></li>
<li><img src="images/icon3.gif" alt=""></li>
<li><img src="images/icon4.gif" alt=""></li>
<li><img src="images/icon5.gif" alt=""></li>
</ul>
</div>
<div id="sponsors">
<img src="images/g1.png" width="150px" height="100px"/>
<img src="images/g2.png" width="150px" height="100px"/>
</div>
<div id="subscribe">
<span class="toto">NEWSLETTER</span><br>
<input type="text" id="inputs" placeholder="e-mail" />
<br>
<div id="sub_but">
SUBSCRIBE
</div>
</div>
</div>
</section>
</div>
</footer>
</body>
</html>
I had the status value as BIT on my local server which was working fine on the local server however it was returning empty on live server.
FIX ON LIVE SERVER: Changed the status column type from BIT to TINYINT on live server.

Categories