How to make link of the database in PHP - php

Hi I want to show URL as a link in PHP the URL is shown by query from database but it is not a link so I want to make it link like using but I don't know what I am doing wrong
My data show like this in browser
ID Name URL
2 This localhost/p_uploads/00.jpg
3 Nissan localhost/p_uploads/7a.jpg
I want these URL's to be link so anyone can click on the url to open the image
Here is my PHP Code:
<?php
if(!isset($_COOKIE['loggedin'])){
header("location:index.php");
}
session_start();
if(!isset($_SESSION['user'])){
header("location: index.php");
}
else {
?>
<?php
$con=mysqli_connect("localhost","root","123","user");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"Select * from private_uploads where username = '".$_SESSION['user']."'")
or die(mysql_error());
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Name</th>
<th>URL</th>
</tr>";
while($row = mysqli_fetch_array($result, MYSQL_ASSOC))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>",'<a href=' . $row['Link'] . '></a>',"</td>";
echo "</tr>";
}
echo "</table>";
//Views Counter
mysqli_close($con);}
?>
<?php
$url = htmlspecialchars($_SERVER['HTTP_REFERER']);
echo "<a href='$url'>back</a>";
?>

replace
echo "<td>",'<a href=' . $row['Link'] . '></a>',"</td>";
for this
echo '<td>[a name here would be nice]</td>';
you are missing the "" in the generated href

You're producing links that look like this:
That's both an invalid URL (unless you really do have a localhost/p_uplaods folder) and a link with no body, so you'll never see it.
If you want to access localhost the host, and not the folder, you should use use an absolute path on the current host, /p_uploads/00.jpg, or a fully qualified URL: http://localhost/p_uploads/00.jpg.

Your link looks like this:
It doesn't have a HTTP protocol. The link should look like this:

You might have a problem on this line:
echo "<td>",'<a href=' . $row['Link'] . '></a>',"</td>";
As you are referring to Link whereas in your database the column is called URL. Also add a text to the tag:
echo '<td>View</td>';

Related

Add sqlite entry Using PHP

I am running a simple code here to insert a set of entries in a sqlite table.
class MyDB extends SQLite3
{
function __construct()
{
$this->open('new_test.db');
}
}
echo "opened database succesfully";
$db = new MyDB();
$db->exec("INSERT INTO RECORDSS(ID,NAME,DEVICE,PROJECT,IP,COMMENT) VALUES('$id','$name','$device','$project','$ip','$comment');");
$ret = $db->exec($sql);
if(!$ret) {
echo "No error in if statement";
echo $db->lastErrorMsg();
The program executes without any error. However when I open the data base. I see no entries in the table.
What I am doing wrong here ?
Here is my code to view the table.
class MyDB extends SQLite3 {
function __construct() {
$this->open('new_test.db');
}
}
$db = new MyDB();
$sql =<<<EOF
SELECT * from RECORDSS;
EOF;
$ret = $db->query($sql);
echo "<table style='width:100%'>
<tr>
<th> ID </th>
<th> NAME </th>
<th> DEVICE </th>
<th>PROJECT </th>
<th>IP</th>
<th>COMMENT</th>
</tr>";
while($row = $ret->fetchArray(SQLITE3_ASSOC))
{
echo "<tr>";
echo "<td>" . $row['ID']. "</td>" ;
echo "<td>" . $row['NAME']. "</td>";
echo "<td>" . $row['DEVICE']. "</td>";
echo "<td>" . $row['PROJECT']. "</td>";
echo "<td>" . $row['IP']. "</td>";
echo "<td>" . $row['COMMENT']. "</td>";
echo "</tr>";
}
echo "Operation done successfully";
$db->close();
echo "</table>"
Couple of things that I have checked.
The table and database are created, in the same directory with executable permissions.
Tried to replace PHP function with python, to achieve the same objective.
Do I have to change something explicitly in the PHP, apache or sqlite Conf files ?
EDIT:- Adding a few details as asked in the comments:
SCRIPT:
called using <form action ="add_info.php" method="get">
The form contains:
`Name
<input type ="text" name ="name"/>
<br/>
Project
<input type ="text" name ="device"/>
<br/>
`
Not pasting all form fields to make it readable. They are similar.
PHP gets the variable as
$name= $_GET['name']
OS: Uuntu 16.04 server
FIle: All scripts stored under /var/www/html
I dont exactly know about 'file systems'
I got it working.
The issue was because of file permissions and ownership.
The apache process was run by user www-data. My files were were run under root. Hence giving write permissions to root did not help.
Here is what I did.
change the user of the directory to www-data using chown -hR www-data /var/www/html.
Gave read and write permissions to the database and PHP file.
Thanks for all the helpful comments.

echo my images on my index page PHP

i built a CMS and my image shows succesfully in the admin page(back-end) once i upload an image with the form but once on the main index page(front-end) it shows that "broke image link"
here is my function to post the uploaded image on my admin page:
function gettestimony() {
$query = mysql_query("SELECT * FROM testimony") or die(mysql_error());
while($post = mysql_fetch_assoc($query)){
echo "<img src =\"" . $post['photo']."\">";
echo "<p>" . $post['imagename'] . "</p>";
echo "<br>";
echo "<p>" . $post['test'] . "</p>";
echo "<br>";
echo "<p>" . $post['author'] . "</p>";
echo "<br>";
echo "Delete";
echo "Edit";
echo "<br>";
}
}
and the directory is htdoc/blah/admin/include/functions.php
and the image is saved in htdoc/blah/admin/image/
for my main index, this is the function to post it:
function gettestimony() {
$query = mysql_query("SELECT * FROM testimony") or die(mysql_error());
while($post = mysql_fetch_assoc($query)){
echo "<div class='column second'>";
echo "<p>" . $post['test'] . "</p>";
echo "<img src =\"" . $post['photo']."\">";
echo "</div>";
}
}
this is the code i use in both functions:
echo "<img src =\"" . $post['photo']."\">";
so like i said it works fine in admin page but my main page it doesn't....someone please help
Your image's path relative to the path of your function is irrelevant. As far as that function is concerned the path to the image is just a string.
You need to make sure the path to the image is correct relative to the HTML file that's viewing it. The easy way to make it work is to just use the full URL to the image:
www.something.com/admin/images/myimage.php

How to Displaying an image with path stored in Database?

I have stored images path in database and images in project folder. Now i am trying to view all IMAGES with their PRODUCTS in my HTML template. I have written the following code and its given me an empty images box in the output and when i open that image box in the new tab, it opens the following URL.
http://localhost/cms/1
Kindly tell me the way of referring images in 'img src ' tag.
include 'connect.php';
$image_query = "select * from product_images";
$image_query_run = mysql_query($image_query);
$image_query_fetch = mysql_fetch_array($image_query_run);
if (!$query=mysql_query ("select product_id,name from products")) {
echo mysql_error();
} else {
while ($query_array = mysql_fetch_array($query)) {
echo '</br><pre>';
$product_id = $query_array['product_id'];
echo "<a href='single_product.php?product_id=$product_id' >";
print_r ($query_array['name']);
echo "</a>";
echo "<img src=". $image_query_fetch
['images'] ." alt=\"\" />";
echo '</pre>';
}
}
} else {
echo "You need to Log in to visit this Page";
}
Add your local image path before source
example
echo "<img src='http://localhost/cms/1/". $image_query_fetch['images'] .'" alt=\"\" />";
*Use PHP *
<?php echo "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?>
You can use the HTML base tag to set a base URL that relative URLs will be resolved from.
See -> http://www.w3schools.com/tags/tag_base.asp

