Ami Ine
Ami Ine

Reputation: 19

How to include a PHP variable inside a MySQL insert

I'm trying to insert values in the contents table. It works fine if I do not have a PHP variable inside VALUES. When I put the variable $address inside VALUES then this doesn't work

$lat= $_GET['lat']; //latitude
$lng= $_GET['lng']; //longitude
$address= $_GET['nom']; // this is an exmple 
    // $address= getAddress($lat,$lng); real fonction my probleme is how to call $address in values
    
   $bdd->exec('INSERT INTO user(nom, prenom, Gsm, Email, Sexe, address) VALUES(\''.$_GET['nom'].'\' , \''.$_GET['prenom'].'\' , \''.$_GET['mobile'].'\' , \''.$_GET['Nemail'].'\' , \''.$_GET['sexe'].'\', '$address'   )');

Upvotes: 1

Views: 77

Answers (1)

Will
Will

Reputation: 2410

You would prefer prepared statement, safer and cleaner.

 <?php
    $stmt = $dbh->prepare("INSERT INTO user(nom, prenom, Gsm, Email, Sexe, address) VALUES(:nom, :prenom, :mobile, :Nemail, :sexe, :address)");
    $stmt->bindParam(':nom', $_GET['nom'];
    $stmt->bindParam(':prenom', $_GET['prenom'];
    $stmt->bindParam(':mobile', $_GET['mobile'];
    $stmt->bindParam(':Nemail', $_GET['Nemail'];
    $stmt->bindParam(':sexe', $_GET['sexe'];
    $stmt->bindParam(':address', $_GET['address'];
    $stmt->execute();
    ?>

Upvotes: 3

Related Questions