implode in where class - php

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 . "')";

Related

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">

php Undefined offset 1

I seem to be getting Undefined offset 1 error when I hit submit. I have been trying to play with the numbers but nothing seems to help. I added a fourth column right after the commented last name. Before the addition the code worked. I realized later on that I needed that third column. Ever since then I get an error and can not update the sql table.
Thank you in advance,
Avi
<!DOCTYPE html>
<?php
echo '<link rel="stylesheet" type="text/css" href="css/newStyle.css"></head>';
session_start();
if (isset($_SESSION['loggedIn']) && ($_SESSION['loggedIn'] == true) && $_SESSION['admin'] == true) {
echo "<br><h3>Welcome to the administrative area Prof. " . $_SESSION['firstname'] . "!</h3><br><br>";
} else {
//echo "<br>Please log in first to see this page.";
header ('Location: index.php');
}
require_once 'login.php';
$connection = new mysqli($hn,$un,$pw,$db);
if($connection->connect_error) die($connection->connect_error);
if(isset($_POST['submit'])){
for($i = 0; $i < $_POST['totalGrades']; $i++){
echo $i . ': ' . $_POST['grade' .$i] . '<br>';
$parts = explode("|", $_POST['grade' .$i]);
$newGrade = "UPDATE Grades SET grade = '" . $parts[1] . "' WHERE gradeID = " .$parts[0];
$result = $connection->query($newGrade);
}
}
$username = "";
$courseId = "";
$grade = "";
$courseName = "";
?>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<link rel="stylesheet" href="">
</head>
<body>
<form method= "post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<link rel="stylesheet" href="css/style.css">
<?php
// $courseSct =
"SELECT username, courseName, grade, gradeID FROM Courses\n"
// . "JOIN Grades\n"
// . "ON courses.courseId = Grades.courseId";
$courseSct =
"SELECT u.firstname, u.lastname, c.courseName, g.grade "
. " FROM Grades g "
. " INNER JOIN Courses c ON c.courseID = g.courseID "
. " INNER JOIN Users u ON u.userID = g.userID "
. " WHERE c.professorID = " .$_SESSION['userID'];
$result = $connection->query($courseSct);
$rows = $result->num_rows;
echo
"<table border = '1' width = '50%'>"
. "<caption><h2>Grades Table</h2></caption>"
. "<tr>"
. '<th>First Name</th>'
. "<th>Last Name</th>"
. "<th>Course Name</th>"
. "<th>Grade</th>"
//. "<th>New Value</th>"
. "</tr>";
for($j = 0; $j < $rows; ++$j) {
$result->data_seek($j);
$row = $result->fetch_array(MYSQLI_NUM);
echo
"<tr>" .
"<td>" . $row[0] . "</td>" . //First Name
"<td>" . $row[1] . "</td>" . //Last Name
"<td>" . $row[2] . "</td>";
"<td>"; //Grade
echo '<select name="grade' . $j . '" size="1" id="' . $row[3] . '">';
echo '<option value="select">Select</option>';
$letterGrade = 'A';
for($x = 0; $x < 6; $x++) {
echo '<option value="' . $row[3] . '|' . $letterGrade . '"';
if($letterGrade == $row[3]) {
echo ' selected';
}
echo '>' . $letterGrade++ . '</option>';
}
echo '</select><br>'. "</td>" . "</tr>";
}
echo "</table>";
?>
<input type="hidden" name="totalGrades" value="<?php echo $rows;?>">
<br>
<input type="submit" name="submit" value="Submit">
<br>
</form>
<a href='index.php?logout'><br>click here to log out<br></a>
</body>
</html>
Have you tried a foreach instead?
if(isset($_POST['submit'])){
foreach($_POST['totalGrades'] as $key => $value) {
echo $key . ': ' . $value . '<br>';
$parts = explode("|", $value);
$newGrade = "UPDATE Grades SET grade = '" . $parts[1]
. "' WHERE gradeID = " .$parts[0];
$result = $connection->query($newGrade);
}
}

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

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.

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]

Rails: printing mysql query

I cant figure out how to display MySql information using Ruby on Rails.
This is the query i want to run
SELECT name, description FROM projects where status > 1
If someone can translate to Rails the code below it would be greatly apreciatted
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Proyects</title>
</head>
<?php
$dsn = "mysql:dbname=redmine_default";
$username = "root";
$password = "";
try {
$conn = new PDO( $dsn, $username, $password );
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
} catch ( PDOException $e ) {
echo "Error: " . $e->getMessage();
}
function mostrarProyectos($conn) {
$sql = 'SELECT * FROM projects';
foreach ($conn->query($sql) as $row) {
print $row['name'] . "\t";
print $row['id'] . "\t";
print $row['description'] . "\n";
}
}
//creating a table with 4 columns
?>
<table border="1" cellspacing=1 cellpadding=2 style="font-size: 8pt"><tr>
<td><font face="verdana"><b>ID</b></font></td>
<td><font face="verdana"><b>Name</b></font></td>
<td><font face="verdana"><b>Status</b></font></td>
<td><font face="verdana"><b>Desc</b></font></td>
</tr>
<?php
//here comes the SQL query
$query = "SELECT name, description FROM projects where status > 1";
$resultado = $conn->query($query);
$numero = 0;
foreach($resultado as $row)
{
echo "<tr><td width=\"25%\"><font face=\"verdana\">" .
$row["id"] . "</font></td>";
echo "<td width=\"25%\"><font face=\"verdana\">" .
$row["name"] . "</font></td>";
echo "<td width=\"25%\"><font face=\"verdana\">" .
$row["status"] . "</font></td>";
echo "<td width=\"25%\"><font face=\"verdana\">" .
$row["description"]. "</font></td></tr>";
$numero++;
}
echo "<tr><td colspan=\"15\"><font face=\"verdana\"><b>N&uacutemero: " . $numero .
"</b></font></td></tr>";
mysql_free_result($result);
mysql_close($link);
?>
</table
</body>
</html>
The RoR code would be something like this:
Project.where("status > ?", 10)
http://guides.rubyonrails.org/active_record_querying.html
The link will give you a large list of mysql queries and their RoR counterpart.

Categories