0

I'm facing a quite strange problem, with an error I've never saw before.

This is my code:

<?php
funcion addPref($nome, $uid, $valore){
   $SQL = "INSERT INTO preferenze_utenti (idPreferenza, idUtente, value) 
   VALUES ((SELECT id FROM preferenze WHERE nome = ?), ?, ?)";
   $stmt = $GLOBALS["db"]->prepare($SQL);
   $stmt->bind_param('sis', $nome, $uid, $valore);
   return $stmt->execute() ? true : false;
}
?>

I'm using the function like this:

addPref("css_animations", 530, "true");

Mind that "true" is a string, because this table accept multiple types of data into it, so I'm just using it like this.

The problem is that actually the function returns always true, and I get no new db record for it. I've tried running the query directly in PHPMyAdmin and works perfectly. I've noticed that i got this warning from MySQL:

Truncated incorrect DOUBLE value: 'css_animations '

Where am I going wrong?

Tables:

CREATE TABLE IF NOT EXISTS `preferenze_utenti` (
  `idPreferenza` bigint(20) NOT NULL,
  `idUtente` bigint(20) NOT NULL,
  `value` varchar(256) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `preferenze` (
`id` bigint(20) NOT NULL,
  `nome` char(64) NOT NULL,
  `def` varchar(128) NOT NULL COMMENT 'Valore default'
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
5
  • Show us your structure of you table pls. Commented Nov 6, 2014 at 13:19
  • whats the table definition of preferenze_utenti? Commented Nov 6, 2014 at 13:19
  • What type is column nome in preferenze? Commented Nov 6, 2014 at 13:24
  • @u_mulder is a string. Commented Nov 6, 2014 at 13:26
  • I've added sql exports of the two tables. Commented Nov 6, 2014 at 13:28

1 Answer 1

1

Do not use VALUES when you use SELECT as on here:

$SQL = "INSERT INTO preferenze_utenti (idPreferenza, idUtente, value)
        VALUES ((SELECT id FROM preferenze WHERE nome = ?), ?, ?)";

Change it to:

$SQL = "INSERT INTO preferenze_utenti (idPreferenza, idUtente, value)
       ((SELECT id FROM preferenze WHERE nome = ?), ?, ?)";
Sign up to request clarification or add additional context in comments.

3 Comments

It worked! I've just rewrote the code using this query and actually worked. What's the difference between using VALUES or not?
@DarioEmerson it is a MySQL syntax standard that we must never use VALUES if the data to insert needs to be fetched by a SELECT statement.
Didn't know that, always used and never had problems with. Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.