I like to fetch data from MySQL by Select Month and year in HTML Select Option, When i submit view, Table will be filtered. Else all data will be showing. But my code showing error.
Notice: Undefined variable: user in C:\xampp\htdocs\testSelect.php on line 85
Fatal error: Call to a member function getAllUser() on null in C:\xampp\htdocs\testSelect.php on line 85
How can solve this error. Please help me.
here my code:
<?php
class DatabaseConnection{
public function __construct(){
global $pdo;
try{
$pdo=new PDO('mysql:host=localhost;dbname=test','root','');
}catch(PDOException $e){
exit('Database Error');
}
}
}
?>
<?php echo "Select : " ; ?>
<?php
$monthArray = range(1, 12);
?>
<select name="month">
<option value="">Select Month</option>
<?php
foreach ($monthArray as $month) {
// padding the month with extra zero
$monthPadding = str_pad($month, 2, "0", STR_PAD_LEFT);
// you can use whatever year you want
// you can use 'M' or 'F' as per your month formatting preference
$fdate = date("F", strtotime("2015-$monthPadding-01"));
echo '<option value="'.$monthPadding.'">'.$fdate.'</option>';
}
?>
</select>
<?php
// set start and end year range
$yearArray = range(2000, 2050);
?>
<!-- displaying the dropdown list -->
<select name="year">
<option value="s">Select Year</option>
<?php
foreach ($yearArray as $year) {
// if you want to select a particular year
$selected = ($year == 's') ? 'selected' : '';
echo '<option '.$selected.' value="'.$year.'">'.$year.'</option>';
}
?>
</select>
<form method="post">
<input type="submit" name="view" value="View" />
</form>
<?php
function getAllUserReport($username){
global $pdo;
$query=$pdo->prepare("SELECT * FROM user name= $username ");
$query->execute(array($username));
return $query->fetchALL(PDO::FETCH_ASSOC);
}
function filterTable(){
global $pdo;
$query=$pdo->prepare("Select from user where year(date) = $year and month(date) = $month ");
$query->execute();
return $query->fetchALL(PDO::FETCH_ASSOC);
}
?>
<table class="tbl_one">
<tr>
<th>Serial</th>
<th>Name</th>
<th>Age</th>
<th>BirthDate</th>
</tr>
<?php
$i=0;
$alluser = $user->getAllUser($username);
if(isset($_POST['view'])){
$alluser = $user->filterTable();
foreach($alluser as $user){
$i++;
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $user['name']; ?></td>
<td><?php echo $user['age']; ?></td>
<td><?php echo $user['birthdate']; ?></td>
</tr>
<?php } ?>
<?php
}else{
foreach($alluser as $user){
$i++;
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $user['name']; ?></td>
<td><?php echo $user['age']; ?></td>
<td><?php echo $user['birthdate']; ?></td>
</tr>
<?php } } ?>
</table>
Notice: Undefined variable: user in C:\xampp\htdocs\testSelect.php on line 85
Fatal error: Call to a member function getAllUser() on null in C:\xampp\htdocs\testSelect.php on line 85
The error says, that your $user variable is not defined. So you cant call a method on it.
Related
When I try to Search Between two dates all are working display on reports-details.php but When I try to fresh reload on reports-details.php show this error Warning:
Invalid argument supplied for foreach() in C:\xampp\htdocs\parking
reservation\admin\reports-details.php on line 52
reports.php content is:
<form action="reports-details.php" method="POST">
<label>From Date</label>
<input type="date" name="start_date">
<label>To Date</label>
<input type="date" name="end_date">
<button name="search">Search</button>
</form>
back-end-reports.php
<?php
include 'config.php';
class report extends Connection{
public function managereport(){
if (isset($_POST['search'])) {
$start_date = $_POST['start_date'];
$end_date = $_POST['end_date'];
$sqlselect = "SELECT * FROM tbl_customers WHERE test_date BETWEEN '$start_date' AND '$end_date' ORDER BY test_date";
$result = $this->conn()->query($sqlselect);
$result->execute();
return $result->fetchAll();
}
}
}
$new_vehicle = new report();
$new_vehicle->managereport();
?>
reports-details.php
<?php
include '../back-end/back-end-reports.php';
$result = new report();
$query = $result->managereport();
?>
<?php $id = 1; foreach($query as $row) { ?>
<tbody>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $row['serial']; ?></td>
<td><?php echo $row['fullname']; ?></td>
<td><?php echo $row['num_plate']; ?></td>
<td>View | <a class="text-dark" href="print.php">Print</a></td>
</tr>
</tbody>
<?php $id++; } ?>
Your issue here is the if statement in your function
if (isset($_POST['search'])) {
When you refresh your page there is no post data so the managereport returns nothing. The value of $query is therefore empty and so you cant iterate over it in the foreach loop.
My suggestion would be that your mangerreport() should return an empty array in the situation where there is no post data i.e
public function managereport(){
if (isset($_POST['search'])) {
$start_date = $_POST['start_date'];
$end_date = $_POST['end_date'];
$sqlselect = "SELECT * FROM tbl_customers WHERE test_date BETWEEN '$start_date' AND '$end_date' ORDER BY test_date";
$result = $this->conn()->query($sqlselect);
$result->execute();
return $result->fetchAll();
} else {
return array();
}
}
<?php $id = 1; foreach($query as $row) { ?>
When using the foreach loop, you must first judge the array $query, otherwise you will be prompted to report an error like this Invalid argument supplied for foreach() , you can add judgment before the loop,
<?php $id = 1; if (is_array($query) && count($query)) {
foreach($query as $row) { ?>
<tbody>
<tr>
<td><?php echo $id; ?></td>
<td><?php echo $row['serial']; ?></td>
<td><?php echo $row['fullname']; ?></td>
<td><?php echo $row['num_plate']; ?></td>
<td>View | <a class="text-dark" href="print.php">Print</a></td>
</tr>
</tbody>
<?php $id++; } } ?>
I'm having a problem where I made a website and I can successfully add data to every Student which works fine, however I'm trying to make a page, where I can basically search for the City(város in my php document) they live in, and it lists me all of those students data. I can succesfully insert into "város", however when I try to read the data, it doesn't work.
The Error message I get is:
SELECT * FROM Diák WHERE város = :város
SQLSTATE[HY093]: Invalid parameter number: parameter was not defined
my code is:
<?php
require "../config.php";
require "../common.php";
if (isset($_POST['submit'])) {
if (!hash_equals($_SESSION['csrf'], $_POST['csrf'])) die();
try {
$connection = new PDO($dsn, $username, $password, $options);
$sql = "SELECT *
FROM Diák
WHERE város = :város";
$város = $_POST['város'];
$statement = $connection->prepare($sql);
$statement->bindParam(':város', $város, PDO::PARAM_STR);
$statement->execute();
$result = $statement->fetchAll();
} catch(PDOException $error) {
echo $sql . "<br>" . $error->getMessage();
}
}
?>
<?php require "../../../htdocs/test/public/templates/header.php";?>
<?php
if (isset($_POST['submit'])) {
if ($result && $statement->rowCount() > 0) { ?>
<h2>Találatok</h2>
<table>
<thead>
<tr>
<th>Oktatási_id</th>
<th>Vezeték név</th>
<th>Keresztnév</th>
<th>Évfolyam</th>
<th>Születési dátum</th>
<th>Város</th>
<th>Utca</th>
<th>Házszám</th>
<th>Irányítószám</th>
<th>Szak</th>
<th>Kar</th>
</tr>
</thead>
<tbody>
<?php foreach ($result as $row) : ?>
<tr>
<td><?php echo escape($row["oktatási_id"]); ?></td>
<td><?php echo escape($row["vezeték_név"]); ?></td>
<td><?php echo escape($row["kereszt_név"]); ?></td>
<td><?php echo escape($row["évfolyam"]); ?></td>
<td><?php echo escape($row["születési_dátum"]); ?></td>
<td><?php echo escape($row["város"]); ?></td>
<td><?php echo escape($row["utca"]); ?> </td>
<td><?php echo escape($row["házszám"]); ?> </td>
<td><?php echo escape($row["irányítószám"]); ?> </td>
<td><?php echo escape($row["szak"]); ?> </td>
<td><?php echo escape($row["kar"]); ?> </td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php } else { ?>
<blockquote>Nem találtunk diákot, aki ilyen Városban lakna. <?php echo escape($_POST['város']); ?>.</blockquote>
<?php }
} ?>
<h2>Városon alapuló keresés.</h2>
<form method="post">
<input name="csrf" type="hidden" value="<?php echo escape($_SESSION['csrf']); ?>">
<label for="város">Város</label>
<input type="text" id="város" name="város">
<input type="submit" name="submit" value="View Results">
</form>
Vissza a fő oldalra.
<?php require "../../../htdocs/test/public/templates/footer.php"; ?>
Apparently PDO doesn't understand named parameters that contain accented characters (I guess anything outside 7-bit ASCII). So change :város to :varos.
I have a number of rows (ingredients) in a table in my database, I'm using the following code to iterate and populate my view with the name of each, but I want to be able to display the children/parents (columns in the DB) of each item, I'm able to log out the values correctly but when I try to echo as below I'm receiving "Message: Undefined index: parents" & "Message: Undefined index: children";
<?php foreach ($ingredient as $row => $key) { ?>
<tr>
<td><?php echo $row + 1; ?> </td>
<td><?php echo $key['name']; ?> </td>
<?php ChromePhp::log($key['parents']); ?>
<td><?php echo $key['parents']; ?> </td>
<?php ChromePhp::log($key['children']); ?>
<td><?php echo $key['children']; ?> </td>
<td>
</tr>
<?php } ?>
I'm confused as I have no issue logging it out.
I assumed it may be due to null values so I assigned child/parent a string value for each row as follows;
but the error persists.
Here is the logged output in the browser;
Output of <?php ChromePhp::log($key); ?>
EDIT: Here is how I'm building the $ingredients array;
<?php
function get_ingredient()
{
$qry = "SELECT CONCAT(UPPER(LEFT(i.name, 1)), LCASE(SUBSTRING(`name`, 2))) as name,i.id as id,'ingredient' as type, i.parents as parents, i.children as children FROM `ingredient` i WHERE i.is_del=0 order by i.name asc";
$qry = $this->db->query($qry);
if ($qry->num_rows() > 0) {
$ingr = $qry->result_array();
} else {
$ingr = array();
}
}
?>
I assume I'm missing something fundamental, any advise would be most appreciated. Cheers.
My array had two sets of data, I have resolved this by limiting the array to one set.
<?php foreach ($ingredient as $row => $key) { ?>
<tr>
<td><?php echo $row + 1; ?> </td>
<?php if(isset($key['name']) && !empty($key['name'])):?>
<td><?php echo $key['name']; ?> </td>
<?php endif;?>
<?php if(isset($key['parents']) && !empty($key['parents'])):?>
<?php ChromePhp::log($key['parents']); ?>
<td><?php echo $key['parents']; ?> </td>
<?php endif;?>
<?php if(isset($key['children']) && !empty($key['children'])):?>
<?php ChromePhp::log($key['children']); ?>
<td><?php echo $key['children']; ?> </td>
<?php endif;?>
</tr>
$key looks like an object so you have to access it with -> so to get value of name use $key->name,$key->parents
I was trying to get month and year from database when I click on the specific month from select option it should display month and as well as year, the result should fetch from database.
below is my code
<?php
$mon_arr=array("Jan"=>"01","Feb"=>"02","Mar"=>"03","Apr"=>"04","May"=>"05","Jun"=>"06","Jul"=>"07","Aug"=>"08","Sep"=>"09","Oct"=>"10","Nov"=>"11","Dec"=>"12");
?>
<select name="month" id="monthr">
<?php foreach($mon_arr as $x=>$xval){ ?>
<option value="<?php echo $xval; ?>" <?php if($m == $xval) echo "selected=selected"; ?>><?php echo $x; ?></option>
<?php } ?>
</select>
<?php //echo $xval; ?>
<?php //echo $m; ?>
<?php //echo $x; ?>
<select name="year" id="yearr">
<?php for($yi=2014;$yi<=2025;$yi++){ ?>
<option value="<?php echo $yi; ?>" <?php if($y == $yi) echo "selected=selected"; ?>><?php echo $yi; ?></option>
<?php } ?>
</select>
</div>
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Description</th>
<th>File</th>
<th>Submitted By</th>
<th>Date</th>
<th></th>
</tr>
</thead>
<tbody>
<?php $sel_month=mysql_query("SELECT * FROM ".TABLE_MONTHLY." WHERE pid='".$_GET['pid']."' ORDER BY m_date DESC ");
$f=new all_calls();
while($res_month=mysql_fetch_array($sel_month))
{
?>
<tr>
<td></td>
<td><?php echo $res_month['m_desc']; ?></td>
<td><?php $pathsi=explode(',',$res_month['m_file']);
for($ji=1;$ji<count($pathsi);$ji++) {
?>
Download<br />
<?php
}
?></td>
<td><?php echo $f->get_username($res_month['m_by']); ?></td>
<td><?php echo $res_month['m_date']; ?></td>
<td><i class="fa fa-pencil"></i></td>
</tr>
<?php } ?>
</tbody>
</table>
<?php
class monthly_report{
function validate_and_insert($data,$mid,$mfile)
{
//var_dump($mid); die;
if(empty($data['mdesc'])) $err='Description Required';
if(empty($err))
{
//echo 'hello'; die;
//var_dump($mid); die;
if(strlen($mid)==0)
{
$insert=mysql_query("INSERT INTO ".TABLE_MONTHLY." SET pid='".$_POST['hide_pid']."', m_desc='".$data['mdesc']."', m_file='".$mfile."', m_by='".$_SESSION['ID']."', m_date=CURDATE()");
if($insert)
{
return 0;
}
}
else
{
$a=new all_calls();
$paths=$a->get_monthly_file($mid);
if(strlen($mfile)==0)
{
$newfile=$paths;
}
else
{
$newfile=$paths.','.$mfile;
}
//var_dump($mid);die;
$update=mysql_query("UPDATE ".TABLE_MONTHLY." SET m_desc='".$data['mdesc']."', m_file='".$newfile."' WHERE mid='".$mid."' ");
if($update)
{
return 0;
}
}
}
}
}
?>
Can anyone help me out with this issue.
I have a table named current with several fields in that table like post, experience, company, etc.
I want to make a search having three select boxes one showing the posts from post field, 2nd showing the experience and 3rd showing the company from a single current table.
I want that if user selects from any one select field or from all it should search the respective data from the table and show the results only related to search.
For this I have written the code but its not working please help me in finding where did I went wrong, I cant understand.
It only shows data entered in post field but not in any other field.
My code goes here
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="">
<table>
<tr>
<?php
$ss=mysql_query("select * from current");
?>
<td><select name="post"><?php while($rr=mysql_fetch_array($ss))
{?><option value="<?php echo $rr['post'];?>"><?php echo $rr['post'];?></option><?php } ?></select></td>
<?php
$s11=mysql_query("select * from current");
?>
<td><select name="experience"><?php while($r11=mysql_fetch_array($s11))
{?><option value="<?php echo $r11['experience'];?>"><?php echo $r11['experience'];?></option><?php } ?> </select></td>
<td>
<?php
$sss=mysql_query("select * from current");
?>
<select name="company"><?php while($rrr=mysql_fetch_array($sss))
{?><option value="<?php echo $rrr['cname'];?>"><?php echo $rrr['cname'];?></option><?php } ?></select></td>
<td><input type="submit" name="search" value="Search" /></td>
</tr>
</table>
</form>
my search code goes here
<?php
include('Admin/config.php');
error_reporting(E_ERROR | E_WARNING | E_PARSE);
if(isset($_REQUEST['search']))
{
$post = $_REQUEST['post'];
$ex = $_REQUEST['experience'];
$company = $_REQUEST['company'];
$query = "select * from current where post like '%$post%' or experience like '%$ex%' or cname like '%$company%' ";
$res1 = mysql_query($query);
while($rows = mysql_fetch_array($res1))
{
?>
<tr>
<td><?php echo $rows['date'];?></td>
<td><?php echo $rows['post'];?></td>
<td><?php echo $rows['qualification'];?></td>
<td><?php echo $rows['experience'];?></td>
<td><?php echo $rows['nop'];?></td>
<td><?php echo $rows['noj'];?></td>
<td><?php echo $rows['cname'];?></td>
<td><?php echo $rows['jloc'];?></td>
</tr><?php
}}
else
{
$s=mysql_query("SELECT * FROM current ORDER BY date DESC");
while($rows=mysql_fetch_array($s))
{
?>
<tr>
<td><?php echo $rows['date'];?></td>
<td><?php echo $rows['post'];?></td>
<td><?php echo $rows['qualification'];?></td>
<td><?php echo $rows['experience'];?></td>
<td><?php echo $rows['nop'];?></td>
<td><?php echo $rows['noj'];?></td>
<td><?php echo $rows['cname'];?></td>
<td><?php echo $rows['jloc'];?></td>
</tr>
<?php
}}
?>
</table>
<?php
include('Admin/config.php');
error_reporting(E_ERROR | E_WARNING | E_PARSE);
$ss=mysql_query("select `post`,`experience`,`cname` from current");
$filter = array();
while($rr = mysql_fetch_assoc($ss)) {
foreach ($rr as $key => $val) {
$filter[$key][] = $val;
}
}
$request = array('post' => '', 'experience' => '', 'cname' => '');
if (isset($_REQUEST['search']))
{
$request = array(
'post' => $_REQUEST['post'],
'experience' => $_REQUEST['experience'],
'cname' => $_REQUEST['cname'],
);
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" name="">
<table>
<tr>
<?php
foreach ($filter as $name => $value) {
?>
<td>
<select name="<?=$name?>">
<?
foreach ($value as $key => $val) {
?>
<option value="<?=$val?>"<?= $val === $request[$name] ? ' selected' : ''?>><?=$val?></option>
<?
}
?>
</select>
</td>
<?
}
?>
<td><input type="submit" name="search" value="Search" /></td>
</tr>
</table>
</form>
<table>
<?php
if(isset($_REQUEST['search']))
{
$where = '';
foreach ($request as $key => $val) {
if ($where !== '') {
$where .= ' OR ';
}
$where .= "`".$key."` like '%".$val."%' ";
}
$query = "select * from current where ".$where;
}
else
{
$query = "select * from current ORDER BY date DESC";
}
$res1 = mysql_query($query);
while($rows=mysql_fetch_array($res1))
{
?>
<tr>
<td><?php echo $rows['date'];?></td>
<td><?php echo $rows['post'];?></td>
<td><?php echo $rows['qualification'];?></td>
<td><?php echo $rows['experience'];?></td>
<td><?php echo $rows['nop'];?></td>
<td><?php echo $rows['noj'];?></td>
<td><?php echo $rows['cname'];?></td>
<td><?php echo $rows['jloc'];?></td>
</tr>
<?php
}
?>
</table>