PHP code (mysqli) queries are not working after specific code - php

I have PHP code we can call this code BLOCKONE:
$maker = "SELECT vidMaker FROM videoinformation";
if ($result = mysqli_query($con, $maker)) {
while ($row = mysqli_fetch_row($result)) {
$values = explode(',',$row[0]);
foreach($values as $v)
if (!empty($v)) {
printf ("<img class=\"makerImg\" src=\"addVid/maker/%s.jpg\">", $v);
}
}
mysqli_free_result($result);
}
else{
echo"PM administrator with an error";
}
I have big PHP code like:
<!DOCTYPE html>
<head>
</head>
<body>
<?php>
.
.
.
.
.
.
.
.
.
.
.
.
BLOCKONE
.
.
.
.
.
.
.
.
.
.
.
?>
</body>
</html>
The question is BEFORE BLOCKONE all the mysqli queries are working, I can display information, images and so on, but AFTER BLOCKONE none of the queries are working.
Why I know that the problem exactly in this code? Because if delete it... full code working fine. And I'm not sure hot to solve it.
This is full code:
<!DOCTYPE html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<?php include 'BSH.php' ?>
<link rel="stylesheet" type="text/css" href="CSS/ldvid.css">
<script type="text/javascript" src="java/FWDUVPlayer.js"></script>
<!-- Setup video player-->
<script type="text/javascript">
<?php include 'video/settings.php'; ?>
</script>
</head>
<body>
<?php include_once 'userPages/check_login_status.php';?>
<?php include_once 'incIndex/headerTop.php'; ?>
<?php include_once 'incIndex/footer.php'; ?>
<?php
// here we get the incomming video id (videoinfo.php?id=123)
$video_id = $_GET['id'];
// build a database query to select the videoInformation
// here we use WHERE to select the video_id we want
$query = 'SELECT newsvid.id, videoinformation.id, videomain.id, newsvid.vidTitle, newsvid.imgCover, videoinformation.vidLD, videoinformation.vidYear, videoinformation.vidCity, videoinformation.vidZanr, videoinformation.vidZanr2,videoinformation.vidZanr3,videoinformation.vidQuality, videoinformation.vidTranslated, videoinformation.vidMaker, videoinformation.vidRoles, videoinformation.vidTime, videoinformation.imgShot1, videoinformation.imgShot2, videoinformation.imgShot3, videomain.vidMUrl FROM newsvid, videomain, videoinformation WHERE newsvid.id = videoinformation.id AND newsvid.id = videomain.id AND newsvid.id = '. $video_id ;
// lets connect and do the query
include 'connect/con.php';
$result = mysqli_query($con, $query);
echo "<div class=\"ldCover\">";
while($row = mysqli_fetch_array($result)) {
echo "<div class=\"panel panel-default\"><div class=\"panel-heading\">";
echo "<div><strong><h3>" . $row['id']. " | " . $row['vidTitle'] . "</h3></strong></div>";
echo "</div><div class=\"panel-body\">";
echo "<div class=\"imgCover\"><img style=\"width:200; height:320px\" class=\"img-thumbnail\"src=\"upload/videoCover/" . $row['imgCover'] . "\"></div>";
echo "<div class=\"vidLD\">
<table>
<tr><td class=\"tdBR\"><strong> Years: </strong></td><td >" . $row['vidYear'] . "</td></tr>
<tr><td class=\"tdBR\"><strong> City: </strong></td><td >". $row['vidCity'] . "</td></tr>
<tr><td class=\"tdBR\"><strong> Zanr: </strong></td><td >". $row['vidZanr'] ." , ". $row['vidZanr2'] ." , ". $row['vidZanr3'] . "</td></tr>
<tr><td class=\"tdBR\"><strong> Quality: </strong></td><td >". $row['vidQuality'] . "</td></tr>
<tr><td class=\"tdBR\"><strong> Translated: </strong></td><td >". $row['vidTranslated'] . "</td></tr>
<tr><td class=\"tdBR\"><strong> Video time: </strong></td><td >". $row['vidTime'] . "</td></tr>
</table>" . $row['vidLD'] . "</div></div>";
echo "<div class=\"panel-body\" style=\"border-top: 1px solid; border-bottom: 1px solid; border-color:#ddd\"><div class=\"vidDesc\">
</div>";
echo "<div class=\"vidMR\">
<div><strong> Maker: </strong>" . $row['vidMaker'] . "</div><div>";
$maker = "SELECT vidMaker FROM videoinformation WHERE id=".$row['id'];
if ($result = mysqli_query($con, $maker)) {
while ($row = mysqli_fetch_row($result)) {
$values = explode(',',$row[0]);
foreach($values as $v)
if (!empty($v)) {
printf ("<img class=\"makerImg img-circle\" src=\"addVid/maker/%s.jpg\">", $v);
}
}
mysqli_free_result($result);
}
else{
echo"PM administrator with an error";
}
echo"</div><div><strong> Roles: </strong>". $row['vidRoles'] . "</div>
<div><table>";
$role = "SELECT vidRoles FROM videoinformation WHERE id=".$row['id'];
if ($result = mysqli_query($con, $role)) {
while ($row = mysqli_fetch_row($result)) {
$values = explode(',',$row[0]);
foreach($values as $v)
if (!empty($v)) {
printf ("<img class=\"roleImg img-circle\" src=\"addVid/roles/%s.jpg\">", $v);
}
}
mysqli_free_result($result);
}
else{
echo"PM administrator with an error";
}
echo "</table></div></div></div>";
echo"<div class=\"panel-body\"><div class=\"screenshots\">";
echo "<div class=\"imgShot1\"><img style=\"width:245px; height:140px\" class=\"img-thumbnail\" src=\"upload/" . $row['imgShot1'] . "\"></div>";
echo "<div class=\"imgShot2\"><img style=\"width:245px; height:140px\" class=\"img-thumbnail\" src=\"upload/" . $row['imgShot2'] . "\"></div>";
echo "<div class=\"imgShot3\"><img style=\"width:245px; height:140px\" class=\"img-thumbnail\" src=\"upload/" . $row['imgShot3'] . "\"></div>";
echo"</div>";
echo "</div><div class=\"panel-footer\">";
echo "<div class=\"vidMUrl\">
<div id=\"myDiv\"></div>
<ul id=\"playlists\" style=\"display:none;\">
<li data-source=\"playlist1\" data-playlist-name=\"MY HTML PLAYLIST\" data-thumbnail-path=\"content/thumbnails/large1.jpg\">
</li></ul>
<ul id=\"playlist1\" style=\"display:none;\">
<li data-thumb-source=\"content/thumbnails/small-fwd.jpg\" data-video-source=\"". $row['vidMUrl'] . "\" data-poster-source=\"upload/videoCover/" . $row['imgCover'] . "\" data-downloadable=\"yes\">
</li></ul>
</div>";
echo "</div></div></div>";
}
mysqli_close($con);
?>
</body>
</html>

