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">
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'
Answer
Solution:
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 underroot
. Hence giving write permissions to root did not help. Here is what I did. change the user of the directory towww-data
usingchown -hR www-data /var/www/html
. Gave read and write permissions to the database and PHP file.Thanks for all the helpful comments.