0

I am trying out sqlite with php. On the server when I execute the command sqlite3, it shows the version and help option suggesting that sqlite is installed there. My folder structure is like this: /username/public_html/
Now I created a db called "chatuser.db" with a table "Users" and two columns "Username" and "Password" using sqlite3 command at /username I then copied the db using WinSCP to my windows folder and then copied it back to the server here: /username/public_html/ (I know it is not a good idea to keep db file in public_html, but am just trying out an example). Now I have the following php file:
add.php:

<?php

$password = $_POST['password'];
$username = $_POST['username'];

$name_es = sqlite_escape_string($username);
$password_es = sqlite_escape_string($password);

if (!empty($username)) {

   $dbhandle = sqlite_open('chatuser.db', 0666, $error);

   if (!$dbhandle) die ($error);

   $stm = "INSERT INTO Users(Username, Password) VALUES('$name_es', '$password_es')";
   $ok = sqlite_exec($dbhandle, $stm, $error);

   if (!$ok) die("Error: $error");  
   else echo "Success";
}
?>

index.html:

<html>
<head>
<title>Login Trial</title>
</head>
<body style="font-size:12;font-family:verdana">

<form action="add.php" method="post">

<p>
Name: <input type="text" name="username"><br>
Password: <input type="text" name="password"><br><br>

</p>

<p>
<input type="submit">
</p>

</form>


</body>
</html>

On clicking the submit button, it loads add.php, but does not show me the success message nor any error message. I am not able to figure out why. As an added note, I have done chmod 777 of chatuser.db as well. This is my first introduction to sqlite, any help would be great. Thanks

[SOLVED] Found the issue, it was version mismatch. sqlite_open will not work. Alternative from this solution: Error: file is encrypted or is not a database

Community
  • 1
  • 1
user1035927
  • 1,653
  • 5
  • 17
  • 18

1 Answers1

1

My guess would be that sqlite_open triggers a fatal error and your PHP isn't configured to show them.

Try placing one echo before the sqlite_open and one after - if only the first one is echo'ed, you know what the issue is.

You will probably want to load the sqlite3 module in PHP.

-edit-

Actually, that only applies to PHP 4, which you're probably not using anymore. Either way, make sure that your PHP outputs errors (go into php.ini and set display_errors to On and error_reporting to E_ALL).

Tom van der Woerdt
  • 29,532
  • 7
  • 72
  • 105
  • strangely both the echoes didn't work. I placed echo as follows: echo "testing"; $dbhandle = sqlite_open('chatuser.db', 0666, $error); if (!$dbhandle) die ($error); echo "testing again"; – user1035927 Nov 30 '11 at 16:54
  • Whoops, `sqlite_escape_string` is called before the `sqlite_open`. Try placing the `echo` before that. – Tom van der Woerdt Nov 30 '11 at 17:02
  • Yup, now the first one got called but the second one didn't. What is wrong and how do I fix it. Thanks – user1035927 Nov 30 '11 at 17:06
  • I strongly recommend updating your server. Sqlite support has been default since PHP 5, which was released in 2004 – Tom van der Woerdt Nov 30 '11 at 17:07
  • so, there is nothing wrong in my script or the way I have setup the db and might be a configuration issue right? Thanks – user1035927 Nov 30 '11 at 17:09