dpp78272 2017-09-21 08:45
浏览 45
已采纳

如何在MySQL插入中包含PHP变量

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'   )');

</div>
  • 写回答

1条回答 默认 最新

  • drn34916 2017-09-21 08:56
    关注

    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();
        ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题