0

I'm new in SQL, I would like to know how I can have this code working. Because it doesn't understand that $lala and $lolo are variable.

$lolo = "ahha";
$lala = "2";
$sql = "INSERT INTO organisation (id_orga, nom) VALUES ";
$sqll = '($lala, $lolo)';

$resultat = $conn->query($sql.$sqll);

so I tried:

$lolo = "ahha";
$lala = "2";
$sql = "INSERT INTO organisation (id_orga, nom) VALUES ";
$sqll = '('.$lala.','. $lolo.')';

$resultat = $conn->query($sql.$sqll);

But both code don't INSERT in my BDD and also give no error.

choz
  • 17,242
  • 4
  • 53
  • 73
moskitos
  • 149
  • 1
  • 2
  • 13
  • 3
    even at the start, always use prepared statements: http://php.net/manual/de/mysqli.quickstart.prepared-statements.php – neurotic-d Jun 16 '16 at 11:49
  • Also which DBMS are you using? And what programming language is that? –  Jun 16 '16 at 11:56

2 Answers2

1

try with double quotes

$sqll = "($lala, $lolo)";
Apostolos
  • 10,033
  • 5
  • 24
  • 39
  • 1
    see also http://stackoverflow.com/questions/5605965/php-concatenate-or-directly-insert-variables-in-string – Apostolos Jun 16 '16 at 11:50
1

In PHP the varibles are declared like so:

$lala = "ahah";

When you need to echo or print them you can do the string concatenation like echo 'this is my' . $lala; OR echo "this is my $lala";

This happens because the double quotes allow you to insert the variable without the need to concatenate. So, in your case:

$sqll = '('.$lala.','. $lolo.')';

is correct and is the same as:

$sqll = "($lala, $lolo)";

With the single quotes $sqll = '($lala, $lolo)'; wouldn't work as it would output ($lala, $lolo) and not (2, ahaha)


Note: take a look at pdo which is a better alternative to mysql security-wise

M.M
  • 2,254
  • 1
  • 20
  • 33
  • Well thanks for your great answer but i figure it out here is the working code:`$lolo = "ahha"; $lala = "2"; $sql = "INSERT INTO organisation (id_orga, nom) VALUES "; $sqll = "('".$lala."','". $lolo."')"; $resultat = $conn->query($sql.$sqll); echo $sql.$sqll;` – moskitos Jun 16 '16 at 12:00
  • yes you also need the single quotes for inserting strings in db and writing them in your insert statement – Apostolos Jun 16 '16 at 12:02
  • please also select an answer and upvote the ones that helped you. thnx – Apostolos Jun 16 '16 at 12:11