LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-10-2020, 11:13 PM   #1
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Rep: Reputation: 12
PHP - SQLight3 ERROR no such table "table"


The database opens successfully. The query produced by my code populates the database using 'DB Browser' with no problem. Under firefox I get the error no such table 'books'. Appreciate a solution to fix this problem!!

Here is the partial output of the code below:

Quote:
Welcome to the Library
Opened database successfully

Failed adding war because of error
Crap - no such table: books

Please enter book information below. Include at least a title and author


Enter a book title:

Enter the author (last, first) of this book:
Here is my code. CREATE Database Library.db and table books:
Code:
<html>
<head>
<title>Create Library Database and Table</title>
</head>
<body>
<?php

   class MyDB extends SQLite3 {
      function __construct() {
         $this->open('library.db');
      }
   }
   $db = new MyDB();
   if(!$db) {
      echo $db->lastErrorMsg();
   } else {
      echo "Opened database successfully\n";
   } 
 
   $sql = "CREATE TABLE books (
      book_id INTEGER PRIMARY KEY,
      book_title VARCHAR,
      book_author VARCHAR,
      book_pub_year INTEGER,
      book_publisher VARCHAR,
      book_read VARCHAR,
      book_score VARCHAR,
      book_loan VARCHAR
   )";
   
     $ret = $db->exec($sql);
   if(!$ret){
      echo $db->lastErrorMsg();
   } else {
      echo "Table created successfully\n";
   }
   $db->close();

?>
</body>
</html>
Here is the code to insert the data:

Code:
<html>
<head>
<title>Personal Library: Add a Book</title>
</head>
<body>
<h1 align="center">Welcome to the Library</h1>

<?php

$database="/var/www/rickSQL.com/public_html/books/library.db";
$table="books";

if (empty($_POST['action'])) $_POST['action'] = '';
if ($_POST['action'] == "Insert") {
	   class MyDB extends SQLite3 {
      function __construct() {
      	global $database;
         $this->open($database);
      }
   }
   $db = new MyDB();
   if(!$db) {
      echo $db->lastErrorMsg();
   } else {
      echo "Opened database successfully<br>";
   } 
   
      $sql="INSERT INTO ".$table." (book_title, book_author, book_pub_year,
               book_publisher, book_read, book_score, book_loan)
               values('$_POST[form_title]', '$_POST[form_author]', 
               '$_POST[form_pub_year]', '$_POST[form_publisher]', 
               '$_POST[form_read]', '$_POST[form_score]', '$_POST[form_loan]');";
//echo $sql;               
             
				$ret=$db->exec($sql);
				if(!$ret) {
   		 	print ( '<p>Failed adding <b>'. $_POST[form_title] . '</b> because of error</p>');
      		echo "Crap - ".$db->lastErrorMsg();
   		} else {
     				print ( '<p>Successfully added '. $_POST['form_title'] . ' to table books.</p>' );
         		print ( '<p>Add another book if you wish.</p>' );
   		}
   $db->close();        

   print ( '<p>Please enter book information below. Include at least a
    title and author</p>') ;
}
?>
<p><form action="books_insert.php" method="post"><br />
Enter a book title:<br />
<input type="text" name="form_title"><br />
Enter the author (last, first) of this book:<br />
<input type="text" name="form_author"><br />
Enter the year of publication<br />
<input type="text" name="form_pub_year"><br />
Enter the publisher:<br />
<input type="text" name="form_publisher"><br>
Have you read this book yet?<br />
<select name="form_read"><br />
<option value="Yes" SELECTED>Yes</option><br />
<option value="No">No</option><br />
</select><br />
Give this book a rating (1 to 5)<br />
<input type="text" name="form_score"><br />
Currently loaned to:<br />
<input type="text" name="form_loan"><br />
<input type="submit" name="action" value="Insert"><br />
</form></p>
<p><a href="index.php">Back to the main library page.</a></p>
</body>
</html>

Last edited by pizzipie; 08-10-2020 at 11:16 PM.
 
Old 08-10-2020, 11:35 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937
One obvious error is the postt variables index in your sql statment are not quoted like your if statements.

$_POST["form_title"]

You can either append the variables like you did with $table in your sql statement or escape the quotes.
 
Old 08-11-2020, 01:14 PM   #3
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
Thanks michaelk,

Here is the output produced by my program exited at the query:

Quote:
Opened database successfully

INSERT INTO books (book_title, book_author, book_pub_year,book_publisher, book_read, book_score, book_loan) VALUES ('Submarine Life', 'Jentzen, Harry', '2001', 'Penguin Books', 'Yes', '4', 'Frank Jones');
Here is my corrected code:
Code:
      $sql="INSERT INTO ".$table." (book_title, book_author, book_pub_year,";
      $sql.="book_publisher, book_read, book_score, book_loan) VALUES ('";
      $sql.= $_POST['form_title']."', '".$_POST['form_author']."', '"; 
      $sql.= $_POST['form_pub_year']."', '".$_POST['form_publisher']."', '"; 
      $sql.= $_POST['form_read']."', '".$_POST['form_score']."', '".$_POST['form_loan']."');";
If I execute the query in DB Browser the data is input with no problem.

Tearing my hair out about now!
 
Old 08-11-2020, 01:48 PM   #4
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
STOP!! Don't need help on this anymore.

I TOTALLY BLEW IT. HAD A COPY OF LIBRARY.DB IN ANOTHER FOLDER AND WAS TRYING TO ACCESS IT.

GOT THE CORRECT DB AND ALL IS FINE!!!

SORRY FOR WASTING YOUR TIME. I AM TOTALLY RED-FACED.

R
 
Old 08-11-2020, 01:56 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
Do you happen to have a complete PHP program that can be run width standalone php (as in php <filename>.php)?
 
Old 08-11-2020, 01:56 PM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,784

Rep: Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937Reputation: 5937
Good, I had run out of ideas... just as an aside although it works anyway is that you don't need to use a trailing ; within the sql statement itself and you really only need quotes around strings.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
What are the differences between the normal symbol table, the dynamic symbol table, and the debugging symbol table? watchintv Linux - Software 5 10-22-2016 08:38 AM
[SOLVED] MySQL run SELECT on a table if column A form table 1 equals column A from table 2 robertjinx Linux - Software 1 01-15-2016 10:48 AM
[SOLVED] error: asm/ppc_asm.h: No such file or directory and error: asm/processor.h: No such f Thirupathip Linux - Newbie 3 01-25-2013 03:02 AM
"Linked From" table below the "Similar Threads" table? win32sux LQ Suggestions & Feedback 1 06-14-2009 06:34 PM
Help With Java Problem Please"""""""""""" suemcholan Linux - Newbie 1 04-02-2008 06:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration