The following PostgreSQL query producing me the following error. But in my intuition everything is correct. I don't know why PostgreSQL making it wrong.
$query = "INSERT INTO test (sku) VALUES ("."3814TT82033102-2".")";
The sku filed is VARCHAR(50) but why the postgres query not allowed my sku. I got the following
PHP Warning: pg_exec(): Query failed: ERROR: syntax error at or near "TT82033102"
LINE 1: INSERT INTO test (sku) VALUES (3814TT82033102-2)
^ in /home/nifras/Documents/User/working-dir/csv/functions/import-rules.php on line 11
I'm not a php expert, but I feel a bind parameter could be used.
$sku = '3814TT82033102-2';
$query = 'INSERT test (sku) VALUES VALUES(:sku)';
$stmt = $this->pdo->prepare($query);
$stmt->bindValue(':sku', $sku);
I have changed my query as follow
$product_sku = $product[0];
$query = "INSERT INTO test (sku) VALUES ('$product_sku')";
The issue is strings should cover by singlw quotes
Thanks a_horse_with_no_name
Related
In my database I have a table TABLE(person, match).
I'm trying to insert values in this table but I get a sintax error, I think I'm writing the query wrong.
$query = "INSERT INTO table ($1, SELECT id FROM match_import) ON CONFLICT DO NOTHING; ";
$result = pg_prepare($dbh, "", $query);
$result = pg_execute($dbh, "", array($user));
The first value is an argument passed with the pg_execute, but the second one (match) is taken from another table in the database.
I get this error
Error: Warning: pg_prepare(): Query failed: ERROR: syntax error at or near "$1" LINE 1: INSERT INTO table ($1, SELECT id FROM match_import) ON CON... ^ in /Applications/mappstack-7.1.27-2/apache2/htdocs...
How can I solve these problem?
You should add the argument to the query:
INSERT INTO table(column1, column2)
SELECT $1, id FROM match_import
ON CONFLICT DO NOTHING
I'm making a search engine based on the API of Faroo.com (http://www.faroo.com/hp/api/api.html) for a school project. I would like to index the index of Faroo, so that users (in my situation, children) can vote up or vote down individual results.
What my (PHP)-script is like:
Look in the MySQL-database if the query exists.
yes => load the results from the database and show them to the user
no => load the results from Faroo, show those results to the user and store them in the database
My database looks like this:
I'm getting all the data stored in the columns from the Faroo API, except for the 'id'-column.
The last part (of storing the Faroo-data in the database) is where it goes wrong:
for($x=0; $x<$tel; $x++){
$sql = "INSERT INTO queries (`id`, `query`, `title`, `url`, `domain`, `kwic`, `votes`) VALUES (NULL, $q, $titles[$x], $urls[$x], $domains[$x], $kwics[$x], 0);";
echo '<br />'.$x.'e query: #'.$sql.'#';
if(!$resultaat = $db->query($sql)){
die('De query kon niet worden uitgevoerd: [' . $db->error . ']');
}
$resultaat = mysqli_fetch_array($resultaat);
}
$tel is a variable which counts the number of results I get from Faroo. It gets defined before this piece of code.
When I run this code, I am getting a nice MySQL-error:
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'States Bureau of Mines - Wikipedia, the free encyclopedia,
www.wikipedia.org' at line 1
I've searched, and searched, but I couldn't find what the SQL-error is. I think it has something to do with the strange characters in the strings, or maybe my quotation is false?
Kind regards,
Max
I think you need to use single quotes ' for varchar columns, so change as follow
$sql = "INSERT INTO queries (`id`, `query`, `title`, `url`, `domain`, `kwic`, `votes`) VALUES (NULL, '$q', '$titles[$x]', '$urls[$x]', '$domains[$x]', '$kwics[$x]', 0)";
You also have an extra double quote at the end of the query which i removed, you won't need singles quotes for columns id and votes since they are integer fields
I am running the following query :
SELECT #newNo := MAX( category_code ) FROM category_master;
INSERT INTO category_master VALUES (#newNo +1, 'Test')
The query runs flawlessly in phpmyadmin but it shows a database error when run using codeigniter :
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'INSERT INTO category_master VALUES(#newNo+1, 'Test')' at line 2
what could be the reason ??
In codeigniter model i use the following code :
$query = 'SELECT #newNo := MAX(category_code) FROM category_master;
INSERT INTO category_master VALUES(#newNo+1,
\''.$category_name.'\')';
$result = $this -> db -> query($query);
You cannot run two queries at once. Seperate them:
$query = 'SELECT #newNo := MAX(category_code) FROM category_master';
$result = $this->db->query($query);
$query = 'INSERT INTO category_master VALUES(#newNo+1, \''.$category_name.'\')';
$result = $this->db->query($query);
EDIT:
On your second query it is recommended to use query bindings:
$query = 'INSERT INTO category_master VALUES(#newNo+1, ?)';
$result = $this->db->query($query, $category_name);
Make sure the query does not contains any special characters. The browser will convert the special characters, so the query is running in phpmyadmin.
To know the special characters, echo the query and copy it and paste in an editor like dreamweaver, it will show you the special characters. Hope this helps.
I used this query to insert all my values into this database:
INSERT INTO products ($fields) VALUES ($values)
However, I try to use the same format for UPDATE:
UPDATE products SET ($fields) VALUES ($values) WHERE sku = '$checksku'
...and am getting thrown a syntax error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('product,make,model,' at line 1
I can't figure it out. Would appreciate any help. Thanks.
UPDATE syntax is different than INSERT syntax. An example of UPDATE would be:
"UPDATE products SET field1 = 'value1', field2 = '$val2', field3 = 5 WHERE sku = '$checksku'"
Though this may be insecure. You should look into parameterized queries.
INSERT INTO products ($fields) VALUES ($values) ON DUPLICATE KEY UPDATE field = VALUES(field), ...
Don't forgot about unique or primary key
you need an =
UPDATE products SET ($fields) = $values WHERE sku = '$checksku'
I run this command in SQL Workbench and it returns my desired results, but it return a syntax error in the browser...
$sql = "SELECT
SUBSTRING(`last_name`, 1, 1) AS alpha,
SUBSTRING(`middle_name`, 1, 1) AS subMiddleName,
`idClients`,
`type`,
`first_name`,
`middle_name`,
`last_name`,
`address`,
`primary_number`,
`secondary_number`,
`home_number`,
`office_number`,
`cell_number`,
`fax_number`,
`ext_number`,
`other_number`,
`comments`
FROM `clients`
WHERE `user_id` = 2
AND `is_sub` = 0
AND `prospect` = 1
ORDER BY `last_name`";
Also user_id, is_sub, and prospect are of the INT data type if anyone wants to know. I tried to treat them as strings in the query, but that still didn't help.
this is the error i get
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND prospect = 1 AND type = 'Buyer'' at line 1
You're not showing us the same query, or the relevant PHP code, as nowhere does the above query use the string 'Buyer'.
That said, you may need to escape the column name type with backticks:
AND `prospect` = 1 AND `type` = 'Buyer'