Add sqlite entry Using PHP - 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.

Related

Jump to Another Page After Generate Excel File in PHP

I'm making a form (form.php) for user to insert data, after user finish inserting data, form action will jump to formGenerateXLS.php (using POST method) for generate user data to XSL file. In the bottom of formGenerateXLS.php I put a javascript code to jump to dashboard (home.php) but it fail. The Excel file successfully generate but javascript code not working. How to work with that?
if(isset($_POST['issue_time'])){
$product_desc = $_POST['product'];
$filename_excel = date("Ymd_")."$product_desc";
header("Content-type: application/vnd-ms-excel");
header("Content-Disposition: attachment; filename=$filename_excel.xls");
echo "<table border='1'>";
echo "<tr>";
echo "<th colspan='3' bgcolor='#1bf3b3'>New Ticket Request</th>";
echo "</tr>";
echo "<tr>";
echo "<td>Name</td>";
echo "<td></td>";
echo "<td>User</td>";
echo "</tr>";
echo "</table>"
}
echo "<script>window.location='home.php'</script>";
Because echo does not execute the command, but simply writes it to the file.
In order to reposition a php file, use the header () function, for example header ('location: home.php');.

images are not displaying in web page when i retrieve them from server folder

I'm trying to retrieve all images from a folder in the server to my web page through PHP.
At local server, they're displaying fine but at main server, images are not displaying and showing error 404 not found
even though the images are there.
Please help me to get rid of this. The images are in Big Rock server.
Here is my code:
$filename = $_POST['file'];
echo '<table class="table1" border="0" width="100%" >';
echo "<tr>";
echo "<td class='td1'>";
$images = glob($_SERVER["DOCUMENT_ROOT"].
"/admin/gallery/".$filename.
"/*.*");
$count = 0;
foreach($images as $image) {
echo '<a href="'.$image.
'" target="_blank"><img src="'.$image.
'"class="img-rounded mySlides" width="200" height="150"/></a><br />';
if ($count < 3) {
$count++;
echo "</td>";
echo "<td class='td1'>";
} else {
echo "</tr>";
$count = 0;
echo "<tr>";
echo "<td class='td1'>";
}
}
The HTTP 404 Not Found Error means that the webpage you were trying to reach could not be found on the server. It is a Client-side Error which means that either the page has been removed or moved and the URL was not changed accordingly, or that you typed in the URL incorrectly. Give extensions in giving url.
There is no issue with the permission on the file. The error is due to mismatch of the Relative Path of the image. 404 means the resource doesn't exist.
You can check my answer here [can't find correct relative paths of CSS background-image
The problem is glob($_SERVER["DOCUMENT_ROOT"]."/admin/gallery/".$filename."/.").$_SERVER["DOCUMENT_ROOT"] will take path as home/xxxx/public_html/admin/gallery.which cannot open in the browser.The image will open only by the path like this "http://example.com/folder/img.jpg".so i have cropped the path till admin folder like home/xxx/public_html/admin/gallery to admin/gallery and in the img src i have given src="http://example.com/".$image as shown in the code
$filename=$_POST['file'];
echo '<table class="table1" border="0" width="100%" >';
echo "<tr>";
echo "<td class='td1'>";
$curdir=getcwd();
$images = glob($curdir."/admin/gallery/".$filename."/*.*");
$count=0;
foreach($images as $image)
{
$dir="http://screccs.com".str_replace($curdir,"",$image);
echo "<a href='".$dir."' target='_blank'>"; echo "<img src='".$dir."'class='img-rounded' width='200' height='150'/></a><br />";
if($count<3)
{
$count++;
echo "</td>";
echo "<td class='td1'>";
}
else
{
echo "</tr>";
$count=0;
echo "<tr>";
echo "<td class='td1'>";
}
}
echo '</tr>';
echo '</table>';
}
I believe issue is related to folder permissions. Give proper read permission and it will work.

How to access hive data to html page

I have some crawled data in hadoop. I have exported data from hbase table to hive table.. So now I have to access its tables via html, php etc. so that I can display it on web. Is their any tutorial or tool available for it. Please guide me thoroughly. My final aim is to search some record from web from hive table.
you can connect Hive with PHP to show the data in webpage.For that you need to use ThrifSQL.phar.
refer the below code
<html>
<body>
<?php
// Load this lib
require_once __DIR__ . '/ThriftSQL.phar';
// Try out a Hive query
$hive = new \ThriftSQL\Hive( 'localhost', 10000, 'user', 'pass' );
$hql = "SELECT * from default.phpexample";
$client = $hive
->connect()
->setSasl(false)
->queryAndFetchAll($hql);
print_r($client);
echo "<table>
<tr>
<th>site_name</th>
<th>price</th>
</tr>";
foreach ($client as $row) {
echo "<tr>\n";
foreach ($row as $field) {
echo "<td>$field</td>";
}
echo "</tr>\n";
}
echo "</table>\n";
$hive->disconnect();
?>
</body>
</html>
Refer https://github.com/Automattic/php-thrift-sql/blob/master/example.php

How to make link of the database in 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>';

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.

Categories