Connecting to SQLite3.

Hello,

can someone help me with a simple question? (Apologies if I haven’t posted in the correct forum).

I’m creating a webpage for use on my desktop using HTML5, CSS3 and jQuery for the UI. I have a form that I want to connect to a SQLite3 database. How can I do this with PHP (I’m using ver. 5.4.45)? How do I embed the PHP code so that this utility I’m making is portable?

When I use this code in a separate file (

):

[php]<?php
class MyDB extends SQLite3
{
function __construct()
{
$this->open(‘mydb.db’);
}
}
$db = new MyDB();
if(!$db){
echo $db->lastErrorMsg();
} else {
echo “Opened database successfully\n”;
}

$sql =<<<EOF
CREATE TABLE MYTABLE
(ID INT PRIMARY KEY,
NAME TEXT,
VS VARCHAR,
VB VARCHAR,
DENIED BOOL DEFAULT n,
USING BOOL DEFAULT n);

  INSERT INTO MYTABLE (ID,NAME,VS,VB,DENIED,USING)
  VALUES(1,'CT','soo1','boo1','n','n');

  INSERT INTO MYTABLE (ID,NAME,VS,VB,DENIED,USING)
  VALUES(2,'NY','soo2','boo2','n','n');

  INSERT INTO MYTABLE (ID,NAME,VS,VB,DENIED,USING)
  VALUES(3,'HA','soo3','boo3','n','n');

  INSERT INTO MYTABLE (ID,NAME,VS,VB,DENIED,USING)
  VALUES(4,'AZ','soo4','boo4','n','n');
		
 INSERT INTO MYTABLE (ID,NAME,VS,VB,DENIED,USING)
 VALUES(5,'TS','soo5','boo5','n','n');

EOF;

$ret = $db->exec($sql);
if(!$ret){
echo $db->lastErrorMsg();
} else {
echo “Records created successfully\n”;
}
$db->close();
?>[/php]

The page shows this along the top in precisely this way:

open(‘mydb.db’); } } $db = new MyDB(); if(!$db){ echo $db->lastErrorMsg(); } else { echo “Opened database successfully\n”; } $sql =<<exec($sql); if(!$ret){ echo $db->lastErrorMsg(); } else { echo “Records created successfully\n”; } $db->close(); ?>

What’s wrong with the code? Do I need to copy any PHP library files into the folder of the project? If I can’t do this with PHP how do I connect with JavaScript?

hafhdrn, What do you mean by portable?

Normally, you either embed your PHP code inside the same page. Or, if you call out to a secondary page,
you need to also include HTML code in that page to display your data. Tell us what you really are trying to
do and we can help.

To give you a sample layout of a normal page, it is something like this:

<?PHP Code here that handles the data that is posted from your ... ?> Your form here... Note that the code for processing your form data in the PHP section can send the user to another page if it is needed. If you send the user to a second page using your action="somescript.php", then that new page needs to be a FULL ENTIRE HTML page. (Which can also include more PHP...) The secondary page displays code because it is not set up as PHP, but, just displays text... Hope that helps!

Also, here is a tutorial on how to set this up. It starts with HTML forms, press NEXT-CHAPTER button to
move to adding in the PHP code for validating the inputs from the form. You can walk thru the chapters to
study how to do most everything you have mentioned. Hope this helps, too…
http://www.w3schools.com/php/php_forms.asp

Hi and thank you ErnieAlex.

Actually, I have found what I need in PHP Desktop - that is what I mean by portable - and I have a copy of the tutorial.

Also, having read up a little on PHP, I can use JavaScript within PHP to access the HTML DOM and show the results.

However, a fuller explanation:

Desktop application (hence PHP Desktop) with a single HTML page and 2 very simple forms:

Form 1 has 5 radio buttons and a submit button. When the choice is submitted the PHP script checks (or creates if not present) a SQLite database and (using Javascript) returns information to 2 elements in the HTML page and updates the SQL record selected in one of the columns to say it is being used.

Form 2 is just the choice from Form 1 and a submit button. When the submit button is clicked (another?) PHP script accesses the same SQLite database and retrieves information and displays it (again Javascript) and updates the SQLite database as it was updated from Form 1 (a reverse action on the SQLite database).

This is a big ask as I have very little knowledge regarding PHP but I now have the means to create this. Are there any tutorials (aside from the one you included the link to - thank you for that) for what I want to do? I will also be checking PHP Desktop documentation for further information.

Well, let’s be clear. What you are calling “PHP Desktop” is an application that allows you to run PHP code
on a desktop. It is basically the same as just using WAMP or XAMP. These set up a “virtual” server on your
computer and works as though you have a standard server up and running your code. Most programmers
that post on this site would use WAMP or XAMP, not PHP-Desktop. If you are creating a desktop application,
it would be better to just write the entire app in Java. (Note: Java, not Javascript!)

