I am using mysql database and I am looking for a way to display particular rows for column B when the column A value meets a specific criteria.
For Example in TBL_NAME:
ColumnA = Months
ColumnB = Holidays
Veterans Day
Thanksgiving
etc...
I would like to display on a page all the Holidays occurring in the month of November. I do not want to display any holidays in other months. Here is the code I have which is returning the error "Unknown column 'ColumnA' in 'where clause'."
<?php
error_reporting(0);
require 'db/connect.php';
$Month=November;
$data = $conn->query("SELECT * FROM TBL_NAME WHERE ColumnA=$Month") or die($conn->error);
$numRows = $data->num_rows;
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Database Connect</title>
</head>
<body>
<?php
// Print results to the page
while($row = $data->fetch_assoc()) { ?>
<p><?php echo $row['LinkName']; ?></p>
<?php } ?>
</body>
</html>
Related
Now I have been trying to figure out a way to insert a dropdown menu selection by a user on the web-interface into MySQL database table_2 using PHP. The problem is that the dropdown list items are retrieved from the MySQL database from another table_2. Can someone please help me? Thank you in advance! Below shows the code I am using.
<?php
$con = mysqli_connect("localhost","root","");
$myDB = mysqli_select_db($con, "database");
$sqlSELECT = mysqli_query($con, 'SELECT disastergroup FROM disastergroups');
if (isset($_POST['group']))
{
$group = $_POST['group'];
$test = "SELECT disastergroupid FROM disastergroups WHERE disastergroup = '$group'";
mysqli_query($con, $test);
$test_store = "INSERT INTO events (groupid_FK) VALUES ($test);"
mysqli_query($con,$test_store);
}
else
{
echo "An option must be selected!";
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Title of Page</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
</head>
<body>
<form action = "detailslog.php" method = "POST">
<label for="groups">Disasters:</label>
<select name = "groups">
<option value = "">Select...</option>
<?php while($row = mysqli_fetch_assoc($sqlSELECT)):;?>
<option><?php $row1['disastergroup'];?></option>
<?php endwhile;?>
</select>
<input type="submit" value="Submit Data">
</form>
</body>
</html>
So what I want to do is take the user's selection from the options "groups" and use that value to get the ID of that value from the table disastergroups and then store that ID into the table "events" as a Foreign Key. This have been giving me hell to figure out. Any help would be greatly appreciated! Thank you!
$sql_quer = mysqli_query($con, $test);
$FK_id = mysqli_fetch_assoc($sql_quer);
$test_store = "INSERT INTO events (groupid_FK) VALUES ($FK_id[disastergroupid]);"
mysqli_query($con,$test_store);
This fixed my problem for anyone viewing this post!
I'm a student using NetBeans to create very basic webpage(s) using HTML, PHP and SQLite. So far, everything is fine. The problem I have is that images aren't displayed on the moviedetails.php page. Everything else including the titles, ratings and description for each table entry works fine. (I am retrieving rows from a database table.) Here is my code:
(This is very new to me, so if it's a simple mistake, sorry for wasting your time :/)
Index.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
$pdo = new PDO('sqlite:movies.db'); //Import SQLite database "movies.db" to a Var
$query = $pdo->query("SELECT * FROM movie");
while ($row = $query->fetch(PDO::FETCH_ASSOC)) {
//For each id number in db, echo a hyperlink containing that ID's title and
echo '' . htmlentities($row['title']) . '';
echo '<br>';
}
?>
</body>
moviedetails.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
$pdo = new PDO('sqlite:movies.db'); //Using movies.db
$query = $pdo->prepare("SELECT * FROM movie WHERE id=:id"); //Prepare this statement
$id = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT); //GET INPUT from Variable 'id' and FILTER anything which isn't a number
$query->bindParam(':id', $id, PDO::PARAM_INT); //Bind :name 'id' to a $id variable
$query->execute(); //Execute the prepared statement
$row = $query->fetch(PDO::FETCH_ASSOC); //Fetch next row of results
//var_dump($row);
//display title, description and rating
echo '<h1>'.htmlentities($row['title']).'</h1>'; //Echo 'Title' from db into a heading
echo ''; //Echo 'image from db into a link
echo '<p>'.htmlentities($row['description']).'</p>'; //Echo 'description' from db to paragraph
echo '<p>Rating: '. htmlentities($row['rating']).'</p>'; //Echo 'rating' from db to paragraph
?>
</body>
Here is my database in an image, as this is the easiest way to show you:
http://i.cubeupload.com/TBI5Fv.png
Here is one of the webpages that should diplay a link. However, it contains only the other table fields:
http://i.cubeupload.com/1tcfsU.png
The strange thing is, it doesn't give me any errors, so I don't know where I'm going wrong.
Hope someone can help :)
Your <a> tag is empty, so it's invisible.
echo '';
You should put some content that will be displayed as a link like this:
echo 'THIS IS LINK TO IMAGE';
If you want to display the image itself instead of a link, you should use <img> tag like this:
echo '<img src="'.htmlentities($row['image']).'"/>';
//here is my updated code it now compares the current time with the
saved time in mysql but my problem is it only compares with the
first added time what I need is the current time will compare it to
the nearest time saved in mysql.
<?php
//Include the database configuration
include 'config.php';
//Get the data of the selected teacher
$teacher = $dbconnect->prepare("SELECT * FROM teacher_info
WHERE IMEI = ? AND NFC = ?");
$teacher->bindValue(1,$_GET['IMEI']);
$teacher->bindValue(2,$_GET['NFC']);
$teacher->execute();
//Get the data
$teacher_info = $teacher->fetch();
//If there is such a teacher let the teacher enter
if(!empty($teacher_info))
{
$time_out = $dbconnect->prepare("INSERT INTO time_out (teacher_id,name,NFC,IMEI,time_out) VALUES (?,?,?,?,NOW())");
$time_out->bindValue(1,$teacher_info['teacher_id']);
$time_out->bindValue(2,$teacher_info['name']);
$time_out->bindValue(3,$teacher_info['NFC']);
$time_out->bindValue(4,$teacher_info['IMEI']);
$time_out->execute();
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Welcome!</title>
</head>
<body>
<h1>
<?php
//If there is such a teacher,welcome him/her
if(!empty($teacher_info))
{
echo 'Welcome '.$teacher_info['name'].'! Your NFC is '.$teacher_info['NFC'];
}
else
{
echo 'You are not registered.';
}
?>
</h1>
</body>
</html>
//Hope you can help me out
Assuming that you have a time_in column in the table time_in
if($stmt = $dbconnect->prepare("SELECT time_in from time_in")){
$stmt->bind_param("s", $time_in);
$stmt->execute();
$cur_time = time();
if($time_in === $cur_time){
echo "Teacher is on time";
}
}
This is mainly an idea about how things should be going. There's always room for improvement. To be precise, you should give a teacher_id in the time_in table so that the respective teacher's time is selected only.
EDIT:
if($stmt = $dbconnect->prepare("SELECT time_in from time_in")){
$stmt->bind_param("s", $time_in);
$stmt->execute();
if($stmtt = $dbconnect->prepare("SELECT time from profschedule")){
$stmtt->bind_param("s", $time);
$stmt->execute();
if($time_in === $time)
echo "Teacher is on time";
}}}
Although, this is poorly written but the whole point of it is to give you the IDEA about how to approach after inspecting your perspective in the comments.
<?php require_once('Connections/pdoConnect.php'); ?>
<?php
// Issue the query
$pdo_recordset1 = $conn->query("SELECT id, title, author, subject FROM audio");
$pdo_recordset1->setFetchMode(PDO::FETCH_ASSOC);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Audio List</title>
</head>
<body>
<?php
// Now iterate over every row and display it
$n = 0;
while($r = $pdo_recordset1->fetch(PDO::FETCH_ASSOC))
{
?>
<a href="PDO_detail.php?item=<?php echo $r['id']?>">
<?php echo ($r['subject'])?><?php echo ($r['title'])?><?php echo ($r['author'])?></a><br />
<?php
++$n;
}
if(0 == $n)
{
echo "Sorry there are no items to display";
}
?>
</body>
</html>
This produces a list of all entries in the database. However I want to separate out the "subject" data as a heading above each title within that subject group. Eg:
SUBJECT 1
Title 1
Title 2
Title 3
SUBJECT 2
Title 1
Title 2
etc
How should I modify the above code to achieve this? I'm guessing it will need a nested loop, but not sure how to put this together.
You can add the subjects into an array in one loop and then in for each subject in that array you can go through the whole list and echo the title and author if the subject matches.
$subjects = array();
array_push($subjects, $r['subject']);
usage requested so something like this though if someone could edit it to be correct and more efficient...
<?php
$subjects = array();$n=0;
while($r = $pdo_recordset1->fetch(PDO::FETCH_ASSOC))
{array_push($subjects, $r['subject']);}
while($s = $subject->$subjects)
{
while($r = $pdo_recordset1->fetch(PDO::FETCH_ASSOC))
{
if($r['subject']==$s)
{
?><a href="PDO_detail.php?item=<?php echo $r['id']?>">
<?php echo ($r['subject']); echo ($r['title']); echo ($r['author'])?>
</a><br /><?php
}
++$n;
}
if(0 == $n){echo "Sorry there are no items to display";}
}
?>
I want to be able to sum up all the revenue that is being displayed in the page and it auto sums every time I added another data to the revenue column:
Following is my code :
<?php
require_once('Connections/connect.php');
$id_customer = mysql_real_escape_string($_GET['id_customer']);
$sql_PK = "SELECT * FROM tbl_delivery_details WHERE tbl_customer_id_customer = {$id_customer}";
$PK = mysql_query($sql_PK, $connect);
if ( mysql_error() ) {
die ( mysql_error());
}
$row_PK = mysql_fetch_assoc($PK);
$customer_name = $row_PK['tbl_customer_id_customer'];
$customer_name = mysql_real_escape_string($customer_name);
$sql = "SELECT tbl_customer.customer_name,
tbl_delivery_details.delivery_details_route,
tbl_delivery_details.delivery_details_destination,
tbl_delivery_details.delivery_details_van_no,
tbl_delivery_details.delivery_details_waybill_no,
tbl_delivery_details.delivery_details_charge_invoice,
tbl_delivery_details.delivery_details_revenue,
tbl_delivery_details.delivery_details_strip_stuff,
tbl_delivery_details.delivery_details_date
FROM tbl_customer, tbl_delivery_details
WHERE tbl_customer.id_customer = tbl_delivery_details.tbl_customer_id_customer
AND tbl_customer.id_customer = '{$customer_name}'";
$res = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($res);
$sum = 0;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/x html">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Customer Revenue</title>
<link rel="stylesheet" type="text/css" href="qcc.css"/>
</head>
<body>
<table border="1">
<tr>
<th>Reveneu</th>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_PK['delivery_details_revenue'];?></td>
</tr>
<?php } while ($row_PK = mysql_fetch_assoc($PK));?>
<?php { ?>
<?php $sum+=$row_PK['delivery_details_revenue'] ?>
<?php } ?>
</table>
<?php echo $sum; ?>
</body>
</html>
When I load the page echo $sum always is zero how to correctly sum up the column I made that it will sum automatically if I add another data to it :
Instead of adding the revenue values up in PHP, why not have MySQL do it for you in the query?
$sql = "SELECT SUM(tbl_delivery_details.delivery_details_revenue) as revenue,
tbl_customer.customer_name,
tbl_delivery_details.delivery_details_route,
tbl_delivery_details.delivery_details_destination,
tbl_delivery_details.delivery_details_van_no,
tbl_delivery_details.delivery_details_waybill_no,
tbl_delivery_details.delivery_details_charge_invoice,
tbl_delivery_details.delivery_details_revenue,
tbl_delivery_details.delivery_details_strip_stuff,
tbl_delivery_details.delivery_details_date
FROM tbl_customer, tbl_delivery_details
WHERE tbl_customer.id_customer = tbl_delivery_details.tbl_customer_id_customer
AND tbl_customer.id_customer = '{$customer_name}'";
And then in youru view, just echo the SUM figure...
echo $row_PK['revenue'];
If I read this correctly, you're summing up the values outside of your while loop. That won't work.
I think you're mixing up a normal while loop, and a 'do while' loop.
See this code:
<?php do { ?>
<tr>
<td><?php echo $row_PK['delivery_details_revenue'];?></td>
</tr>
<?php } while ($row_PK = mysql_fetch_assoc($PK));?>
<?php { ?>
<?php $sum+=$row_PK['delivery_details_revenue'] ?>
<?php } ?>
It should be more along these lines:
<?php do { ?>
<tr>
<td><?php
echo $row_PK['delivery_details_revenue'];
$sum+=$row_PK['delivery_details_revenue']
?>
</td></tr>
<?php } while ($row_PK = mysql_fetch_assoc($PK));?>
this wouldn't happen if you would write the code a bit more clearly; try to avoid interleaving html and php so much:
<?php
do {
$revenue = $row_PK['delivery_details_revenue'];
$sum += revenue;
println("<tr><td>$revenue</td></tr>");
} while ($row_PK = mysql_fetch_assoc($PK));
?>
This is a lot clearer, if you ask me.
Well, I don't have a PHP interpreter in my head to run your code on sight. So, just a few things which I can spot
First, there is an SQL injection in your first query. Either cast your variable to integer
$id_customer = intval($_GET['id_customer']);
or treat it as a string in your query
$sql_PK = "SELECT * FROM tbl_delivery_details WHERE tbl_customer_id_customer = '$id_customer'";
or - better yet - use some database wrapper that allows you to use placeholders to represent actual data in the query.
Next, your query is incredible hard to read.
If your field names do not interfere, there is no reason to use table.field notation then.
Also use shortland aliases and consider using * if you want most of the fields from the table:
$sql = "SELECT SUM(delivery_details_revenue) as revenue,
customer_name, tbl_delivery_details.*
FROM tbl_customer, tbl_delivery_details
WHERE id_customer = tbl_customer_id_customer
AND id_customer = '$customer_name'";
By the way, while editing your query, I've noticed inconsistent naming: id_customer = '$customer_name'. Don't confuse yourself with wrong variable names. If it's id, then call it "id", not "name"
And also I see no point in the first query at all, if id_customer is equal to tbl_customer_id_customer. I think you need to simplify your code - it's compexity is the main reason why you're not getting your results, I believe.
Start from very simple query like
$sql = "SELECT SUM(delivery_details_revenue) as revenue,
FROM tbl_delivery_details
WHERE tbl_customer_id_customer = '$id_customer'";
and see if it returns anything.
If so - start adding some more data to fetch.
If no - check your data and overall data structure if it's all right.