dongzhi6905 2017-12-20 20:02
浏览 22
已采纳

PDO插入一列

Really idk whats wrong here every time I try to insert into 1 signal column?

Database connected very well, table name is stad, and it contains two columns id AI PRIMARY and name unique

<?php
    @$submit = $_POST['submit'] ;
    @$stadName = $_POST['stadName'];
    if(isset($submit)){
       if(!$stadName){
         echo "Error" ;
       }else{
         $sql = 'INSERT INTO stad name VALUES :name';
         $stmt = $pdo->prepare($sql);
         $stmt->execute(['name' =>  $stadName]);
         echo "DONE" ;
       }
    }
    ?>

connection file

<?php
$serv = '127.0.0.1';
$user = 'root';
$pass = 'root';
$dbname = 'akfoot';

// db base connection
$pdo = new PDO('mysql:dbname='.$dbname.';host='.$serv.'; charset=utf8',$user,$pass);

?>

Database:

enter image description here

  • 写回答

3条回答 默认 最新

  • dsg435665475 2017-12-20 20:04
    关注

    You're missing brackets around your column list and your value list (even if it's just one column, those brackets are required):

    $sql = 'INSERT INTO stad (name) VALUES (:name)';
    $stmt = $pdo->prepare($sql);
    $stmt->execute(['name' =>  $stadName]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 用matlab 实现通信仿真
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档