Unable to insert data into the sqlite database

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
vik87
New php-forum User
New php-forum User
Posts: 5
Joined: Thu Feb 08, 2018 6:49 am

Thu Feb 08, 2018 6:55 am

Hi guys, I am unable to insert data via form into sqlite database. It creates the database file but wouldnt insert the data. I am new to programming so i am clueless, i tried in the stackoverflow but couldnt find an helpful post. Help would be much appreciated. Thanks for taking your time to read and answer. Here is the code,
<?php
require 'formHelper.php';
try{
$db = new PDO('sqlite:c:\Users\BurstMine\Documents\restaurant.db','','');
}
catch(PDOException $e){
print "Cant connect. ". $e->getMessage();
exit();
}
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if($_SERVER['REQUEST_METHOD'] == 'POST'){
list($errors,$input) = validate_form();
if($errors){
show_form($errors);
}
else{
process_form($input);
}
}
else{
show_form();
}

function show_form($errors = array()){
$default = array('price'=>'5.00');
$form = new formHelper($default);
include 'restaurant-form.php';
}

function validate_form(){
$input = array();
$errors = array();
$input['dish_name'] = trim($_POST['dish_name'] ?? '');
if(!strlen($input['dish_name'])){
$errors = "Please enter the name of the dish.";
}
$input['price'] = filter_input(INPUT_POST,'price',FILTER_VALIDATE_FLOAT);
if($input['price'] <= 0){
$errors = "Please enter the price of the dish.";
}
$input['is_spicy'] = $_POST['is_spicy'] ?? 'no';
return array($errors, $input);
}

function process_form($input){
global $db;
if($input['is_spicy'] == 'yes'){
$is_spicy = 1;
}
else{
$is_spicy = 0;
}
try{
$stmt = $db->prepare('INSERT INTO dishes(dish_name,price,is_spicy) VALUES(?, ?, ?)');
$stmt = $db->execute(array($input['dish_name'], $input['price'], $is_spicy));
print 'Added'. htmlentities($input['dish_name']). 'to the database';
}
catch(PDOException $e){
print "Couldnt add dish to the database";
}
}
?>

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 693
Joined: Mon Feb 22, 2016 5:52 pm

Thu Feb 08, 2018 9:08 am

What errors are you getting?

vik87
New php-forum User
New php-forum User
Posts: 5
Joined: Thu Feb 08, 2018 6:49 am

Thu Feb 08, 2018 7:09 pm

Couldnt add dish to the database...

chorn
php-forum Fan User
php-forum Fan User
Posts: 546
Joined: Fri Apr 01, 2016 2:18 am

Fri Feb 09, 2018 12:49 am

so you got an PDOException $e there, what does var_dump() say about it?

vik87
New php-forum User
New php-forum User
Posts: 5
Joined: Thu Feb 08, 2018 6:49 am

Fri Feb 09, 2018 1:20 am

Couldnt add dish to the databaseobject(PDOException)#3 (8) { ["message":protected]=> string(55) "SQLSTATE[HY000]: General error: 1 no such table: dishes" ["string":"Exception":private]=> string(0) "" ["code":protected]=> string(5) "HY000" ["file":protected]=> string(30) "C:\xampp\htdocs\dishesform.php" ["line":protected]=> int(54) ["trace":"Exception":private]=> array(2) { [0]=> array(6) { ["file"]=> string(30) "C:\xampp\htdocs\dishesform.php" ["line"]=> int(54) ["function"]=> string(7) "prepare" ["class"]=> string(3) "PDO" ["type"]=> string(2) "->" ["args"]=> array(1) { [0]=> string(60) "INSERT INTO dishes(dish_name,price,is_spicy) VALUES(?, ?, ?)" } } [1]=> array(4) { ["file"]=> string(30) "C:\xampp\htdocs\dishesform.php" ["line"]=> int(17) ["function"]=> string(12) "process_form" ["args"]=> array(1) { [0]=> array(3) { ["dish_name"]=> string(3) "new" ["price"]=> float(5) ["is_spicy"]=> string(2) "no" } } } } ["previous":"Exception":private]=> NULL ["errorInfo"]=> array(3) { [0]=> string(5) "HY000" [1]=> int(1) [2]=> string(21) "no such table: dishes" } }

vik87
New php-forum User
New php-forum User
Posts: 5
Joined: Thu Feb 08, 2018 6:49 am

Fri Feb 09, 2018 1:29 am

Here is the more prettier version of the var dump, the one above is almost unreadable
PDOException::__set_state(array(
'message' => 'SQLSTATE[HY000]: General error: 1 no such table: dishes',
'string' => '',
'code' => 'HY000',
'file' => 'C:\\xampp\\htdocs\\dishesform.php',
'line' => 54,
'trace' =>
array (
0 =>
array (
'file' => 'C:\\xampp\\htdocs\\dishesform.php',
'line' => 54,
'function' => 'prepare',
'class' => 'PDO',
'type' => '->',
'args' =>
array (
0 => 'INSERT INTO dishes(dish_name,price,is_spicy) VALUES(?, ?, ?)',
),
),
1 =>
array (
'file' => 'C:\\xampp\\htdocs\\dishesform.php',
'line' => 17,
'function' => 'process_form',
'args' =>
array (
0 =>
array (
'dish_name' => 'new',
'price' => 5.0,
'is_spicy' => 'no',
),
),
),
),
'previous' => NULL,
'errorInfo' =>
array (
0 => 'HY000',
1 => 1,
2 => 'no such table: dishes',
),
))

vik87
New php-forum User
New php-forum User
Posts: 5
Joined: Thu Feb 08, 2018 6:49 am

Fri Feb 09, 2018 1:47 am

Ok guys i got it. Although database is created automatically if it doesnt exist... table and columns have to be added manually. Thanks for your time and help.

Post Reply