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.

2
  • 3
    even at the start, always use prepared statements: php.net/manual/de/mysqli.quickstart.prepared-statements.php Commented Jun 16, 2016 at 11:49
  • Also which DBMS are you using? And what programming language is that? Commented Jun 16, 2016 at 11:56

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

3 Comments

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;
yes you also need the single quotes for inserting strings in db and writing them in your insert statement
please also select an answer and upvote the ones that helped you. thnx
1

try with double quotes

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

1 Comment

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.