Anyways, if you are using PHP to handle database programming, you do not really need to use Javascript to
access the database as PHP can do it.

Now, you have two forms. With buttons on them. PHP can handle everything you need to do. I can read
the radio buttons on form 1 and pull info from the database and display the choices in form 2 and update
the database. So, it can do everything you need.

I think I do not understand what your questions are. You can post code here and we can help. The code
you posted only creates a table. You did not show the forms or what you need. Give us a little more info
and we can help you solve this.

Also, PHP-Desktop requires creating your code, testing and then sending out the PHP-Desktop runtime and
your code. This means that it is not really secure. If you want to create applications for the desktop, you
might want to download Microsoft’s Visual Suite. It is free and the Visual-Basic is very easy to learn and
create full Windows applications. Just a thought…

Here is the HTML form (although there, I think mistakes here, for example the for each ).

[code]



Ct


Ny


Ha


Az


Ts




Image chosen from form 1

You have chosen

Choice from form 1

Image from form 2 as dictated by the choice in form 1
[/code]

Using Javascript (within PHP) to access the HTML DOM would mean that the HTML page itself would be updated rather than requiring PHP to create a new page. I realise this may seem like I’m ignoring a capability of PHP but I want to reduce the number of pages if possible. I pretty much want to use PHP to process the requests and SELECT and UPDATE the SQLite3 database. Sorry if I’ve not been clear before but this is a new area of coding for me.

Well, I do understand what you are saying about Javascript changing the DOM.

Your code for the form you posted uses radio buttons. But, it does not use them as one selection.
Therefore, you must be using them as checkboxes instead. Is that correct? I mean, can your user select
more than one state at a time?

Back to your original question. If you have your form post out to a second page name somescript.php,
then that must be PHP code. In that page, your code needs to be <?PHP some code; ?> If you just place
your PHP code in that page, it will display as text. My guess is that the second page is not set up as PHP
code. Also, even though it is PHP code, it must still have HTML in it or at the least be redirected to a page
that does contain HTML so that something is displayed on the page. You said it was displaying this:

open('mydb.db'); } } $db = new MyDB(); if(!$db){ echo $db->lastErrorMsg(); } else { echo "Opened database successfully\n"; } $sql =<lastErrorMsg(); } else { echo "Records created successfully\n"; } $db->close(); ?>
It shows the ENDING ?> But, there is no STARTING <?PHP ... Looks like the PHP is not starting correctly.

I have tested the HTML form and only one radio button can be chosen at a time. The user should not be able to select more than one - this would cause a problem.

I think I am getting confused between POST and GET: I must remedy this. From what you’ve said though, GET would be the appropriate METHOD for me to use. I will get myself confirmation (W3Schools website as you suggested - thank you for that).

Yes, embarrassing for me - I have pretty much started with PHP - I will check the installation with regard to the HTML editor I’m using.

Thank you for your response and advice. I’m sure I will return with another question though. :slight_smile:

Okay, sounds like you understand the radio buttons. Well, let me explain POST/GET for you. This will speed you along a bit…

Using GET, the data is passed thru to other pages in the URL. Basically, it looks something like this:
www.mysite.com/mypage.php?somevariable=3&somevariable2=“sometext”
This passes the values in the actual URL of the page. I never use that as a good hacker can view them and
post invalid data to your site. There are ways to hide that data, but, it is more work.

Using POST, the data is passed thru the browser’s “session” stream and is not seen by anyone. It is done
behind the scenes and therefore, harder to hack. I always use this version. Or, I save the info into the
$_SESSION[] variables which are valid until the user logs off.

Either way is good for testing or learning. Both are used in PHP pages in a similar manner and both are
easy to use. You just use $_POST[“variable_name”] or $_GET[“variable_name”] to access the data you
are sending.

The problem with $_GET is that the user will see your variables on the URL’s address line. Let’s say you
want to send the user’s ID to a page using GET, it would show in the address line. So, not secure. Now
there is a way to read the global array which will hide it from user, but, that is not the normal way to do it.

Not sure if this made it more complicated for you or if it helps. There are many reasons why you would use
one instead of the other. For instance, GET “cache’s” it’s data in the browser. This is handy if you need to
go back a page to look at a different set of data again. (Not secure as hackers might be able to look at the
cache on your system if they are good at it.) But, here is a link to explain it further. Might help you decide
which is best for your site…
http://www.w3schools.com/tags/ref_httpmethods.asp
Ask any questions you might have… Someone here will help…

Yes, I must consider what you recommended seriously and I think that POST may be the way forward for me.

Thank you for your help. Very comprehensive.

You are very welcome. Once you get your 'posted" code in place, ask any other questions and we can
help you here. There are many helpful programmers here who will jump in when needed. Good luck!

Sponsor our Newsletter | Privacy Policy | Terms of Service