I'm getting this

-> [Tue Sep 10 10:42:46 2013] [error] [client 127.0.0.1] PHP Warning: SQLite3::exec(): unable to open database file in /var/www/ET/insert.php on line 21, referer: http://localhost/ET/

I changed the owner of the DB file (et.sqlite3) to www-data and now this is what I'm getting. Before I changed the owner I was getting an "unable to write to DB" error. Code is below, any ideas? Thanks.

<?php

echo "<html>";
echo "<head>";
echo "</head>";
echo "<body>";

$db = new SQLite3('./et.sqlite3', SQLITE3_OPEN_READWRITE);

if(!$db)
{
    echo "Could not open/access DB";
}
else
{
    // get variable from html form
    $fName = $_POST['fname'];
    $lName = $_POST['lname'];
    $email = $_POST['email'];

    $db->exec("INSERT INTO customers VALUES('$fName', '$lName', '$email')");
}

echo "</body>";
echo "</html>";

?>

Recommended Answers

All 6 Replies

Check if the file and also the directory containing the database, have read and write permissions.

This is the containing directory:
drwxr-xr-x 2 root root 4096 Sep 10 12:05 ET

Here is the file:
-rw-r--r-- 1 www-data root 2048 Aug 31 15:58 et.sqlite3
-rw-r--r-- 1 root root 410 Sep 10 10:35 insert.php

try to...
change $db->exec("INSERT INTO customers VALUES('$fName', '$lName', '$email')");
to
$db->exec("INSERT INTO customers VALUES(" . $fName . "," . $lName . "," . $email . ")");
or
$db->exec("INSERT INTO customers VALUES(" . "'" . $fName . "'" . "," . "'" . $lName . "'" . "," . "'" . $email . "'" . ")");

Apache is running as root or as www-data user? It should be www-data, verify the output of:

ps -aux |grep apache

You should see the main process owned by root, the childs owned by www-data user. So, in order to work correctly, you have to change the ownership of the directory and of the files under the DocumentRoot of your website, try this:

sudo chown www-data:www-data -R /var/www/ET

With -R flag we recursively change the owners and the group of all the files inside ET directory. That should fix the problem.

Thanks guys, I'm a little closer but still not there yet. I chagned the insert line to this:

$db->exec("INSERT INTO customers VALUES(" .$fName . "," . $lName . "," . $email .")");

And now I'm getting this in the error log when I enter the name Bailey to be written into the fName field. The column name is

[Tue Sep 10 13:36:35 2013] [error] [client 127.0.0.1] PHP Warning: SQLite3::exec(): no such column: Bailey in /var/www/ET/insert.php on line 21, referer: http://localhost/ET/

Got it, thanks gusy.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.