function insert_into($field, $value, $TABLE, $database){
$db = new PDO($database);
// $query = 'INSERT INTO ' . $TABLE . '(' . $field. ') VALUES (' . $value . ');'; First way
$query = 'INSERT INTO ' . $TABLE . '(' . $field. ') VALUES (' . '"' . $value . '"' . ');'; Second way
// echo $query;
$db->exec($query); // Value needs to be in parenthesis, but when it is, I can't insert $val (below).
$db = NULL;
}
I was experimenting with using SQLite as a database instead of MySQL and made the above function, which is then called (twice) using the code below:
if (isset($_POST)){ // If submit pressed
if (isset($_POST['submit-add'])){
$one = filter_input(INPUT_POST, "one");
insert_into('Name', $one, "Russ", 'sqlite:russ.db');
}}
$val = '"Red", "30"';
insert_into('Name, Age', $val, "Russ", 'sqlite:russ.db');
Currently, the uncommented line that sets the $query variable in the function makes it include double quotes around $value because it's the only way the first "insert into" query works. The commented line that sets the $query variable doesn't include double quotes, but is the only way I can get the second "insert into" query to successfully execute. Obviously, you see my dilemma - I can't get one (single) query to work for both the $one variable, and the $val variable. Each value seems to need double quotes (or single quotes, like in the 2nd query below this paragraph) around it, or it won't successfully enter into the database. Is there anyway to make a single query that'll work with both variable's ($one, and $val)? Sorry if any of this may seem confusing at first... I did try to provide enough information, however.
Before this, my original queries were:
// $query = '"INSERT INTO $TABLE ($field) VALUES ($value);"'; This works for the $val query, but not the $one query.
// $query = '"INSERT INTO $TABLE ($field) VALUES ('$value');"'; This works for the $one query/variable, but not the $val query/variable.
Thanks.. any help would be appreciated...
Edit: Assume there is a submit button present, and it has been pressed (just so nobody thinks this is all the code).
$db = new PDO($database);
// $query = 'INSERT INTO ' . $TABLE . '(' . $field. ') VALUES (' . $value . ');'; First way
$query = 'INSERT INTO ' . $TABLE . '(' . $field. ') VALUES (' . '"' . $value . '"' . ');'; Second way
// echo $query;
$db->exec($query); // Value needs to be in parenthesis, but when it is, I can't insert $val (below).
$db = NULL;
}
I was experimenting with using SQLite as a database instead of MySQL and made the above function, which is then called (twice) using the code below:
if (isset($_POST)){ // If submit pressed
if (isset($_POST['submit-add'])){
$one = filter_input(INPUT_POST, "one");
insert_into('Name', $one, "Russ", 'sqlite:russ.db');
}}
$val = '"Red", "30"';
insert_into('Name, Age', $val, "Russ", 'sqlite:russ.db');
Currently, the uncommented line that sets the $query variable in the function makes it include double quotes around $value because it's the only way the first "insert into" query works. The commented line that sets the $query variable doesn't include double quotes, but is the only way I can get the second "insert into" query to successfully execute. Obviously, you see my dilemma - I can't get one (single) query to work for both the $one variable, and the $val variable. Each value seems to need double quotes (or single quotes, like in the 2nd query below this paragraph) around it, or it won't successfully enter into the database. Is there anyway to make a single query that'll work with both variable's ($one, and $val)? Sorry if any of this may seem confusing at first... I did try to provide enough information, however.
Before this, my original queries were:
// $query = '"INSERT INTO $TABLE ($field) VALUES ($value);"'; This works for the $val query, but not the $one query.
// $query = '"INSERT INTO $TABLE ($field) VALUES ('$value');"'; This works for the $one query/variable, but not the $val query/variable.
Thanks.. any help would be appreciated...
Edit: Assume there is a submit button present, and it has been pressed (just so nobody thinks this is all the code).
Last edited: