Retrieve links from an Access db with PHP - php

i'm having an issue listing up the links I have in an access database.
I'm new to this so even if i searched thru' the answered questions I couldn't find anything to help me out. I have a search form that when you type, it automatically filters the table i'm accessing from the databse. But i get this error when i'm accessing the field where I have the links listed: "Notice: Undefined index: Título in C:\xampp\htdocs\buscar.php on line 21"
Bassically what I have to do is a search form where you search in a category and all the pdfs related to that category would show up and should be clickable so you can read the one you're interested in.
The code I have is this, where Ser_presenta is the category and Título is where the url's are listed.
Index php page:
<!DOCTYPE html>
<html lang=es>
<head>
<meta charset="utf-8">
<title>Protocolos</title>
</head>
<body>
<link rel="stylesheet" href="css/styles.css?v=1.0">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"> </script>
<script type="text/javascript">
function buscarq() {
var buscarTxt = $("input[name='Buscar']").val();
$.get("buscar.php", {buscarVal: buscarTxt}, function(output){
$("#output").html(output);
});
}
</script>
<center>
<h2> Protocolos Médicos </h2>
<form action="protocolosphp.php" method="get" enctype="multipart/form-data">
<input type="text" name="Buscar" placeholder="Buscar protocolos..." onkeyup="buscarq();" size="50"/>
<input type="submit" value=">>" />
</form>
<div id="output">
</div>
</center>
</body>
</html>
Php that should retrieve data from the databse:
<?php
$conexion = odbc_connect ("ProtocolosIndex", "", "")
or die ('No se pudo conectar a la base de datos');
$output = '';
//collect
if(isset($_GET['buscarVal'])) {
$buscarq = $_GET['buscarVal'];
$query = odbc_exec($conexion, "SELECT * FROM tblRegistros WHERE Ser_presenta LIKE '%$buscarq%';") or die ("No se ha podido buscar");
$count = odbc_num_rows($query);
if($count == 0){
$output = 'No se han encontrado resultados';
}
else {
while($row = odbc_fetch_array($query)){
$depart = $row['Ser_presenta'];
$titulo = $row['Título'];
$output .= '<div>' .$depart. '<br />' .'$row->Título'. '</div>';
}
}
}
echo ($output);?>
Thanks a lot and sorry for the trouble.

Related

Ajax search results with on click result to download file

MY INDEX PAGE
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Some name</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
<div class="container">
<br />
<h2 align="center">Search by name</h2><br />
<div class="form-group">
<div class="input-group">
<span class="input-group-addon">Search</span>
<input type="text" name="search_text" id="search_text" placeholder="Enter model / search here" class="form-control" />
</div>
</div>
<br />
<div id="result"></div>
</div>
</body>
</html>
<script>
$(document).ready(function(){
load_data();
function load_data(query)
{
$.ajax({
url:"fetch.php",
method:"POST",
data:{query:query},
success:function(data)
{
$('#result').html(data);
}
});
}
$('#search_text').keyup(function(){
var search = $(this).val();
if(search != '')
{
load_data(search);
}
else
{
load_data();
}
});
});
</script>
AND MY fetch.php PAGE
which is used to get data from the database tables and output results.
I also added if the result is > 50 it will ask to enter few more characters because if I don't add if result > 50 then my page took 20sec to display all data because my database table has 25000 entries.
<?php
$connect = mysqli_connect("localhost", "root", "PASSWORD", "DATABASE");
if(isset($_POST["query"]))
{
$search = mysqli_real_escape_string($connect, $_POST["query"]);
$query = "
SELECT * FROM files
WHERE Name LIKE '%".$search."%'
";
}
else
{
$query = "
SELECT * FROM files ORDER BY ID
";
}
$result = mysqli_query($connect, $query);
if(mysqli_num_rows($result) < 500)
{
$output1 .= '
<div class="table-responsive">
<table class="table table bordered">
<div>
<th>Name</th>
<th>URL</th>
<th>Extension</th>
<th>Download</th>
</div>
';
while($row = mysqli_fetch_array($result))
{
$output2 .= '
<tr>
<td>'.$row["Name"].'</td>
<td>'.$row["URL"].'</td>
<td>'.$row["Extension"].'</td>
</tr>
';
}
if(mysqli_num_rows($result) > 0)
{
echo "$output1";
echo "$output2";
}
else
{
echo 'no results found';
}
}
else
{
echo 'Please enter few more charecters';
}
?>
I want href link for each of my results and on the click, it should download a file from ./"URL" column from a database table.
My database looks like this:
AND MY CURRENT PAGE IS http://mss1996.ddns.net:8008/files
I tried adding < a href=".$row["URL"]."> in outpur'array' but it destroys my page.
You should be able to easily add the URL field to form a valid URL if the contents of that field is indeed a valid URL. What I see missing form your code is http or https if it's an external link. If it's a relative link within the page then you're ok. Then add the </a> to close the link. So you'd form your table column like this:
echo '<td>' . $row["URL"] . '</td>';

Is there any way in PHP to store $variable of $variable in session

I created a web-page,where you can search plant's scientific name. Type plant name in search_text it will give you results in search_result(live search like google and facebook search bar) . Ex: when you will type C in search input, in search result you will get C related search. Like C typed in search input, in search result it will start showing Cucumber(Cucumis sativus), Capsicum(Capsicum annuum), etc.
Now I want when you will click on Cucumber(Cucumis sativus) in search result, it have to direct to home.php/Cucumber . Or when user click on Capsicum(Capsicum annuum), it have to direct on home.php/Capsicum .
And on home.php in body tag I want to display plant name with their scientific name. And in para tag information related to plant search result.
index.php
<!DOCTYPE html>
<html>
<head>
<title>Search</title>
<style type="text/javascript"
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </style>
<style type="text/javascript">
function searchq() {
var searchTxt = $("input[name='search']").val();
$.get("search.php", {searchVal: searchTxt}, function(output) {
$("#output").html(output);
});
}
</script>
</head>
<body>
<form action="index.php" method="post">
<input type="text" name="search" id="myInput1" autocomplete="off"
placeholder="Search username..." onkeydown="searchq(); " />
<input type="submit" value=">>"/>
</form>
<br>
<div id="output"></div>
</body>
</html>
search.php
<?php
$con = mysqli_connect('localhost', 'root');
mysqli_select_db($con, 'plant');
$output = '';
if (isset($_GET['searchVal'])) {
$searchq = $_GET['searchVal'];
$sql = "select * from type where plant like '%$searchq%'";
$query = mysqli_query($con, $sql) or die("could not search");
$count = mysqli_num_rows($query);
if ($count == 0) {
$output = 'There is no serach result';
} else {
while ($row = mysqli_fetch_array($query)) {
$plantname = $row['plant'];
$sciencename = $row['species'];
$id = $row['id'];
$output .= '<div>' . $plantname . ' ' . $sciencename . '</div>';
}
}
}
echo '<a herf="home.php/">' . $output . '</a></div>';
?>
There are many ways of doing this
Passing the name of the plant as a GET param is not an option?
You could do
echo "<a href='home.php?plantname={$plantname}' target='_blank'>{$output}</a></div>";
As the response of your server, that would create the link and in home.php you retrieve the plant name with $_GET['plantname'].
in home.php you do
if(isset($_GET['plantname'])){
echo $_GET['plantname'];
}
Please correct this line in index.php
<style type="text/javascript">
with
<script type="text/javascript">

ReferenceError: toastr is not defined

