Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 315
  • Last Modified:

need help inserting data into a sqlite3 database from a html / php webform

I'm trying to insert data into a sqlite3 from an html webform.  The html form is using method post, and calling action roadshow.php.  Attaching the code thats in roadshow.php.

when i fill out the form and click submit, i'm getting the error "file is encrypted or is not a database"

I have traced this down to the way i'm trying to open the DB file in php, sqlite_open is for sqlite version two, the database was created with sqlite3.  From what I've read, I need to be calling the DB file using PDO, but I cant figure out how to do this.  If someone could help me get this working, I would appreciate it.

FYI, probably not necessary information, but this is all hosted on a local machine, windows xp.  Please, do not make assumptions, if you need more information, please ask.
<?php

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$phone = $_POST['phone'];
$address1 = $_POST['address1'];
$city = $_POST['city'];
$state = $_POST['state'];



if (!empty($firstname)) {

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

   if (!$dbhandle) die ($error);
   
   $stm = "INSERT INTO participants(firstname, lastname, phone, address1, city, state) VALUES('$firstname', '$lastname', '$phone', '$address1', '$city', '$state')";
   $ok = sqlite3_exec($dbhandle, $stm, $error);

   if (!$ok) die("Error: $error");  
   echo "Form submitted successfully";
}
?>

Open in new window

0
Sappbrosts
Asked:
Sappbrosts
  • 4
  • 3
1 Solution
 
Ludwig DiehlSystems ArchitectCommented:
have you tried using sqlite_query?, as you are not really executing a batch .
sqlite_query($dbhandle, "INSERT INTO participants(firstname, lastname, phone, address1, city, state) VALUES('$firstname', '$lastname', '$phone', '$address1', '$city', '$state')");
.
.
.
sqlite_close($dbhandle);

Open in new window

0
 
Ludwig DiehlSystems ArchitectCommented:
...if it doesn't work pleace attach "roadshow.db" so I can try and insertion
0
 
SappbrostsAuthor Commented:
ok, so this is what I have now, this gives an http error 500 when i click submit on the form.

I also tried this, this one does not give http error 500, but just shows a blank page after clicking submit, i checked the table and there was no data inserted.  DB file attached (had to change to txt extension, wouldnt let me attach it even in a zip file as .db )

if (!empty($firstname)) {

      sqlite_query($dbhandle, "INSERT INTO participants(firstname, lastname, phone, address1, city, state) VALUES('$firstname', '$lastname', '$phone', '$address1', '$city', '$state')");

      sqlite_close($dbhandle);
}

if (!empty($firstname)) {

	sqlite_query($dbhandle, "INSERT INTO participants(firstname, lastname, phone, address1, city, state) VALUES('$firstname', '$lastname', '$phone', '$address1', '$city', '$state')"); 

     $ok = sqlite3_exec($dbhandle);

  if (!$ok) die("Error: $error");  
    echo "Form submitted successfully";


	sqlite_close($dbhandle);
}
?>

Open in new window

roadshow.txt
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
SappbrostsAuthor Commented:
if you need it, this is what I have in the DB table
CREATE TABLE participants (
firstname text not null,
lastname text not null,
phone numeric(10) not null,
address1 varchar(64) not null,
address2 varchar(64) not null,
city varchar(30) not null,
state varchar(2) not null,
zip varchar(5) not null,
datetime timestamp default current_timestamp null,
pkey integer not null primary key autoincrement
)

Open in new window

0
 
Ludwig DiehlSystems ArchitectCommented:
Ok, it seems build-in sqlite api cannot read such version so you can try using PDO:

for further info take a look at: http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html#7.2
$firstname = 'FirstName';
$lastname = 'LastName';
$phone = '2123123';
$address1 = 'address';
$city = 'CITY';
$state = 'State';


try 
{
    $dbh = new PDO('sqlite:/path_to_your_db/roadshow.db');
    $sql="INSERT INTO participants(firstname, lastname, phone, address1, city, state) VALUES('$firstname', '$lastname', '$phone', '$address1', '$city', '$state')";
    echo $affected = $dbh->exec($sql);
  
}
catch(PDOException $e)
{
    echo $e->getMessage();
}
/*** close the database connection ***/
$dbh = null;

Open in new window

0
 
SappbrostsAuthor Commented:
thank you, you've been a great help!
0
 
Ludwig DiehlSystems ArchitectCommented:
you are welcome!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now