Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am trying to run this code to insert a new row in my "questions" table:
$sql = "INSERT INTO questions (id,quiz_id,question,image,type,option,answer,explanation,date)
VALUES (:id, :quiz_id, :question, :imagesstring, :type, :optionsstring, :answer, :explanation, :date)";
$query = $db->prepare($sql);
$results = $query->execute(array(
":id" => $id,
":quiz_id" => $quiz_id,
":question" => $question,
":imagesstring" => $imagesstring,
":type" => $type,
":optionsstring" => $optionsstring,
":answer" => $answer,
":explanation" => $explanation,
":date" => $date
));
It says there's a syntax error near, but I cannot find it. I checked the table multiple times and the columns correspond with the variables I inserted. Also the variables have the proper value assigned to them. Please help.
OPTION is a reserved keyword. If you don't want to receive any syntax error, you need to escape it using backtick,
INSERT INTO questions (id, quiz_id, question, image, type,
`option`, answer, explanation, date)
VALUES(...)
MySQL Reserved Keywords List
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm creating a small project that scrapes specific webshops and gives an alert when the price changed.
When I try to insert a new record (if price is up/down) it won't insert the record. A connection with the database is in place as I check if the last known price went up or down.
$sql = "INSERT INTO `product_prices` (`productId`, `shopId`, `url`, `originalPrice`, `lowestPrice`, `dateChanged`) VALUES ($productId, $shopId, '".$url."', '".$originalPrice."', '".$lowestPrice."', '".$dateChanged."')";
When I echo $sql it generates this:
INSERT INTO `product_prices` (`productId`, `shopId`, `url`, `originalPrice`, `lowestPrice`, `dateChanged`) VALUES ('1', '1', 'https://www.webshop.com/', '999,99', '800', '2020-07-28 15:04:30')
When I use a remote SQL client (like Sequal Pro) and paste the output of above line it will insert the record. What am I doing wrong?
Problem found, I wasn't using $mysqli->query($sql); after inserting. Now I will dive into SQL injection. Thanks #nico for the heads-up.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I am trying to execute the following query:
INSERT INTO table_timesheet (name, datein, dateout)
VALUES ('Rupert', 'NOW()', '')
WHERE NOT EXISTS (
SELECT name FROM table_listnames WHERE datein='NOW()'
);
But this returns an error.
Basically I don't want to insert a record if the 'datein' field of the record already exists in another record - how to check if the new datein is unique?
And how can i insert in the same way the date out on the same row something like an update the datein row?
This INSERT query will do exactly what you want:
INSERT INTO table_timesheet (name, datein, dateout)
SELECT 'Rupert', NOW(), null
FROM DUAL
WHERE NOT EXISTS (
SELECT * FROM table_listnames WHERE datein=NOW()
);
Please see it here. But a proper solution would be to set a table constraint. How are table_timesheet and table_listnames related? I would use a unique constraint on the datein column, but this depends on what are your requirements.
first check this query (using MYSQLI or PDO ) if this query return false than you insert record
IF... this return false
SELECT name FROM table_listnames WHERE datein='NOW()'
FALSE
INSERT INTO table_timesheet (name, datein, dateout)VALUES ('Rupert', 'NOW()', '')
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
evening all, i have an issue with a syntax sql error
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'where username = danny (name, url, banner, description, sponsor, date, password)' at line 1
Here is my code
$query = "UPDATE websites where username = $login_session (name, url, banner, description, sponsor, date, password) VALUES ('$n', '$b', '$d', '0', now(), SHA('$p'))";
Your MySQL query is wrong, as the error says, check the manual.
In UPDATE you don't use table(field,field1) values('value','value1') like in INSERT, you use field='value', field1='value1' also, WHERE should be at the end, the right order is query + where + order + limit. MySQL is not that flexible.
That's because your UPDATE statement syntax is wrong. Check MySQL documentation for proper UPDATE syntax. I think you meant to do a INSERT rather
INSERT INTO websites (name, url, banner, description, sponsor, date, password)
VALUES ('$login_session', '$n', '$b', '$d', '0', now(), SHA('$p'))
EDIT:
I think this is what you are after
UPDATE websites SET name = '$n',
url = '$b',
banner = '$d',
description = '0',
sponsor = 'some_value_here',
date = now(),
password = SHA('$p')
where username = '$login_session';
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have the following code. I am grabbing values from a form, and using those values to try to update "customers" and "workorder". The "name" value will be duplicate across the customers and workorder table. I keep on getting the error "number of bound variables does not match number of tokens". I am totally new to using PDO, and am unsure on how to proceed. Any ideas?
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE customers set name = ?, email = ?, mobile =? WHERE id = ?; UPDATE workorder set name = ?;";
$q = $pdo->prepare($sql);
$q->execute(array($name,$email,$mobile,$id));
Database::disconnect();
header("Location: index.php");
You have 4 variables here:
$q->execute(array($name,$email,$mobile,$id));
Should be 5:
$q->execute(array($name,$email,$mobile,$id, $name));
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have a database called mkmatchmanager and it has a table called warviewer as you can see below.
You can see the structure of the table here. I am trying to add a row in this table:
$a= mysqli_connect("localhost","username","password","my_mkmatchmanager");
if (mysqli_connect_errno()) {
echo "Failed to connect";
}
mysqli_query($a, "INSERT INTO `my_mkmatchmanager`.`warviewer` (`clan1`, `score1`, `clan2`, `score2`, `table`) VALUES ('test3', '330', 'test4', '241', 'www.mylink.com'");
mysqli_close($a);
This code is included in a file called salva.php. This code by the way doens't work because the query doesn't add the data in the table. Do you have any idea?
Put your double quotes after the parentheses...instead of this:
mysqli_query($a, "INSERT INTO `my_mkmatchmanager`.`warviewer` (`clan1`, `score1`, `clan2`, `score2`, `table`) VALUES ('test3', '330', 'test4', '241', 'www.mylink.com'");
it should be this:
mysqli_query($a, "INSERT INTO `my_mkmatchmanager`.`warviewer` (`clan1`, `score1`, `clan2`, `score2`, `table`) VALUES ('test3', '330', 'test4', '241', 'www.mylink.com')");
should be
mysqli_query($a, "INSERT INTO `my_mkmatchmanager`.`warviewer` (`clan1`, `score1`, `clan2`, `score2`, `table`) VALUES ('test3', '330', 'test4', '241', 'www.mylink.com')");