I´ve been trying to show toast notifications on my page with toastr.js, but this error keep showing:
The error
I already included jquery and both js and css, but didn´t anyway, this is the code:
EDIT: when i try to show the toast notifications directly on the html code, it works fine, but when i call them inside of the php function, the error keep appearing
<?php
// WHEN I TRY TO SHOW THE ALERT WITH THIS PHP FUNCTION, IT DOESN'T WORKS!
function cms_alert($message, $type) {
switch ($type) {
case "info":
echo "<script>toastr.info('INFO!');</script>";
break;
case "success":
echo "<script>toastr.success('SUCCESS!');</script>";
break;
case "warning":
echo "<script>toastr.warning('WARNING!');</script>";
break;
case "error":
echo "<script>toastr.error('ERROR!');</script>";
break;
}
}
?>
<?php
// THE ERROR IS HAPPENING HERE!
} else {
// Senão, mostra a página de login
if (isset($_POST['username'], $_POST['password'])) {
$username = $_POST['username'];
$password = ($_POST['password']);
// Caso deixe todos os campos em branco, uma mensagem de erro é mostrada
if (empty($username) or empty($password)) {
cms_alert("Preencha todos os campos!", "error");
} else {
if ($username == $admin_username and $password == $admin_password) {
// Usuário digitou login e senha corretos
$_SESSION['logged_in'] = true;
header('Location: index.php');
exit();
} else {
// Usuário digitou login e senha incorretos
cms_alert("Nome/senha esta incorreto!", "error");
}
}
}
?>
<html>
<head>
<meta charset="utf-8">
<title>tinyCMS - Login</title>
<link rel="stylesheet" href="../assets/style.css">
<link rel="stylesheet" href="../assets/w3.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.css">
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js"></script>
</head>
<body>
<center>
<h1><?php echo $site_name; ?></h1> <!-- Mostra o titulo do site -->
<h5><?php echo $site_description; ?></h5> <!-- Mostra a descrição do site -->
<hr/>
<h4>Digite seu login e senha para continuar:</h4>
<!-- LOGIN -->
<form action="index.php" method="post" autocomplete="off">
<input type="text" name="username" placeholder="Usuário" />
<input type="password" name="password" placeholder="Senha" />
<input type="submit" value="Login" />
</form>
</center>
<hr/>
<?php echo $site_footer_admin; ?>
</body>
<!-- WHEN I USE THE CODE DIRECTLY ON THE HTML FILE, IT WORKS FINE! -->
<script>toastr.error('Hello World!');</script>
</html>
You have whitespace in the link which causes a 404.
You put the javascript in a stylesheet tag and the CSS in a javascript tag.
You must include jQuery.
Try this:
<?php
function cms_alert($message, $type) {
switch ($type) {
case "info":
echo "<script>toastr.info('INFO!');</script>";
break;
case "success":
echo "<script>toastr.success('SUCCESS!');</script>";
break;
case "warning":
echo "<script>toastr.warning('WARNING!');</script>";
break;
case "error":
echo "<script>toastr.error('ERROR!');</script>";
break;
}
}
?>
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/toastr.min.js"></script>
</head>
<body>
<?php cms_alert("Hello World!", "success"); ?>
</body>
</html>

PHP MySQL Success Delete Message Always Shows

this is my HTML code:
<!doctype html>
<html lang="fr">
<head>
<meta charset="utf-8">
<title>supprimer un moyen</title>
<link rel="stylesheet" href="style.css">
</head>
<body class="example">
<br><br><br>
<form name="f" method="post" action=" supprimer_moyen.php ">
<center><b><h1>supprimer un moyen</h1> </b>
<label> &nbsp Entrer le code du moyen : </label>
<input type="text" name="code_moy" value="" placeholder="Code" required>
&nbsp <input id="gobutton" type="submit" name="sup" value="Supprimer"><br><br>
</center>
</form>
</body>
</html>
and this is my php code:
<?php
if (isset ($_POST['sup']))
{
$code_moy= $_POST["code_moy"];
$con=mysql_connect("localhost","root","") or die("Echec de connexion au serveur.".mysql_error());
mysql_select_db("ttp",$con) or die("Echec de sélection de la base.".mysql_error());
$sql = "delete from moyen_transport where ID='$code_moy'";
if (mysql_query($sql))
{
echo '<br>';
echo '<h1><center><font color="white"> suppression avec succès <font></center> </h1>';
echo '<center></center>';
}
else
{
echo '<br>';
echo '<h1><center><font color="red"> ce moyen n\'&eacutexiste pas <font></center> </h1> ';
echo '<center></center>';
}
mysql_close();
}
?>
My problem is that no matter what i enter in input the php result is always "suppression avec succès" even if the "ID" dosen't exist in my database!!!
First try using mysqli
second you can use mysqli_num_rows like that
<?php
if (isset ($_POST['sup'])){
$code_moy= $_POST["code_moy"];
$con=mysqli_connect("localhost","root","","ttp") or die("Echec de connexion au serveur.".mysqli_error($con));
$sql = "select * from moyen_transport where ID='$code_moy'";
$query = mysqli_query($con,$sql);
if(mysqli_num_rows($query) == 0){
//No id found
echo '<br>';
echo '<h1><center><font color="red"> ce moyen n\'&eacutexiste pas <font></center> </h1> ';
echo '<center></center>';
}else{
//id found
$sql = "delete from moyen_transport where ID='$code_moy'";
$query = mysqli_query($con,$sql)or die(mysqli_error($con));
echo '<br>';
echo '<h1><center><font color="white"> suppression avec succès <font></center> </h1>';
echo '<center></center>';}}
?>
Try checking if the returned value is empty
$result = mysql_query($sql);
if(!empty($result)){
//success code here
} else {
//fail code here
}

PHP MySQL display image from database

Hi I have form to make new article with informations and image. I susccesfuly save all info and image to database (i guess). And when I want to display info and image so only info works.
See in picture.
Any help? Thanks a lot
Inserting_post.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>APK Market</title>
<link rel="stylesheet" type="text/css" href="../style/style.css">
<link href="../bootstrap/css/bootstrap.css" rel="stylesheet" media="screen">
<script src="../bootstrap/js/bootstrap.min.js"></script>
<script src="../bootstrap/js/bootstrap.js"></script>
</head>
<body>
<form method="post" action="insert_post.php" enctype="multipart/form-data">
<div class="new_post">
<div class="headtitle">Insert new post</div>
<div class="new_title">
<p>New title</p>
<input type="text" name="title">
</div>
<div class="new_author">
<p>Author</p>
<input type="text" name="author">
</div>
<div class="new_keywords">
<p>Keywords</p>
<input type="text" name="keywords">
</div>
<div class="new_image">
<p>Image</p>
<input type="file" name="image">
</div>
<div class="new_content">
<textarea name="content" cols="20" rows="8"></textarea>
</div>
<div class="submit">
<input type="submit" name="submit" value="OK">
</div>
</div>
</form>
<script src="https://code.jquery.com/jquery.js"></script>
<script src="../bootstrap/js/bootstrap.js"></script>
</body>
</html>
<?php
include("../includes/connect.php");
if(isset($_POST['submit']))
{
$games_date = date('y-m-d-h');
$games_title = $_POST['title'];
$games_author = $_POST['author'];
$games_keywords = $_POST['keywords'];
$games_image = $_FILES['image']['name'];
$games_tmp = $_FILES['image']['tmp_name'];
$games_content = $_POST['content'];
if($games_title=="" or $games_author=="" or $games_keywords==""
or $games_content=="")
{
echo"<script>alert('any field is empty')</script>";
exit();
}
else
move_uploaded_file($games_tmp,"../uploaded_images/$games_image");
$insert_query= "insert into games(games_title,games_date,games_author,games_image,
games_keywords,games_content)
values ('$games_title','$games_date','$games_author','$games_image',
'$games_keywords','$games_cont ent')";
}
if(mysql_query($insert_query))
{
echo "<center><h1>Post published seccesfuly!</h1></center>";
}
?>
display page:
<div class="content">
<?php
include('connect.php');
$select_posts = "select * from games";
$run_posts = mysql_query($select_posts);
while($row=mysql_fetch_array($run_posts))
{
echo '<p class="games_title_result">' .$games_title = $row['games_title'];
echo '<p class="games_image_result"><img src="<?php echo $row["games_image"];?>';
echo '<p class="games_content_result">' .$games_content = $row['games_content'];
echo '<p class="games_date_result">' .$games_date = $row['games_date'];
echo '<p class="games_author_result">' .$games_author = $row['games_author'];
}
?>
</div>
Here's my answer:
echo '<p class="games_image_result"><img src="uploaded_images/'.$row["games_image"].'" />';
You got a syntax error where you include a <?php and ?> inside your php, and on your echo at that.
So when you look at your img's src, it would have those.
Also, you forgot to close your img tag and your other p tags.
Your image tag isn't closed properly and the path to the image is not complete. You are only specifying the filename. I am not sure about your directory structure
echo '<p class="games_image_result"><img src="uploaded_images/<?php echo $row["games_image"];?>" />';
Display Image from DataBase using php
# SQL Statement
$sql = "SELECT `idimage` FROM `yourTableName` WHERE id=" . mysqli_real_escape_string($_GET['id']) . ";";
$result = mysqli_query("$sql") or die("Invalid query: " . mysqli_error());
# Set header
header("Content-type: image/your image name which type of your image");
echo mysqli_result($result, 0);
}
else
echo 'Please check the ID!';
?>
I wish it's may be help you

Categories