Showing PHP in a PhoneGap based App

I'm trying to show the results of the php in my app, when I test it outside of the app, it works fine, and I have tried many solutions out there to load it with js, ajax, etc. but can't get it to work. Any ideas?
The php gets data from MySql database
-----select.php------
<?php
$con=mysqli_connect("localhost","localhost","","test");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
/*while($row = mysqli_fetch_array($result))
{
echo $row['FirstName'] . " " . $row['LastName'];
echo "<br />";
}*/
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Phonegap doesn't have any concept of PHP, you are unable to run PHP in a PhoneGap application. You will need to make AJAX requests from your application to your server for interaction.
The reason it works fine out of the app, is that you're probably running it in the browser straight on the server.
Take a look at jQuery if you're new to it all, especially $.ajax().
Phone gap does not support php, so will be good, if you want non static content... if you have a API which it will provide the content for you application. epiphany it is a really good open source framework created by Jaisen. This will give you an good start to create a good API for it. You can also learn about Zend Frameworks which it is more robust for big applications.

how to use urlencode( ) in my example?

I checked php.net and read a few examples of how urlencode( ) works but somehow I just can't get it right. Can someone give me a hand?
it'll be a lot to example so hopefully my brief example would make sense.
I have a page called 2.php and it was called to show some contents of a .txt file choosen in 1.php.
I am told to make a link for 3.php and the link should look something like /3?filename=a.txt
with filename as GET parameter name and Ensure GET parameter value is urlencoded using the urlencode( ) function.
but I'm confused how and where I should put urlencode() to make it work.
I'll paste my 2.php code here...I simplified the codes a bit...
<?php
$fileContents = file("./aaa/" . $_GET["course"] . ".txt");
echo "<table border=\"1\">";
foreach($fileContents as $row)
{
echo "<tr>";
$contents = preg_split("/,/", $row);
foreach($contents as $eachline)
{
echo "<td>";
if(!(preg_match("/#/", $eachline)))
{
echo trim(ucfirst($eachline));
}
else
{
echo trim(strtolower($eachline));
}
echo "</td>";
}
echo "</tr>";
}
echo "</table>";
echo "<a href='./1.php'>Choose another txt file</a><br/>";
echo "or<br/>";
echo "<a href='.3.php?'>Work with this txt file</a>";
?>
BUT…the 3.php option must have a query string appended to it: the name of the text file that was selected in 1, so instead of ./3.php, the url should be something such as ./3?filename=asdf.txt
Use “filename” as the GET parameter name. Ensure the GET parameter value is urlencoded using the urlencode( ) function.
but I'm just not sure how to get it to work....
You can wrap the part that should be url encoded in the function within the string:
$url = 'http://www.google.com?q=' . urlencode($search);
OR in html
http://www.google.com?q=<?php echo urlencode($search); ?>
Where . is the concatenation of 2 outputs.

Categories