Add sqlite entry Using PHP

920

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.

  1. The table and database are created, in the same directory with executable permissions.
  2. 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'

520

Answer

Solution:

I got it working. The issue was because of file permissions and ownership. The apache process was run by userwww-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.

People are also looking for solutions to the problem: php - Yii2 - Multiples files download from Folder

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.