You are assuming to get more than one row, but you are always rewriting that:
while ($row = mysqli_fetch_array($result)) {
In your main loop, you are always rewrite your $result and $row variable.
Inside the while loop, use $result2, and $row2.

Related

php search script in mysql database dosen't work

I am trying to develop a program that search in mysql database and return the search results but in when I hit the submit it displays all the results I don't know why this is happening, so any help will be appreciated
<?php
include('connect2.php');
?>
<?php
echo "<center>";
echo "<table border = '3'>";
echo "<thead>";
echo "<tr>";
echo "<th><u>id</u></th>";
echo "<th><u>name</u></th>";
echo "<th><u>countrycode</u></th>";
echo "<th><u>district</u></th>";
echo "<th><u>population</u></th>";
echo "</center>";
echo "</tr>";
echo "</thead>";
$name = isset($_POST['search']) ? $_POST['search'] : '';
$result = mysqli_query($conn, "SELECT id, name, district, population, countrycode FROM city WHERE concat(id, name, district, population, countrycode) LIKE '%$name%' ");
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_array($result)) {
echo "<tr><td>" . $row['id'] . "</td><td>" . $row['name'] . "</td><td>" . $row['countrycode'] . "</td><td>" . $row['district'] . "</td><td>" . $row['population'] . "</td></th>";
}
}
else {
header( "Location:error page.html" ); die;
}
echo "</table>";
mysqli_close($conm);
?>
the search page search.php
<?php
include('connect2.php') ;
?>
<!DOCTYPE html>
<head>
<title>city results</title>
<link href="style-table.css" rel="stylesheet">
<link href="animate.css" rel="stylesheet">
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
<script>
function validateform() {
var x = document.forms["myform"]["input"].value;
if (x == "") {
alert("input must be filled out");
return false;
}
}
</script>
</head>
<body>
<form method="POST" action="display-results-city.php" name="myform" onsubmit="return validateform()">
<input type="text" name="input" placeholder="search.........">
<button name="submit" type="submit">go</button>
</form>
<?php
echo '<div class="animated fadeInUp">';
echo '<i class="fa fa-arrow-left fa-lg" aria-hidden="true" ></i>';
echo "<div id='records'>";
echo "<table border = '0'>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>name</th>";
echo "<th>countrycode</th>";
echo "<th>district</th>";
echo "<th>population</th>";
echo "</tr>";
echo "</div>";
echo '</div>';
$sql = "select * from city limit 50";
$result = $conn->query($sql);
if( $result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row['ID']. "</td><td>" . $row['Name']. "</td><td>" . $row['CountryCode'] . "</td><td>" . $row['District'] . "</td><td>" . $row['Population'] . "</td></tr>";
}
} else {
echo " 0 results";
}
echo '</table>';
$conn->close();
?>
</body>
</html>
Try running this query..
$result = mysqli_query($conn, "SELECT id, name, district, population, countrycode FROM city WHERE UPPER(concat(id, name, district, population, countrycode)) LIKE UPPER('%$name%') ");
It will match both "Name" and "name".

