1

I have this PHP script:

<?php
    if($_GET["name"] != null && $_GET["name"] != ""){
        $name = urlencode($_GET["name"]);
        $name = htmlentities($name);
        $title = urlencode($_GET["title"]);
        $title = htmlentities($title);
        $art = urlencode($_GET["art"]);
        $art = htmlentities($art);
        $output = array();
        exec("python add.py $name $title $art",$output);            
    }
?>

and here's the add.py file:

import sys
import sqlite3

name = sys.argv[1]
title = sys.argv[2]
art = sys.argv[3]
tup = (name,title,art)

conn = sqlite3.connect('arts.db')
c = conn.cursor()

c.execute('CREATE TABLE IF NOT EXISTS arts(name,title,art)')
c.execute('INSERT INTO arts VALUES(?,?,?)',tup)

conn.commit()
conn.close()

When the file runs there is no arts.db file in my current directory. Not only that, when I debugged my program by adding print statements every here and there I realised that my program runs till conn = sqlite3.connect('arts.db') and then exits before the statement is executed.

There is no error in my program because I used the python editor in the terminal (I use Ubuntu) and then I could execute this program successfully but this doesn't happen when I execute this from the PHP script.

1 Answer 1

1

If you're trying to reference an existing arts.db file, which is not in the current directory when the PHP script runs, then it's probably easiest to refer to the file by its full path, i.e. change the line...

conn = sqlite3.connect('arts.db')

...to something like...

conn = sqlite3.connect('/the/full/path/to/arts.db')

If you're trying to create a new arts.db, it's highly probable that the webserver process doesn't have permission to create the file in the current directory when the PHP script runs.

It's probably safer not to give PHP permission to write to the directory containing the PHP script[s], so create a directory somewhere else to store the file, give it the appropriate permissions, and use the full path to that directory in the script.

Update

i have figured out that i dont have permissions to write hence i created a db using another program and created a table in it but still for inserting values into it i require permissions, can you tell me how to do that?

Problem is that any process spawned by the PHP script will inherit the permissions from the parent process, and will have the same limitations.

On recent versions of Ubuntu, the /var/tmp directory can always be written to by any user, so you could put it there with...

conn = sqlite3.connect('/var/tmp/arts.db')

...or you can put it anywhere else by modifying the permissions, with...

$ sudo chgrp www-data /the/full/path/to/directory
$ sudo chmod g+ws /the/full/path/to/directory

Of course, this means there's not much point in using a seprate Python script, so you may as well do it all in PHP.

Sign up to request clarification or add additional context in comments.

3 Comments

There should also be some error handling in the Python script to complain and exit if the database file is not found.
@halfer Actually, upon investigation, it looks like sqlite3 will try to create the file if it doesn't exist, which is more likely to be the problem.
thankyou @Aya but i have figured out that i dont have permissions to write hence i created a db using another program and created a table in it but still for inserting values into it i require permissions, can you tell me how to do that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.