When adding a name with a space in the search bar the header and the footer remains however the body disappears.
I even added ini_set('display_errors', '1') and ini_set( 'default_charset', 'UTF-8' ); to check for errors however that didn't help.
Please note that the function works fine except when when entering any spaces
Here is the code:
<?php
//ini_set('display_errors', '1');
//ini_set( 'default_charset', 'UTF-8' );
session_start();
$title="Search";
include('includes/connection.php');
$url=parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH);
$url_array=explode("/",$url);
$name=end($url_array);
$uname = urldecode($name);
$position="absolute";
$sql ="SELECT userName , firstName, lastName,id
FROM users
WHERE userName LIKE ?
or firstName like ?
or lastName Like ?
or concat(firstName,' ',lastName) = ?";
$params=array("$uname","$uname","$uname","$uname");
$query= $dbh -> prepare($sql);
if($query-> execute($params)){
$results=$query->fetchAll(PDO::FETCH_OBJ);
if($query->rowCount() > 6) {
$position="inherit";
} else {
$position="absolute";
}
}
if (!$results) {
$mess = true;
// echo '<script type="text/javascript">alert("Hi");</script>';
}
//else {
//$mess = true;
//$query->errorInfo();
//}
include('includes/header.php');
//else
//{
// $mess = $query->errorInfo();
// echo '<script type="text/javascript">alert("'.$mess[2].'");</script>';
//}
?>
<main role="main" class="mbr-fullscreen" style="display: grid;position: <?php echo $position;?>;">
<div class="album py-5" >
<div class="container">
<div class="row">
<?php
if(!$results) {
echo'<div class="col-lg-12 mx-auto">
<center>
<h1 style="padding: 10px; color: #575957;">No results found</h1>
<h6>We couldn\'t find any user matching your search</h6>
<a href="/index.php" class="btn btn-primary display-4" >Search Again</a>
</center>
</div> ';
}
?>
<?php
foreach($results as $result) {
?>
<div class="col-md-4">
<div class="card mb-4 shadow-sm">
<div class="card-body">
<h3 class="card-text text-center" style="text-transform: capitalize;"><?php echo htmlentities($result->userName);?></h3>
<p class="text-center"> <?php echo htmlentities ($result->firstName).' '; echo htmlentities ($result->lastName); ?> </p>
<div class="d-flex justify-content-between align-items-center">
<div class="btn-group mx-auto mbr-form">
View
</div>
</div>
</div>
</div>
</div>
<?php
}
?>
</div>
</div>
</div>
</main>
<?php include('includes/footer.php');?>
Like statement does not work like that. You should change your query in the proper format.
Replace:
$params=array("$uname","$uname","$uname","$uname");
With:
$params=array("%$uname%","%$uname%","%$uname%","%$uname%");
1. Add $uname=str_replace(" ", "% %", $uname); after $uname = urldecode($name); to wrap each space with %
2. Replace concat(firstName,' ',lastName) = ? With concat(firstName,' ',lastName) like ?
3. Follow Tahasin instructions, Replace $params=array("$uname","$uname","$uname","$uname"); with $params=array("%$uname%","%$uname%","%$uname%","%$uname%");
I solved the problem by adding the following line in the home page
search = search.replace(/\s/g,"-");
And in my search page, I added the following code
$name=str_replace('-',' ',$uname);
You people are great, thank you for helping me out
Related
I need help with my PHP script.
I have 3 Tables in my Database (its German so sorry if you dont understand ^^):
-firma (in english company)
-produkt (in english product)
-firmaprodukt (in english companyproduct)
If I click on a company, the product image should be output (from the "Produkt" database) and the appropriate URL (from the "firmaprodukt").
So far I've managed that when you click on a company, the ID is displayed (example: "localhost/TESTING//index?1=") but I'm struggling with the output.
That's my index.php script for the output:
<div class="col-xl-6">
<div id="login_content">
<div class="scrollP">
<?php
$fpresults = $fpCrud->getPicUrl($FirmenID);
?>
<div class="col-12 d-flex justify-content-center heading-div">
<h3> <?= $result['firmenname']?> </h3>
</div>
<table border="0">
<tbody>
<?php
foreach ($fpresults as $fpresult){
?>
<tr>
<div class="box customer-box" data-parent="#login_card" id="project_reg_box">
<img src="dashboard/TESTING/<?= $fpresult['ProduktLogo']?>" height="100%" >
</div>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
</div>
</div>
And that's in my crud.php:
public function getPicUrl($FirmenID) {
$stmt = $this->conn->query(
"SELECT fp.FirmenID, f.firmenname, p.ProduktLogo, fp.url
FROM firma f
JOIN firmaprodukt fp ON fp.FirmenID = f.FirmenID
JOIN produkt p ON fp.ProduktID = p.ProduktID
WHERE fp.FirmenID = :FirmenID");
$stmt->execute(array(':FirmenID' => $FirmenID));
$data = $stmt->fetch();
return $data;
}
In my formprocess.php I don't have anything for that.
My solution:
index.php
<?php
if (isset($_GET['firmaprodukte'])) {
$FirmenID = (int) $_GET['firmaprodukte'];
$firmaprodukte = $fpCrud->getPicUrl($_GET['firmaprodukte']);
$firmenresults = $firmaCrud->getFirma($_GET['firmaprodukte']);
?>
<div class="col-xl-6">
<div class="scrollP">
<div id="login_content">
<div id="login_card" class="container">
<?php
$fpresults = $fpCrud->getPicUrl($FirmenID);
?>
<div class="col-12 d-flex justify-content-center heading-div">
<h3> <?= $firmenresults['firmenname']?> </h3>
</div>
<table border="0">
<tbody>
<?php
foreach ($fpresults as $fpresult){
?>
<tr>
<div class="box customer-box" data-parent="#login_card" id="project_reg_box">
<img src="dashboard/TESTING/<?= $fpresult['ProduktLogo']?>" height="80vh" max-width="200vh">
</div>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
</div>
<?php } ?>
Crud.php
public function getPicUrl($FirmenID) {
$stmt = $this->conn->prepare("SELECT fp.FirmenID, f.firmenname, p.ProduktLogo, fp.url FROM firma f JOIN firmaprodukt fp ON fp.FirmenID = f.FirmenID JOIN produkt p ON fp.ProduktID = p.ProduktID WHERE fp.FirmenID = :FirmenID");
$stmt->execute(array(':FirmenID' => $FirmenID));
$data = $stmt->fetchAll();
return $data;
}
formprocess.php
if(isset($_POST['firmaprodukte'])) {
$FirmenID = $_POST['FirmenID'];
if($crud->getPicUrl($FirmenID)) {
header('location: FirmaProdukt.php');
exit;
} else{
header('location: FirmaProdukt.php');
exit;
}
}
So i want to place this code below
$keyword="";
if (isset($_POST['searchpakaian'])) {
$keyword = $_POST['searchpakaian'];
}
$query = mysqli_query($conn,"SELECT * FROM tbl_pakaian WHERE nama_pakaian LIKE '%".$keyword."%' ORDER BY id ASC limit 0,6");
$hitung_data = mysqli_num_rows($query);
if ($hitung_data > 0) {
while ($row = mysqli_fetch_array($query)) {
?>
<div class="col">
<div class="card h-100">
<div class="card-img-wrap ">
<img src="img/Galeri/pakaian/<?php echo $row['gambar_pakaian'] ?>" class="card-img-top" alt="...">
</div>
<div class="card-body">
<h5 class="card-title"><?php echo $row['nama_pakaian'] ?></h5>
<p class="card-text "><?php echo $row['keterangan_pakaian'] ?></p>
</div>
</div>
</div>
<?php } } else { ?>
<center><h4>Tidak Ada Data</h4></center>
<?php } ?>
On if conditional statement
if($req == 'something'){
}
Here is my full code
<?php
include '../koneksi.php';
$req = $_REQUEST['req'];
$keyword="";
if (isset($_POST['searchpakaian'])) {
$keyword = $_POST['searchpakaian'];
}
$query = mysqli_query($conn,"SELECT * FROM tbl_pakaian WHERE nama_pakaian LIKE '%".$keyword."%' ORDER BY id ASC limit 0,6");
$hitung_data = mysqli_num_rows($query);
if ($hitung_data > 0) {
while ($row = mysqli_fetch_array($query)) {
?>
<div class="col">
<div class="card h-100">
<div class="card-img-wrap ">
<img src="img/Galeri/pakaian/<?php echo $row['gambar_pakaian'] ?>" class="card-img-top" alt="...">
</div>
<div class="card-body">
<h5 class="card-title"><?php echo $row['nama_pakaian'] ?></h5>
<p class="card-text "><?php echo $row['keterangan_pakaian'] ?></p>
</div>
</div>
</div>
<?php } } else { ?>
<center><h4>Tidak Ada Data</h4></center>
<?php } ?>
If i just copy the code to the if conditional statement code, it just say 'unexpected end of file'
What should i do?
The syntax error are vey simple to find just confirm that when you copy inside the if, dont repeat php keys as and the echo finish it with ;
<?php echo('hello world'); ?>
OR the same echo but simple deleting the php tag by = simbol (only work with echo)
<?= 'hello world' ?>
after adding some PHP code the code below that is getting removed. if I remove the PHP part then it is working fine. even in the source code the below HTML code is not showing
<div class="row data">
<div class="row info">
<div class="datatitle">Education</div>
</div>
<div class="grad-fetch row">
<?php
session_start();
$hostname = "127.0.0.1";
$username = "root";
$db_password = "";
$db_name = "resume";
$conn = mysqli_connect($hostname, $username, $db_password, $db_name);
if(!$conn){
die("connection failed : ".mysqli_connect_error());
}
/* $sql = graduation(degree_status ,college ,start_year ,end_year ,degree ,stream ,performence_scale ,performence_marks)*/
$sql = 'SELECT * FROM graduation ';
$result = mysqli_query($conn, $sql);
if (!$result) {
die("Error: " . $sql . "<br>" . mysqli_error($conn));
}
while ($row=mysqli_fetch_array($result)) {
$deg= $row['degree'];
$str= $row['stream'];
$str_yr= $row['start_year'];
$end_yr= $row['end_year'];
$clg= $row['college'];
$prf_scl= $row['performence_scale'];
$prf_mrk= $row['performence_marks'];
}
?>
</div class="row">
<h5>
<?php
echo $deg;
echo $str;
echo $str_yr;
echo $end_yr;
?>
</h5>
<div>
<?php
echo $clg;
?>
</div>
<div>
<?php
echo $prf_scl;
echo $prf_mrk;
?>
</div>
<div>
<?php
exit;
mysqli_close($conn);
?>
</div>
<div class="row dialouge"><h4>Tell us bit about your education</h4></div>
<div class="col userinfo">
<div class="row discipline" data-toggle="modal" data-target="#graduation-modal" id="grad">Graduation
<i class="fa fa-plus education-fa-plus fa-x" aria-hidden="true"></i>
</div>
<div class="row discipline" data-toggle="modal" data-target="#hs-modal">XII(Higer Secondary)
<i class="fa fa-plus education-fa-plus fa-x" aria-hidden="true"></i>
</div>
<div class="button-container">
PREVIOUS
</div>
</div>
if I'm not adding the PHP code then the all the contents are showing but after adding the below part is not showing. I don't understand what is wrong here
<?php
exit;
mysqli_close($conn);
?>
You need to remove exit from your code as it will stop the execution right there.
I want to output something like this:
viewlogs.php?id=1&EmployeeNo=15000000600
However, I'm not sure what condition I should be using. I have two separate database to call, one is on MySQL which is easily taken to the localhost, and MSSQL which I'm not very familiar with.
I'm trying a logic where I use && within a while loop.
My code works something like this:
<?php
session_start();
if (!isset($_SESSION['username']))
{
header('location: login.php');
die();
}
?>
<?php
try {
include ("config.php");
include ("sqlsrv.php");
}catch(Exception $e){
die("ERROR:".$e->getMessage());
}
if(isset($_POST['usn']) && $_POST['usn']!=""){
$res = $conn->prepare("SELECT EmployeeNo, FirstName, MiddleName, LastName, DateHired, ResignationDate FROM TA3.dbo.Employees");
$res->execute();
$req = $db->prepare("SELECT * FROM students WHERE usn LIKE :usn");
$req->execute(array(
'usn'=>'%'.$_POST['usn'].'%'));
if ($req->rowCount()==0 && $_SESSION['type']=="Super_Admin") { ?>
<span class="notfound">Student not found? <a class="addstud" href="create.php">add student?</a></span>
<?php
}
elseif ($req->rowCount()==0 && $_SESSION['type']=="Admin") { ?>
<span class="henhen">Student not found</span>
<?php
}
else{
while ($data=$req->fetch() && $outcome=$res->fetch()){
?>
<div class="infohen">Info </div>
<div class="uy">
<div class="name"><?php echo $data['fname']." ".$data['mname']." ".$data['lname']; ?></div>
<div class="email"><?php echo $data['email']; ?></div>
<div class="usn"><?php echo $data['usn']; ?></div>
<div class="schedule"><?php echo $data['schedule']; ?></div>
<div class="strand"><?php echo $data['strand']; ?></div></div>
<?php if ($_SESSION['type']=="Super_Admin")
{ ?>
<div class="gridme">
<div class="action-list">
<div class="action">Action</div>
<div class="edit"> Edit</div>
<div class="logs">Logs</div>
<?php } else { ?>
<div class="gridme">
<div class="action-list">
<div class="action">Action</div>
<div class="logs">Logs</div>
<?php } ?>
</div>
</div>
</div>
</div>
<?php
}
}
}else { ?>
<span class="message">Enter USN!</span>
<?php
}
?>
<!-- <div class="enable"> <a onclick="return confirm('Are you sure you want to Enable student?')" href="enable.php?id=<?= $data['id']?>"> Enable </div>
<div class="disable"> <a onclick="return confirm('Are you sure you want to disable student?')" href="disable.php?id=<?= $data['id']?>">Disable</a>
</div> -->
<!-- </div>
<div class="status-list">
<div class="status">Status</div>
<div class="active">Active</div>
</div> -->
If I use while ($data=$req->fetch() && $outcome=$res->fetch()), the details called from $data are all gone.
I'm having trouble with logic since I'm not very good at it.
If anyone might need it, I found a solution.
I just needed to use open and close parenthesis.
while (($data=$req->fetch()) && ($outcome=$res->fetch()))
I've been coding PHP for 2 weeks (it's not pretty) and I have not been able to find the answer to my question. I want an admin type user to be able to fill a form and post it to a page where base level users can view the content. I've gotten all of this to work like a charm, but my dilemma is to allow the admin user to include an image as well. Maybe I just don't know what to search for.
Here is the php code and the form for the admin user page:
<?php
error_reporting(E_ALL & ~E_NOTICE);
session_start();
include_once("connection.php");
if (isset($_SESSION['adminid'])) {
$adminid = $_SESSION['adminid'];
$adminemail = $_SESSION['adminemail'];
if ($_POST['submit']) {
$title = $_POST['title'];
$deadline = $_POST['deadline'];
$content = $_POST['content'];
$sql_blog = "INSERT INTO blog (title, deadline, content, logoname) VALUES ('$title', '$deadline', '$content', '$logoname')";
$query_blog = mysqli_query($dbcon, $sql_blog);
echo "<script>alert('Your inquiry has been posted')</script>";
}
} else {
header('Location: index.php');
die();
}
$sql = "SELECT adminid, adminemail, adminpassword, adminname FROM admin WHERE adminemail = '$adminemail' LIMIT 1";
$query = mysqli_query($dbcon, $sql);
if ($query) {
$row = mysqli_fetch_row($query);
$adminname = $row[3];
}
?>
and here is the code for the base level user page: (i commented out the image block where I want the admin's image to be shown.
<main>
<div class="container">
<div class="row topbuffpost">
<h1>business inquiries</h1>
<hr>
<?php
include_once('connection.php');
$sql = "SELECT * FROM blog ORDER BY id DESC";
$result = mysqli_query($dbcon, $sql);
while ($row = mysqli_fetch_array($result)) {
$title = $row['title'];
$content = $row['content'];
$date = strtotime($row['deadline']);
?>
<div class="col-md-4 col-lg-3">
<div class="card hoverable">
<!-- <div class="card-image">
<div class="view overlay hm-white-slight z-depth-1">
<img src="">
<a href="#">
<div class="mask waves-effect">
</div>
</a>
</div>
</div> -->
<div class="card-content">
<h5> <?php echo $title; ?> <br/> <h6>Deadline |<small> <?php echo date("j M, Y", $date); ?> </small> </h6></h5> <br/>
<p> <?php echo $content; ?> </p>
<div class="card-btn text-center">
Read more
<i class="fa fa-lightbulb-o"></i>  propose a plan
</div>
</div>
</div>
</div>
<?php
}
?>
</div>
</div>
</main>
All of this works perfectly, I just can't figure out how to have an image display in the same way as the title, deadline, and content. Youtube wont help either, too much outdated php + I haven't been coding long enough to really work things out on my own.
You can save all user images under a folder (let's call /images/user) and record the file name into database.
if ($_POST['submit']) {
$title = $_POST['title'];
$deadline = $_POST['deadline'];
$content = $_POST['content'];
$logoname = basename($_FILES["fileToUpload"]["logoname"]; // <-- Make sure your form is ready to submit an file
// Update below as per your need.
$target = 'images/users/' . $logoname;
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target);
$sql_blog = "INSERT INTO blog (title, deadline, content, logoname) VALUES ('$title', '$deadline', '$content', '$logoname')";
$query_blog = mysqli_query($dbcon, $sql_blog);
echo "<script>alert('Your inquiry has been posted')</script>";
}
You can then display the image your page
<main>
<div class="container">
<div class="row topbuffpost">
<h1>business inquiries</h1>
<hr>
<?php
include_once('connection.php');
$sql = "SELECT * FROM blog ORDER BY id DESC";
$result = mysqli_query($dbcon, $sql);
while ($row = mysqli_fetch_array($result)) {
$title = $row['title'];
$content = $row['content'];
$date = strtotime($row['deadline']);
$logoname = 'images/user/' . $row['logoname'];
?>
<div class="col-md-4 col-lg-3">
<div class="card hoverable">
<div class="card-image">
<div class="view overlay hm-white-slight z-depth-1">
<img src="<?php echo $logoname; ?>">
<a href="#">
<div class="mask waves-effect">
</div>
</a>
</div>
</div>
<div class="card-content">
<h5> <?php echo $title; ?> <br/> <h6>Deadline |<small> <?php echo date("j M, Y", $date); ?> </small> </h6></h5> <br/>
<p> <?php echo $content; ?> </p>
<div class="card-btn text-center">
Read more
<i class="fa fa-lightbulb-o"></i>  propose a plan
</div>
</div>
</div>
</div>
<?php
}
?>
</div>
</div>
</main>