Run UPDATE on single row within PHP loop

I have written this code, sorry I'm pretty new to PHP.
I have a loop which grabs all the horses from the database.
I have created a sell button underneath each entry, the problem I have is that when I hit "Sell" on a single entry it "Sell"s ALL of the entries.
Here is my code;
$query = "SELECT * FROM horses WHERE (owner = '$id') AND (status = 'active')";
if($result = $connect->query($query)) {
echo "<ul id='horses' class='row'>";
while ($row = $result->fetch_assoc()) {
echo "<li class='col-sm-4'>";
echo "<div class='horse-wrap'>";
echo "<h2>" . $row['name'] . "</h2>";
echo "<div class='details'>";
echo "<strong>Age:</strong> " . $row['age'] . "<br>";
echo "<strong>Colour:</strong> " . $row['colour'] . "<br>";
echo "<strong>Country:</strong> " . $row['country'] . "<br>";
echo "<strong>Value:</strong> £" . number_format($row['value']) . "<br>";
echo "</div>";
echo "<h3>Record</h3>";
echo "<ul class='history row'>";
echo "<li class='col-sm-3'>Runs: " . $row['runs'] . "</li>";
echo "<li class='col-sm-3'>Wins: " . $row['first'] . "</li>";
echo "<li class='col-sm-3'>2nd: " . $row['second'] . "</li>";
echo "<li class='col-sm-3'>3rd: " . $row['third'] . "</li>";
echo "</ul>";
echo "<ul class='skills'>";
echo "<h4>Speed</h4><li><span class='skill' style='width: " . $row['skill_speed'] . "%;'>" . $row['skill_speed'] . "</span></li>";
echo "<h4>Jumping</h4><li><span class='skill' style='width: " . $row['skill_jump'] . "%;'>" . $row['skill_jump'] . "</span></li>";
echo "<h4>Temperament</h4><li><span class='skill' style='width: " . $row['skill_temperment'] . "%;'>" . $row['skill_temperment'] . "</span></li>";
echo "<h4>Endurance</h4><li><span class='skill' style='width: " . $row['skill_endurance'] . "%;'>" . $row['skill_endurance'] . "</span></li>";
echo "</ul>";
// SELL HORSE BUTTON
echo "
<form action='./stables.php' method='post'>
<input type='hidden' value='" . $row['value'] . "' name='sellvalue' />
<input type='hidden' value='" . $row['name'] . "' name='sellname' />
<input type='submit' value='Sell Horse' name='sell' class='sell' />
</form>
";
echo "</div>";
echo "</li>";
// SELL HORSE
if(!empty($_POST["sell"])) {
//REMOVE HORSE FROM USER
$horseName = $row['name'];
$status = 'sold';
if($updateHorse = $connect->prepare("UPDATE horses SET status = ? WHERE name = ?")) {
$updateHorse->bind_param('ss', $status, $horseName);
$updateHorse->execute();
$updateHorse->close();
}
//ADD MONEY TO USER
$updateAmount = $money + $row['value'];
if($updateUser = $connect->prepare("UPDATE users SET money = ? WHERE id = ?")) {
$updateUser->bind_param('ss', $updateAmount, $id);
$updateUser->execute();
$updateUser->close();
}
}
}
echo "</ul>";
} else {
echo "You currently have no horses.";
}
You're doing the update inside of a loop, but you're never checking to see if the loop's current horse is the one the customer is trying to sell. As it's written, it is going to execute for every one.
You're checking to see if the form was submitted:
// SELL HORSE
if(!empty($_POST["sell"])) {
This is the perfect place to also make sure you have the right horse:
// SELL HORSE
if(!empty($_POST["sell"]) && $row["name"] === $_POST["sellname"]) {

Adding products to shopping cart results in error

My issue is that when I try to access my cart after adding products to it I keep getting the same error,
"mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean
given"
with this line of code:
$itemnumrows = mysqli_num_rows($itemsresult);
<?php
function showcart()
{
require('connect.php');
if (isset($_SESSION['SESS_ORDERNUM'])) {
if (isset($_SESSION['SESS_LOGGEDIN'])) {
$custquery = "SELECT id, status from orders WHERE customer_id = " . $_SESSION['SESS_USERID'] . " AND status < 2;";
$custresult = mysqli_query($con, $custquery);
$custrow = mysqli_fetch_assoc($custresult);
$itemsquery = "SELECT products.*, orderitems.*, orderitems.id AS itemid FROM products, orderitems WHERE orderitems.product_id =products.id AND order_id = " . $custrow['id'];
$itemsresult = mysqli_query($con, $itemsquery);
$itemnumrows = mysqli_num_rows($itemsresult);
} else {
$custquery = "SELECT id, status from orders WHERE session = '" . session_id() . "' AND status < 2;";
$custresult = mysqli_query($con, $custquery);
$custrow = mysqli_fetch_assoc($custresult);
$itemsquery = "SELECT products.*, orderitems.*, orderitems.id AS itemid FROM products, orderitems WHERE orderitems.product_id = products.id AND order_id = " . $custrow['id'];
$itemsresult = mysqli_query($con, $itemsquery);
$itemnumrows = mysqli_num_rows($itemsresult);
}
} else {
$itemnumrows = 0;
}
if ($itemnumrows == 0) {
echo "You have not added anything to your shopping cart yet.";
} else {
echo "<table cellpadding='10'>";
echo "<tr>";
echo "<td></td>";
echo "<td><strong>Item</strong></td>";
echo "<td><strong>Quantity</strong></td>";
echo "<td><strong>Unit Price</strong></td>";
echo "<td><strong>Total Price</strong></td>";
echo "<td></td>";
echo "</tr>";
while ($itemsrow = mysqli_fetch_assoc($itemsresult)) {
$quantitytotal = $itemsrow['price'] * $itemsrow['quantity'];
echo "<tr>";
if (empty($itemsrow['image'])) {
echo "<td><img src='productimages/dummy.jpg' width='50' alt='" . $itemsrow['name'] . "'></td>";
} else {
echo "<td><img src='productimages/" . $itemsrow['image'] . "' width='50' alt='" . $itemsrow['name'] . "'></td>";
}
echo "<td>" . $itemsrow['name'] . "</td>";
echo "<td>" . $itemsrow['quantity'] . "</td>";
echo "<td><strong>£" . sprintf('%.2f', $itemsrow['price']) . "</strong></td>";
echo "<td><strong>£" . sprintf('%.2f', $quantitytotal) . "</strong></td>";
echo "<td>[<a href='delete.php?id=" . $itemsrow['itemid'] . "'>X</a>]</td>";
echo "</tr>";
#$total = $total + $quantitytotal;
$totalquery = "UPDATE orders SET total = " . $total . " WHERE id = " . $_SESSION['SESS_ORDERNUM'];
$totalresult = mysqli_query($con, $totalquery);
}
echo "<tr>";
echo "<td></td>";
echo "<td></td>";
echo "<td></td>";
echo "<td>TOTAL</td>";
echo "<td><strong>£" . sprintf('%.2f', $total) . "</strong></td>";
echo "<td></td>";
echo "</tr>";
echo "</table>";
echo "<p><a href='checkout.php'>Go to the checkout</a></p>";
}
}
?>
Heres my showcart page, also getting the same error with this line:
$numrows = mysqli_num_rows($result);
<?php
session_start();
require("header.php");
require("functions.php");
echo "<h1>Your shopping cart</h1>";
showcart();
if (isset($_SESSION['SESS_ORDERNUM']) == TRUE) {
$query = "SELECT * FROM orderitems WHERE order_id = " . $_SESSION['SESS_ORDERNUM'] . ";";
$result = mysqli_query($con, $query);
$numrows = mysqli_num_rows($result);
if ($numrows >= 1) {
echo "<h2><a href='checkout.php'>Go to the checkout</a></h2>";
}
}
require("footer.php");
?>
My header page as well, when I click the viewcart link it outputs same errors as above blocks of code..
<?php
if (!isset($_SESSION)) {
session_start();
}
if (isset($_SESSION['SESS_CHANGEID']) == TRUE) {
session_unset();
session_regenerate_id();
}
include("connect.php");
?>
<html>
<head>
<link rel="stylesheet" href="css/style.css">
</head>
<body>
<div id="header">
<h1 style="color:#ffffff"> <?php echo $site; ?> </h1>
</div>
<div id="menu">
Home
View Basket/Checkout</div>
<div id="container">
<div id="bar">
<?php
include("bar.php");
echo "<hr />";
if (isset($_SESSION['SESS_LOGGEDIN']) == TRUE) {
echo "Logged in as <strong>'" . $_SESSION['SESS_USERNAME'] . "'</strong>[<a href='" . $access . "logout.php'>logout</a>]";
} else {
echo "<a href='" . $access . "login.php'>Login</a>";
} ?></div>
<div id="main">

implode in where class

I am having a problem with array while using it in "select" statement
that array contains the following strings
Array
(
[0] => M.A.JINNA
[1] => K.DHANA RAJU
[2] => B.EPHRIM
)
array data had came from the following data
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<style>
table{
border : 1px solid black;
}
tr{
border : 1px solid black;
}
td{
border : 1px solid black;
}
</style
<head>
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Update Deployement</title>
<style type="text/css">
body
{
background-color: #00FF00;
background-image: url(images/gail-india.jpg);
color: #000000;
scrollbar-face-color: #0B0B0B;
scrollbar-arrow-color: #C8C8C8;
scrollbar-3dlight-color: #0B0B0B;
scrollbar-darkshadow-color: #000000;
scrollbar-highlight-color: #141414;
scrollbar-shadow-color: #060606;
scrollbar-track-color: #0B0B0B;
}
</style>
</head>
<body>
<form method="post" action="edit_data.php">
<div id="wb_Image3" style="margin:0;padding:0;position:absolute;left:7px;top:4px;width:208px;height:129px;text-align:left;z-index:0;">
<img src="images/image_thumb3.png" id="Image2" alt="" border="0" style="width:208px;height:129px;"></div>
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("gail", $con);
$installation_1 = trim($_POST['installation']);
$area_1 = trim($_POST['area']);
$district_1 = trim($_POST['district']);
if(empty($area_1) AND empty($district_1))
{
$sql = "SELECT * FROM deployment WHERE installation ='" . $installation_1 . "'";
}
else if(empty($installation_1) AND empty($district_1))
{
$sql = "SELECT * FROM deployment WHERE area ='" . $area_1 . "'";
}
else if(empty($installation_1) AND empty($area_1))
{
$sql = "SELECT * FROM deployment WHERE district ='" . $district_1 . "'";
}
else if(empty($district_1))
{
$sql = "SELECT * FROM deployment WHERE installation ='" . $installation_1 . "' AND area ='" . $area_1 . "'";
}
else if(empty($area_1))
{
$sql = "SELECT * FROM deployment WHERE installation ='" . $installation_1 . "' AND district ='" . $district_1 . "'";
}
else if(empty($installation_1))
{
$sql = "SELECT * FROM deployment WHERE area ='" . $area_1 . "' AND district ='" . $district_1 . "'";
}
else
{
$sql = "SELECT * FROM deployment WHERE installation ='" . $installation_1 . "' AND area ='" . $area_1 . "' AND district ='" . $district_1 . "'";
}
$result = mysql_query($sql);
echo "<table id='table1' width = '500' align = 'center' style= 'border:1px'>";
echo "<tr><b>";
echo "<td>Installation</td>";
echo "<td>Area</td>";
echo "<td>District</td>";
echo "<td>Employee Name</td>";
echo "<td>Reference</td>";
echo "</b></tr>";
$employee = array();
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo ("<td>$row[installation]</td>");
echo ("<td>$row[area]</td>");
echo ("<td>$row[district]</td>");
echo ("<td>$row[employeename]</td>");
echo ("<td>$row[ref]</td>");
echo"</tr>";
$employee[] = $row['employeename'];
$arrlength = count($employee);
}
echo"</table>";
echo '<pre>'; print_r(array_filter($employee)); echo '</pre>';
?>
</body>
</html>
now I am getting error like this: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''M.A.JINNA,K.DHANA RAJU,B.EPHRIM'' at line 1
please give me any suggestions, thanks in advance
NOTE: I cannot use employee id instead of employee name due to client request
the code is
<?
$emp = implode( ',', $employee );
echo '<pre>'; print_r($emp); echo '</pre>';
$sql = "SELECT * FROM securitystaffdetails WHERE employeename IN '" . $emp . "'";
$result = mysql_query($sql) or die(mysql_error());
echo "<table id='table1' width='1500' style= 'border:1px'>";
echo "<tr><b>";
echo "<td>Employee Name</td>";
echo "<td>Address</td>";
echo "<td>DOB</td>";
echo "<td>Age</td>";
echo "<td>SEx</td>";
echo "<td>Mobile Number</td>";
echo "<td>Blood Group</td>";
echo "<td>ID Card</td>";
echo "<td>Ex Army Idcard</td>";
echo "<td>Police Clearence</td>";
echo "<td>ESI Card</td>";
echo "<td>PF Account</td>";
echo "<td>PAN Card</td>";
echo "<td>Voter ID</td>";
echo "<td>Ration/Family</td>";
echo "</b></tr>";
$employee = array();
while($record = mysql_fetch_object($result))
{
echo "<tr>";
echo ("<td>$record[employeename]</td>");
echo ("<td>$record[address]</td>");
echo ("<td>$record[dob]</td>");
echo ("<td>$record[age]</td>");
echo ("<td>$record[sex]</td>");
echo ("<td>$record[mobn]</td>");
echo ("<td>$record[bg]</td>");
echo ("<td>$record[icard]</td>");
echo ("<td>$record[exarmycard]</td>");
echo ("<td>$record[policeclearence]</td>");
echo ("<td>$record[esicard]</td>");
echo ("<td>$record[pfa]</td>");
echo ("<td>$record[pancard]</td>");
echo ("<td>$record[acard]</td>");
echo ("<td>$record[vcard]</td>");
echo ("<td>$record[rcard]</td>");
echo"</tr>";
}
echo"</table>";
?>
Change your implode line to be:
$emp = implode( "','", $employee );
and your query to be:
"SELECT * FROM securitystaffdetails WHERE employeename IN ('" . $emp . "')";
you missed ().
$sql = "SELECT * FROM securitystaffdetails WHERE employeename IN ('" . $emp . "')";

Select db rows between a date range

Im trying to add a form so I can select a date range then resubmit the form so I can have rows only from a specific date range listed in the table, but I cant get it to work very well. Whats wrong in the code ? (php newbie alert) (UPDATED)
<html>
<head>
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="HandheldFriendly" content="true">
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>EBS Service Skjema</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<link href="print.css" rel="stylesheet" media="print" type="text/css" />
</head>
<body>
<?php
include 'connection.php';
// Check if session is not registered, redirect back to main page.
// Put this code in first line of web page.
session_start();
if (!isset($_COOKIE["user"])) {
header("location:login.php");
}
echo "<div class=\"blackbar\">Sjåfør:" .$_COOKIE["user"]."</div>";
$dbx = $_POST["databases"];
$con=mysqli_connect("$host", "$username", "$password","$db_name")or die("cannot connect");
?>
<div class="bluebox"><form action="data.php" method="post">
<h2 align="center">Utskrift av logger</h2>
<label>Velg type logg du vil skrive ut:</label><br />
<select name="databases">
<option value="trallevask">Trallevask</option>
<option value="bilvask">Bilvask</option>
<option value="maintenance">Vedlikehold</option>
<option value="diesel">Diesel Stats</option>
<option value="workhours">Arbeidstid</option>
</select>
<input type="date" name="df" /><input type="date" name="dt" />
<input type="submit" value="Submit"><input type="button" value="Tilbake" onClick="parent.location='index.php'" />
<input type="button" value="Skriv ut" onclick="window.print();return false;" />
</form></div>
<?php
//Row Colors setting
$color1 = "#E1EEf4";
$color2 = "#FFFFFF";
$row_count = 0;
echo "<div class=\"datagrid\"><table width=\"100%\" cellspacing=\"0\" cellpadding=\"5\" border=\"0\">";
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if ($dbx == "trallevask") {
//$result = mysqli_query($con,"SELECT trallevask.regnr, trallevask.date, trallevask.type, equipment.eqname AS vaske_type FROM trallevask JOIN equipment ON equipment.eqid = trallevask.type WHERE userid = ". $_COOKIE['userid']);
$result = mysqli_query($con, "SELECT trallevask.regnr, trallevask.date,
trallevask.type, equipment.eqname AS vaske_type
FROM trallevask JOIN equipment ON equipment.eqid = trallevask.type
WHERE userid = ". $_COOKIE['userid'] . " AND trallevask.date
BETWEEN " . $_POST['df'] . " AND " . $_POST['dt']);
echo "<thead><tr><th>Dato</th><th>Tralle nr.</th><th>Vaskemiddel</th><th>Utført av</th></tr></thead><tbody>";
while($row = mysqli_fetch_array($result))
{
$row_color = ($row_count % 2) ? $color1 : $color2;
echo "<tr bgcolor=\"$row_color\"><td>" . $row['date'] . "</td><td>" . $row['regnr'] ."</td><td>" . $row['vaske_type'] . "</td><td>".$_COOKIE['user']."</td></tr>";
$row_count++;
}
}
elseif ($dbx == "bilvask") {
//$result = mysqli_query($con,"SELECT * FROM bilvask, equipment");
$result = mysqli_query($con,"SELECT bilvask.date, bilvask.type, equipment.eqname AS vaske_type FROM bilvask JOIN equipment ON equipment.eqid = bilvask.type WHERE userid = ". $_COOKIE['userid']);
echo "<thead><tr><th>Dato</th><th>Beskrivelse</th></tr></thead><tbody>";
while($row = mysqli_fetch_array($result))
{
$row_color = ($row_count % 2) ? $color1 : $color2;
echo "<tr bgcolor=\"$row_color\"><td>" . $row['date'] . "</td><td>" . $row['vaske_type'] ."</td></tr>";
$row_count++;
}
}
elseif ($dbx == "workhours") {
$result = mysqli_query($con,"SELECT * FROM workhours WHERE userid = ". $_COOKIE['userid']);
echo "<thead><tr><th>Dato</th><th>Status</th><th>Klokka</th></tr></thead><tbody>";
while($row = mysqli_fetch_array($result))
{
$row_color = ($row_count % 2) ? $color1 : $color2;
echo "<tr bgcolor=\"$row_color\"><td>" . $row['date'] . "</td><td>" . $row['status'] ."</td><td>" . $row['time'] . "</td></tr>";
$row_count++;
}
}
elseif ($dbx == "diesel") {
$result = mysqli_query($con,"SELECT * FROM diesel WHERE userid = ". $_COOKIE['userid']);
echo "<thead><tr><th>Dato</th><th>Km.stand</th><th>Liter</th><th>KR/L</th><th>Stasjon</th><th>Sted</th></tr></thead><tbody>";
while($row = mysqli_fetch_array($result))
{
$row_color = ($row_count % 2) ? $color1 : $color2;
echo "<tr bgcolor=\"$row_color\"><td>" . $row['dato'] . "</td><td>" . $row['km'] . "</td><td>" . $row['liter'] . "</td><td>" . $row['krl'] . "</td><td>" . $row['stasjon'] . "</td><td>" . $row['sted'] . "</td></tr>";
$row_count++;
}
}
elseif ($dbx == "maintenance") {
$result = mysqli_query($con,"SELECT * FROM maintenance WHERE userid = ". $_COOKIE['userid']);
echo "<thead><tr><th>Dato</th><th>Km.stand</th><th>Reg.nummer</th><th>Beskrivelse</th></tr></thead><tbody>";
while($row = mysqli_fetch_array($result))
{
$row_color = ($row_count % 2) ? $color1 : $color2;
echo "<tr bgcolor=\"$row_color\"><td>" . $row['date'] . "</td><td>" . $row['mileage'] ."</td><td>" . $row['registration'] ."</td><td>" . $row['info'] ."</td></tr>";
$row_count++;
}
}
else {
echo "Velg en rapport fra menyen over!";
}
echo "</tbody></table></div>";
mysqli_close($con);
?>
</body>
</html>
May be this will help. You haven't used df and dt parameters in SQL query:
$result = mysqli_query($con, "SELECT trallevask.regnr, trallevask.date,
trallevask.type, equipment.eqname AS vaske_type
FROM trallevask JOIN equipment ON equipment.eqid = trallevask.type
WHERE userid = ". $_COOKIE['userid'] . " AND trallevask.date
BETWEEN " . $_POST['df'] . " AND " . $_POST['dt']);
First thing I see that HTML is formatted incorrectly:
Change line:
echo "<div class=\"datagrid\"><table width=\"100%\" cellspacing=\"0\" cellpadding=\"5\" border=\"0\"><input type=\"submit\" /></form>";
to
echo "<input type=\"submit\" /></form><div class=\"datagrid\"><table width=\"100%\" cellspacing=\"0\" cellpadding=\"5\" border=\"0\">";
Also in the very last line before ?> add </table></div> because you need to close DIV and TABLE tags.
I also can't see where $_POST["databases"]; is coming from, that might cause code to work incorrectly.
select *
from table
where date >= [start date] and date <= [end date]
or (i'm not 100% sure if this works in mysql but it does in postgres!)
select *
from table
where date between [start date] and [end date]

Categories